-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from fogx/patch-1
Update vector search example in README.md
- Loading branch information
Showing
1 changed file
with
33 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -582,7 +582,7 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({ | |
The general idea is to first hook into the query life-cycle of Instantsearch, intercept the typed query and send it to an embedding API, fetch the embeddings and then send the vectors to Typesense to do a nearest neighbor vector search. | ||
|
||
Here's a demo that you can run locally to see this in action: [https://github.com/typesense/typesense-instantsearch-semantic-search-demo](https://github.com/typesense/typesense-instantsearch-semantic-search-demo). | ||
Here's a demo that you can run locally to see this in action: [https://github.com/typesense/showcase-hn-comments-semantic-search](https://github.com/typesense/showcase-hn-comments-semantic-search). | ||
|
||
Here's how to do this in Instantsearch.js: | ||
|
||
|
@@ -601,39 +601,39 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({ | |
}, | ||
additionalSearchParameters, | ||
}); | ||
// from https://github.com/typesense/showcase-hn-comments-semantic-search/blob/8a33006cae58b425c53f56a64e1273e808cd9375/src/js/index.js#L101 | ||
const searchClient = typesenseInstantsearchAdapter.searchClient; | ||
const search = instantsearch({ | ||
searchClient, | ||
indexName: "products", | ||
routing: true, | ||
async searchFunction(helper) { | ||
const query = helper.getQuery().query; | ||
const page = helper.getPage(); // Retrieve the current page | ||
const totalNearestNeighborsToFetch = 1000; | ||
|
||
if (query !== "") { | ||
// Get embedding for the query | ||
let response = await fetch( | ||
"http://localhost:8000/embedding?" + new URLSearchParams({ q: query }), // <=== Embedding API | ||
); | ||
|
||
let parsedResponse = await response.json(); | ||
|
||
console.log(parsedResponse); | ||
|
||
// Send the embedding to Typesense to do a nearest neighbor search | ||
helper | ||
.setQueryParameter( | ||
"typesenseVectorQuery", // <=== Special parameter that only works in [email protected] and above | ||
`vectors:([${parsedResponse["embedding"].join(",")}], k:${totalNearestNeighborsToFetch})`, | ||
) | ||
.setPage(page) | ||
.search(); | ||
} else { | ||
helper.setQueryParameter("typesenseVectorQuery", null).setPage(page).search(); | ||
} | ||
}, | ||
}); | ||
search = instantsearch({ | ||
searchClient, | ||
indexName: INDEX_NAME, | ||
routing: true, | ||
async searchFunction(helper) { | ||
// This fetches 200 (nearest neighbor) results for semantic / hybrid search | ||
|
||
let query = helper.getQuery().query; | ||
const page = helper.getPage(); // Retrieve the current page | ||
|
||
if ( | ||
query !== "" && | ||
["semantic", "hybrid"].includes($("#search-type-select").val()) | ||
) { | ||
console.log(helper.getQuery().query); | ||
helper | ||
.setQueryParameter( | ||
"typesenseVectorQuery", // <=== Special parameter that only works in [email protected] and above | ||
`embedding:([], k:200)`, | ||
) | ||
.setPage(page) | ||
.search(); | ||
console.log(helper.getQuery().query); | ||
} else { | ||
helper | ||
.setQueryParameter("typesenseVectorQuery", null) | ||
.setPage(page) | ||
.search(); | ||
} | ||
}, | ||
}); | ||
``` | ||
|
||
## Compatibility | ||
|