Skip to content

Commit

Permalink
fix(core): include _originalId in groq2024 search, update refs adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 24, 2025
1 parent 5dc4a6b commit 5358550
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ export function referenceSearch(
})
return search(textTerm, {includeDrafts: true}).pipe(
map(({hits}) => hits.map(({hit}) => hit)),
map((docs) =>
docs.map((doc) => ({
...doc,
// Pass the original id if available, it could be a `draftId` or a `versionId` , the _id will be the published one when using perspectives or groq2024 to query the data.
_id: (doc._originalId as string) || doc._id,
})),
),
map((docs) => collate(docs)),
// pick the 100 best matches
map((collated) => collated.slice(0, 100)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('createSearchQuery', () => {
expect(query).toMatchInlineSnapshot(
`
"// findability-mvi:5
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["basic-schema-test"] && title match text::query($__query), 10), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id}"
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["basic-schema-test"] && title match text::query($__query), 10), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id, _originalId}"
`,
)

Expand Down Expand Up @@ -145,7 +145,7 @@ describe('createSearchQuery', () => {

expect(query).toMatchInlineSnapshot(`
"// findability-mvi:5
*[_type in $__types && [@, _id] match text::query($__query) && !(_id in path("versions.**"))] | order(exampleField desc) [0...$__limit] {exampleField, _type, _id}"
*[_type in $__types && [@, _id] match text::query($__query) && !(_id in path("versions.**"))] | order(exampleField desc) [0...$__limit] {exampleField, _type, _id, _originalId}"
`)

expect(query).toContain('| order(exampleField desc)')
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('createSearchQuery', () => {

expect(query).toMatchInlineSnapshot(`
"// findability-mvi:5
*[_type in $__types && [@, _id] match text::query($__query) && !(_id in path("versions.**"))] | order(exampleField desc,anotherExampleField asc,lower(mapWithField) asc) [0...$__limit] {exampleField, anotherExampleField, mapWithField, _type, _id}"
*[_type in $__types && [@, _id] match text::query($__query) && !(_id in path("versions.**"))] | order(exampleField desc,anotherExampleField asc,lower(mapWithField) asc) [0...$__limit] {exampleField, anotherExampleField, mapWithField, _type, _id, _originalId}"
`)

expect(query).toContain(
Expand All @@ -198,7 +198,7 @@ describe('createSearchQuery', () => {

expect(query).toMatchInlineSnapshot(`
"// findability-mvi:5
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["basic-schema-test"] && title match text::query($__query), 10), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id}"
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["basic-schema-test"] && title match text::query($__query), 10), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id, _originalId}"
`)

expect(query).toContain('| order(_score desc)')
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('createSearchQuery', () => {

expect(query).toMatchInlineSnapshot(`
"// findability-mvi:5
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["numbers-in-path"] && cover[].cards[].title match text::query($__query), 5), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id}"
*[_type in $__types && !(_id in path("versions.**"))] | score(boost(_type in ["numbers-in-path"] && cover[].cards[].title match text::query($__query), 5), [@, _id] match text::query($__query)) | order(_score desc) [_score > 0] [0...$__limit] {_score, _type, _id, _originalId}"
`)

expect(query).toContain('cover[].cards[].title match text::query($__query), 5)')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function createSearchQuery(
options.cursor ?? [],
].flat()

const projectionFields = sortOrder.map(({field}) => field).concat('_type', '_id')
const projectionFields = sortOrder.map(({field}) => field).concat('_type', '_id', '_originalId')
const projection = projectionFields.join(', ')

const query = [
Expand Down

0 comments on commit 5358550

Please sign in to comment.