Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instant-meilisearch: Fix _matchesPosition not being included in hits #1306

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-ways-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@meilisearch/instant-meilisearch": patch
---

Fix \_matchesPosition not being included in hits
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure the best way to test this, or if a test was needed, but took a stab at it. Happy to remove it or (under guidance) improve it

Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { adaptHits } from '../hits-adapter'

const searchResponsePositionMatchesFalse = {
indexUid: 'steam-video-games',
hits: [
{
name: 'Half-Life: Opposing Force',
description:
'Return to the Black Mesa Research Facility as one of the military specialists assigned to eliminate Gordon Freeman. Experience an entirely new episode of single player action. Meet fierce alien opponents and experiment with new weaponry. Named Game of the Year by the Academy of Interactive Arts and Sciences.',
id: '50',
price: '4.99 $',
image:
'http://steam.meilisearch.dev/steam/apps/50/header.jpg?t=1447350813',
releaseDate: 'Nov 1 1999',
recommendationCount: 2934,
platforms: ['MacOS', 'Linux', 'Windows'],
players: ['Single player', 'Multiplayer'],
genres: ['Action'],
misc: [],
_formatted: {
name: '__ais-highlight__Half__/ais-highlight__-Life: Opposing Force',
description:
'Return to the Black Mesa Research Facility as one of the military specialists assigned to eliminate Gordon Freeman. Experience an entirely new episode of single player action. Meet fierce alien opponents and experiment with new weaponry. Named Game of the Year by the Academy of Interactive Arts and Sciences.',
id: '50',
price: '4.99 $',
image:
'http://steam.meilisearch.dev/steam/apps/50/header.jpg?t=1447350813',
releaseDate: 'Nov 1 1999',
recommendationCount: '2934',
platforms: ['MacOS', 'Linux', 'Windows'],
players: ['Single player', 'Multiplayer'],
genres: ['Action'],
misc: [],
},
},
],
query: 'half',
processingTimeMs: 2,
hitsPerPage: 6,
page: 1,
totalPages: 39,
totalHits: 232,
facetDistribution: {
genres: {
Action: 118,
Adventure: 76,
Casual: 39,
RPG: 44,
Racing: 2,
Simulation: 35,
Sports: 10,
Strategy: 47,
},
misc: {
'Early access': 33,
'Free to play': 20,
'VR support': 11,
},
platforms: {
Linux: 68,
MacOS: 96,
Windows: 232,
},
players: {
Coop: 32,
MMO: 5,
Multiplayer: 76,
'Single player': 206,
},
},
facetStats: {},
pagination: {
hitsPerPage: 6,
page: 0,
finite: true,
},
}

const searchResponsePositionMatchesTrue = {
indexUid: 'steam-video-games',
hits: [
{
name: 'Half-Life: Opposing Force',
description:
'Return to the Black Mesa Research Facility as one of the military specialists assigned to eliminate Gordon Freeman. Experience an entirely new episode of single player action. Meet fierce alien opponents and experiment with new weaponry. Named Game of the Year by the Academy of Interactive Arts and Sciences.',
id: '50',
price: '4.99 $',
image:
'http://steam.meilisearch.dev/steam/apps/50/header.jpg?t=1447350813',
releaseDate: 'Nov 1 1999',
recommendationCount: 2934,
platforms: ['MacOS', 'Linux', 'Windows'],
players: ['Single player', 'Multiplayer'],
genres: ['Action'],
misc: [],
_formatted: {
name: '__ais-highlight__Half__/ais-highlight__-Life: Opposing Force',
description:
'Return to the Black Mesa Research Facility as one of the military specialists assigned to eliminate Gordon Freeman. Experience an entirely new episode of single player action. Meet fierce alien opponents and experiment with new weaponry. Named Game of the Year by the Academy of Interactive Arts and Sciences.',
id: '50',
price: '4.99 $',
image:
'http://steam.meilisearch.dev/steam/apps/50/header.jpg?t=1447350813',
releaseDate: 'Nov 1 1999',
recommendationCount: '2934',
platforms: ['MacOS', 'Linux', 'Windows'],
players: ['Single player', 'Multiplayer'],
genres: ['Action'],
misc: [],
},
_matchesPosition: {
name: [
{
start: 0,
length: 4,
},
],
},
},
],
query: 'half',
processingTimeMs: 2,
hitsPerPage: 6,
page: 1,
totalPages: 39,
totalHits: 232,
facetDistribution: {
genres: {
Action: 118,
Adventure: 76,
Casual: 39,
RPG: 44,
Racing: 2,
Simulation: 35,
Sports: 10,
Strategy: 47,
},
misc: {
'Early access': 33,
'Free to play': 20,
'VR support': 11,
},
platforms: {
Linux: 68,
MacOS: 96,
Windows: 232,
},
players: {
Coop: 32,
MMO: 5,
Multiplayer: 76,
'Single player': 206,
},
},
facetStats: {},
pagination: {
hitsPerPage: 6,
page: 0,
finite: true,
},
}

describe('Hit adapter', () => {
test('_matchesPosition should be created in hit object with meiliSearchParams.showMatchesPosition=true', () => {
const expectedPositionMatch = {
name: [
{
start: 0,
length: 4,
},
],
}

const config = {
placeholderSearch: true,
keepZeroFacets: false,
clientAgents: [],
finitePagination: true,
meiliSearchParams: {
showMatchesPosition: true,
},
}

const adaptedHits = adaptHits(searchResponsePositionMatchesTrue, config)

expect(adaptedHits[0]._matchesPosition).toEqual(expectedPositionMatch)
})

test('_matchesPosition should NOT be created in hit object with meiliSearchParams.showMatchesPosition=false', () => {
const config = {
placeholderSearch: true,
keepZeroFacets: false,
clientAgents: [],
finitePagination: true,
meiliSearchParams: {
showMatchesPosition: false,
},
}

const adaptedHits = adaptHits(searchResponsePositionMatchesFalse, config)

expect(adaptedHits[0]).not.toHaveProperty('_matchesPosition')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export function adaptHits(
adaptFormattedFields(formattedHit)
)

if (config?.meiliSearchParams?.showMatchesPosition) {
adaptedHit._matchesPosition = _matchesPosition
}

if (primaryKey) {
adaptedHit.objectID = hit[primaryKey]
}
Expand Down
Loading