mirror of
https://github.com/falsycat/ar.falsy.cat.git
synced 2024-12-26 00:14:51 +00:00
fix: Semantic Search: Use Operand Beta API (#235)
This commit is contained in:
parent
120d104230
commit
d80f6946c8
@ -1,46 +1,54 @@
|
|||||||
import {
|
// Note: Currently, we use the REST API for Operand because of some unpkg/webpack issues.
|
||||||
operandClient,
|
// In the future, we'd like to use the SDK (https://github.com/operandinc/typescript-sdk).
|
||||||
indexIDHeaderKey,
|
// If someone knows how to do this w/o breaking the Operand typescript-sdk for npm users,
|
||||||
} from "https://unpkg.com/@operandinc/sdk@4.1.3/dist/esm/index.js"
|
// please let Morgan (@morgallant) and/or (@_jzhao) know! <3
|
||||||
|
|
||||||
const apiKey = "{{$.Site.Data.config.search.operandApiKey}}"
|
const apiKey = "{{$.Site.Data.config.search.operandApiKey}}"
|
||||||
const indexId = "{{$.Site.Data.config.search.operandIndexId}}"
|
const indexId = "{{$.Site.Data.config.search.operandIndexId}}"
|
||||||
const operand = operandClient(
|
|
||||||
ObjectService,
|
function parseSearchResults(searchResults) {
|
||||||
apiKey,
|
return searchResults.matches.map((m) => ({
|
||||||
"https://api.operand.ai",
|
content: m.content,
|
||||||
{
|
title: searchResults.objects[m.objectId].properties.properties._title.text,
|
||||||
[indexIDHeaderKey]: indexId,
|
url: searchResults.objects[m.objectId].properties.properties._url.text,
|
||||||
}
|
}))
|
||||||
);
|
}
|
||||||
|
|
||||||
async function searchContents(query) {
|
async function searchContents(query) {
|
||||||
const results = await operand.searchWithin({
|
const result = await fetch("https://api.operand.ai/operand.v1.ObjectService/SearchWithin", {
|
||||||
query,
|
method: "POST",
|
||||||
limit: 10,
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `${apiKey}`,
|
||||||
|
"Operand-Index-ID": `${indexId}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
query: query,
|
||||||
|
limit: 8,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
console.log(results.matches)
|
if (result.ok) {
|
||||||
return results.matches.flat()
|
return parseSearchResults(await result.json())
|
||||||
|
} else {
|
||||||
|
console.error(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function debounce(func, timeout = 200) {
|
function debounce(func, timeout = 200) {
|
||||||
let timer;
|
let timer
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
clearTimeout(timer)
|
clearTimeout(timer)
|
||||||
timer = setTimeout(() => { func.apply(this, args); }, timeout)
|
timer = setTimeout(() => {
|
||||||
};
|
func.apply(this, args)
|
||||||
|
}, timeout)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registerHandlers(debounce((e) => {
|
registerHandlers(
|
||||||
term = e.target.value
|
debounce((e) => {
|
||||||
if (term !== "") {
|
let term = e.target.value
|
||||||
searchContents(term)
|
if (term !== "") {
|
||||||
.then((res) => res.results.map(entry => ({
|
searchContents(term).then((results) => displayResults(results))
|
||||||
url: entry.object.properties.url,
|
}
|
||||||
content: entry.snippet,
|
}),
|
||||||
title: entry.object.metadata.title
|
)
|
||||||
})
|
|
||||||
))
|
|
||||||
.then(results => displayResults(results))
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user