Skip to content

Commit ac69707

Browse files
committed
Add geo search response adapter
1 parent 7c58925 commit ac69707

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/adapter/search-request-adapter/geo-rules-adapter.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { SearchContext, GeoSearchContext } from '../../types'
22
import { getDistanceInMeter, middleGeoPoints } from '../../utils/geographic'
33

44
export function adaptGeoPointsRules(
5-
geoSearchContext: GeoSearchContext
5+
geoSearchContext?: GeoSearchContext
66
): { filter?: string; sort?: string } | undefined {
7+
if (!geoSearchContext) {
8+
return undefined
9+
}
710
const {
811
insideBoundingBox,
912
aroundLatLng,
@@ -17,8 +20,9 @@ export function adaptGeoPointsRules(
1720
if (aroundLatLng) {
1821
middlePoint = aroundLatLng
1922
}
20-
if (aroundRadius || minimumAroundRadius) {
21-
radius = aroundRadius
23+
if (aroundRadius != null || minimumAroundRadius != null) {
24+
if (aroundRadius != null) radius = aroundRadius
25+
else radius = minimumAroundRadius
2226
}
2327

2428
// If insideBoundingBox is provided it takes precedent over all other options
@@ -35,7 +39,7 @@ export function adaptGeoPointsRules(
3539
middlePoint = middleGeoPoints(lat1, lng1, lat2, lng2)
3640
}
3741

38-
if (middlePoint && radius) {
42+
if (middlePoint != null && radius != null) {
3943
const [lat3, lng3] = middlePoint.split(',')
4044

4145
// check if radius is big enough
@@ -44,7 +48,7 @@ export function adaptGeoPointsRules(
4448
const sort = `_geoPoint(${lat3}, ${lng3}):asc`
4549

4650
return { filter, sort }
47-
} else if (middlePoint) {
51+
} else if (middlePoint != null) {
4852
const [lat3, lng3] = middlePoint.split(',')
4953
const sort = `_geoPoint(${lat3}, ${lng3}):asc`
5054
return { sort }
@@ -67,7 +71,6 @@ export function createGeoSearchContext(
6771
} = searchContext
6872

6973
if (aroundLatLng) {
70-
// only filter
7174
geoContext.aroundLatLng = aroundLatLng
7275
}
7376

@@ -84,7 +87,7 @@ export function createGeoSearchContext(
8487
See this discussion to track its implementation https://github.com/meilisearch/product/discussions/264`)
8588
}
8689

87-
if (minimumAroundRadius && aroundLatLng) {
90+
if (minimumAroundRadius) {
8891
geoContext.minimumAroundRadius = minimumAroundRadius
8992
}
9093

@@ -93,7 +96,9 @@ export function createGeoSearchContext(
9396
}
9497
// TODO: issue
9598
if (insidePolygon) {
96-
geoContext.insidePolygon = insidePolygon
99+
console.warn(
100+
`instant-meilisearch: \`insidePolygon\` is not implented in instant-meilisearch.`
101+
)
97102
}
98103
return geoContext
99104
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {any[]} hits
3+
* @returns {Array<Record<string, any>>}
4+
*/
5+
export function adaptGeoResponse(hits: any[]): Array<Record<string, any>> {
6+
for (let i = 0; i < hits.length; i++) {
7+
if (hits[i]._geo) {
8+
hits[i]._geoloc = {
9+
lat: hits[i]._geo.lat,
10+
lng: hits[i]._geo.lng,
11+
}
12+
13+
hits[i].objectID = `${i + Math.random() * 1000000}`
14+
delete hits[i]._geo
15+
}
16+
}
17+
return hits
18+
}

src/adapter/search-response-adapter/hits-adapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { PaginationContext, SearchContext } from '../../types'
22
import { adaptPagination } from './pagination-adapter'
33
import { adaptFormating } from './highlight-adapter'
4+
import { adaptGeoResponse } from './geo-reponse-adapter'
45

56
/**
67
* @param {Array<Record<string} hits
@@ -17,7 +18,7 @@ export function adaptHits(
1718
const { hitsPerPage, page } = paginationContext
1819
const paginatedHits = adaptPagination(hits, page, hitsPerPage)
1920

20-
return paginatedHits.map((hit: any) => {
21+
let formattedHits = paginatedHits.map((hit: Record<string, any>) => {
2122
// Creates Hit object compliant with InstantSearch
2223
if (Object.keys(hit).length > 0) {
2324
const { _formatted: formattedHit, _matchesInfo, ...restOfHit } = hit
@@ -29,4 +30,6 @@ export function adaptHits(
2930
}
3031
return hit
3132
})
33+
formattedHits = adaptGeoResponse(formattedHits)
34+
return formattedHits
3235
}

0 commit comments

Comments
 (0)