From aa5ab03d4ad6e7cb4d9efd3f9c177ebde7e4c4af Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Thu, 02 Mar 2023 17:14:29 +0000
Subject: [PATCH] docs: update to account for github changes

---
 assets/js/semantic-search.js |   76 +++++++++++++++++++++++++-------------
 1 files changed, 50 insertions(+), 26 deletions(-)

diff --git a/assets/js/semantic-search.js b/assets/js/semantic-search.js
index 3cebe5a..fca2851 100644
--- a/assets/js/semantic-search.js
+++ b/assets/js/semantic-search.js
@@ -1,30 +1,54 @@
-const apiKey = "{{$.Site.Data.config.operandApiKey}}"
+// Note: Currently, we use the REST API for Operand because of some unpkg/webpack issues.
+// In the future, we'd like to use the SDK (https://github.com/operandinc/typescript-sdk).
+// If someone knows how to do this w/o breaking the Operand typescript-sdk for npm users,
+// please let Morgan (@morgallant) and/or (@_jzhao) know! <3
 
-async function searchContents(query) {
-  const response = await fetch('https://prod.operand.ai/v3/search/objects', {
-    method: 'POST',
-    headers: {
-      'Content-Type': 'application/json',
-      Authorization: apiKey,
-    },
-    body: JSON.stringify({
-      query,
-      max: 10
-    }),
-  });
-  return (await response.json());
+const apiKey = "{{$.Site.Data.config.search.operandApiKey}}"
+const indexId = "{{$.Site.Data.config.search.operandIndexId}}"
+
+function parseSearchResults(searchResults) {
+  return searchResults.matches.map((m) => ({
+    content: m.content,
+    title: searchResults.objects[m.objectId].properties.properties._title.text,
+    url: searchResults.objects[m.objectId].properties.properties._url.text,
+  }))
 }
 
-registerHandlers((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))
+async function searchContents(query) {
+  const result = await fetch("https://api.operand.ai/operand.v1.ObjectService/SearchWithin", {
+    method: "POST",
+    headers: {
+      "Content-Type": "application/json",
+      Authorization: `${apiKey}`,
+      "Operand-Index-ID": `${indexId}`,
+    },
+    body: JSON.stringify({
+      query: query,
+      limit: 10,
+    }),
+  })
+  if (result.ok) {
+    return parseSearchResults(await result.json())
+  } else {
+    console.error(result)
   }
-})
+}
+
+function debounce(func, timeout = 200) {
+  let timer
+  return (...args) => {
+    clearTimeout(timer)
+    timer = setTimeout(() => {
+      func.apply(this, args)
+    }, timeout)
+  }
+}
+
+registerHandlers(
+  debounce((e) => {
+    let term = e.target.value
+    if (term !== "") {
+      searchContents(term).then((results) => displayResults(term, results))
+    }
+  }),
+)

--
Gitblit v1.10.0