Jacky Zhao
2022-11-20 e9aa6ae9e7ec1792b11ebcb6cac606c47ae3cf7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {
  operandClient,
  indexIDHeaderKey,
} from "https://unpkg.com/@operandinc/sdk@4.1.3/dist/esm/index.js"
 
const apiKey = "{{$.Site.Data.config.search.operandApiKey}}"
const indexId = "{{$.Site.Data.config.search.operandIndexId}}"
const operand = operandClient(
  ObjectService,
  apiKey,
  "https://api.operand.ai",
  {
    [indexIDHeaderKey]: indexId,
  }
);
 
async function searchContents(query) {
  const results = await operand.searchWithin({
    query,
    limit: 10,
  })
  console.log(results.matches)
  return results.matches.flat()
}
 
function debounce(func, timeout = 200) {
  let timer;
  return (...args) => {
    clearTimeout(timer)
    timer = setTimeout(() => { func.apply(this, args); }, timeout)
  };
}
 
registerHandlers(debounce((e) => {
  term = e.target.value
  if (term !== "") {
    searchContents(term)
      .then((res) => res.results.map(entry => ({
        url: entry.object.properties.url,
        content: entry.snippet,
        title: entry.object.metadata.title
      })
      ))
      .then(results => displayResults(results))
  }
}))