Skip to content

Commit 871b675

Browse files
meili-bors[bot]queerflevi29
authored
Merge #1314
1314: 🐛 fix: Don't overwrite object ID in geosearch response r=brunoocasali a=queer # Pull Request ## Related issue Fixes #1308. ## What does this PR do? This avoids overwriting the `objectID` field if it is already present. This allows code that depends on the `objectID` returned by Meilisearch to function, while still adding it for data sets that don't have it. This also updates `CONTRIBUTING.md` to have the right command for the geosearch playground. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: amy null <null@amy.gg> Co-authored-by: F. Levi <55688616+flevi29@users.noreply.github.com>
2 parents 8b757e8 + fa02104 commit 871b675

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

.changeset/hip-geese-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@meilisearch/instant-meilisearch": patch
3+
---
4+
5+
Don't write objectID property if it already exists when adapting geosearch.

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ yarn playground:html
108108
An additional playground is provided to test out the [GeoSearch](./packages/instant-meilisearch#-geo-search).
109109

110110
```bash
111-
yarn playground:geo-javascript
111+
yarn playground:geosearch
112112
```
113113

114114
Note: If the Google Maps stopped working, please create a new [Google Api Key](https://developers.google.com/maps/documentation/javascript/get-api-key) and add it in the `.env` file at the root of the playground: `/playgrounds/geo-javascript`

packages/instant-meilisearch/src/adapter/search-response-adapter/__tests__/geo-adapter.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,29 @@ describe('Geopoint adapter', () => {
4949
expect(adaptedHits[1]._formatted._geoloc).toBeUndefined()
5050
expect(adaptedHits[1]._formatted._geo).toBeUndefined()
5151
})
52+
53+
test('hits with _geoloc field should not overwrite `objectID` property', () => {
54+
const hits = [
55+
{
56+
objectID: '2',
57+
id: 2,
58+
_geo: {
59+
lat: 1,
60+
lng: 2,
61+
},
62+
_formatted: {
63+
_geo: {
64+
lat: 1,
65+
lng: 2,
66+
},
67+
},
68+
},
69+
]
70+
71+
const adaptedHits = adaptGeoResponse(hits)
72+
73+
expect(adaptedHits[0]._geoloc).toEqual(hits[0]._geo)
74+
expect(adaptedHits[0]._geo).toEqual(hits[0]._geo)
75+
expect(adaptedHits[0].objectID).toEqual(hits[0].objectID)
76+
})
5277
})

packages/instant-meilisearch/src/adapter/search-response-adapter/geo-reponse-adapter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ export function adaptGeoResponse(hits: any[]): Array<Record<string, any>> {
77
const objectID = `${i + Math.random() * 1000000}`
88
if (hits[i]._geo) {
99
hits[i]._geoloc = hits[i]._geo
10-
hits[i].objectID = objectID
10+
if (!hits[i].objectID) {
11+
hits[i].objectID = objectID
12+
}
1113
}
1214

1315
if (hits[i]._formatted?._geo) {
1416
hits[i]._formatted._geoloc = hits[i]._formatted._geo
15-
hits[i]._formatted.objectID = objectID
17+
if (!hits[i]._formatted.objectID) {
18+
hits[i]._formatted.objectID = objectID
19+
}
1620
}
1721
}
1822
return hits

0 commit comments

Comments
 (0)