Skip to content

Commit 45b1473

Browse files
Merge #896
896: Keep both _geo and _geoloc fields in hits and hits._formatted r=bidoubiwa a=bidoubiwa Instantsearch geo widget requires the geo field to be named `_geoloc`. Nonetheless, the field returned from meilisearch is `_geo`. Previously we replaced `_geo` with `_geoloc` at the root of the hits (so not in `_formatted`) which in itself was a bug. Since `instantsearch` needs `_geoloc` but we also want to avoid confusing the user to why their `_geo` field disappeared, we decided to keep both `_geo` and `_geoloc` files at the root of the hit but also in _formatted (of course only if there was a `_geo` field present to begin with). Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 89a0e8d + 905f643 commit 45b1473

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { adaptGeoResponse } from '../geo-reponse-adapter'
2+
3+
describe('Geopoint adapter', () => {
4+
test('_geoloc field should be created in hit object with _geo fields', () => {
5+
const hits = [
6+
{
7+
id: 2,
8+
_geo: {
9+
lat: 1,
10+
lng: 2,
11+
},
12+
_formatted: {
13+
_geo: {
14+
lat: 1,
15+
lng: 2,
16+
},
17+
},
18+
},
19+
]
20+
21+
const adaptedHits = adaptGeoResponse(hits)
22+
23+
expect(adaptedHits[0]._geoloc).toEqual(hits[0]._geo)
24+
expect(adaptedHits[0]._geo).toEqual(hits[0]._geo)
25+
expect(adaptedHits[0]._formatted._geoloc).toEqual(hits[0]._formatted._geo)
26+
expect(adaptedHits[0]._formatted._geo).toEqual(hits[0]._formatted._geo)
27+
expect(adaptedHits[0].objectID).toBeDefined()
28+
expect(adaptedHits[0]._formatted.objectID).toEqual(adaptedHits[0].objectID)
29+
})
30+
31+
test('_geoloc field should not be created in hit object without _geo fields', () => {
32+
const hits = [
33+
{
34+
id: 2,
35+
},
36+
{
37+
id: 1,
38+
_formatted: {},
39+
},
40+
]
41+
42+
const adaptedHits = adaptGeoResponse(hits)
43+
44+
expect(adaptedHits[0]._geoloc).toBeUndefined()
45+
expect(adaptedHits[0]._geo).toBeUndefined()
46+
expect(adaptedHits[0]._formatted).toBeUndefined()
47+
expect(adaptedHits[1]._geoloc).toBeUndefined()
48+
expect(adaptedHits[1]._geo).toBeUndefined()
49+
expect(adaptedHits[1]._formatted._geoloc).toBeUndefined()
50+
expect(adaptedHits[1]._formatted._geo).toBeUndefined()
51+
})
52+
})

src/adapter/search-response-adapter/geo-reponse-adapter.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
*/
55
export function adaptGeoResponse(hits: any[]): Array<Record<string, any>> {
66
for (let i = 0; i < hits.length; i++) {
7+
const objectID = `${i + Math.random() * 1000000}`
78
if (hits[i]._geo) {
8-
hits[i]._geoloc = {
9-
lat: hits[i]._geo.lat,
10-
lng: hits[i]._geo.lng,
11-
}
9+
hits[i]._geoloc = hits[i]._geo
10+
hits[i].objectID = objectID
11+
}
1212

13-
hits[i].objectID = `${i + Math.random() * 1000000}`
14-
delete hits[i]._geo
13+
if (hits[i]._formatted?._geo) {
14+
hits[i]._formatted._geoloc = hits[i]._formatted._geo
15+
hits[i]._formatted.objectID = objectID
1516
}
1617
}
1718
return hits

0 commit comments

Comments
 (0)