From d285e4e4859fb7cf8cc45bb81848635691af3630 Mon Sep 17 00:00:00 2001
From: BelfordZ <belfordz66@gmail.com>
Date: Thu, 12 Oct 2017 20:49:00 -0700
Subject: [PATCH] fix(search) Update getAutoComplete

---
 package.json                     |  2 +-
 server/controllers/lighthouse.js | 72 ++++++++++++++++++++++----------
 server/utils/importer/index.js   |  2 +-
 3 files changed, 52 insertions(+), 24 deletions(-)

diff --git a/package.json b/package.json
index fac36c6..cf377a2 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
   "name": "lighthouse",
   "description": "Lighthouse is a lightning-fast advanced search engine API for publications on the lbrycrd with autocomplete capabilities.",
   "version": "0.0.1",
-  "author": "filipnyquist <filip@lbry.io>,
+  "author": "filipnyquist <filip@lbry.io>",
   "keywords": [
     "lbry",
     "search",
diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js
index d69a5e3..fbdf141 100644
--- a/server/controllers/lighthouse.js
+++ b/server/controllers/lighthouse.js
@@ -18,6 +18,7 @@ const eclient = new elasticsearch.Client({
   },
 });
 
+
 function getResults (input) {
   if (input.size === undefined) input.size = 10;
   if (input.from === undefined) input.from = 0;
@@ -46,30 +47,57 @@ function getResults (input) {
   });
 }
 
-function getAutoComplete (input) {
-  if (input.size === undefined) input.size = 10;
-  if (input.from === undefined) input.from = 0;
+function getIndex() {
+  // ideally, data is inserted into elastic search with an index that helps us query it faster/better results
+  // A simple start is to default queries to be within the n months, and to make a new index each month.
+  return;
+}
+
+function getRoutingKey() {
+  // This is the most important field for performance. Being able to route the queries ahead of time can make typedowns insanely good.
+  return;
+}
+
+function getAutoCompleteQuery(query) {
+  return {
+    multi_match: {
+      query: query.s.trim(),
+      type: 'phrase_prefix',
+      slop: 5,
+      max_expansions: 50,
+      fields: [
+        'name',
+        'value.stream.metadata.author',
+        'value.stream.metadata.title',
+        'value.stream.metadata.description'
+      ]
+    }
+  };
+}
+
+function getFilter(query) {
+  // this is the best place for putting things like filtering on the type of content
+  // Perhaps we can add search param that will filter on how people have categorized / tagged their content
+  return;
+}
+
+function getAutoComplete(query) {
   return eclient.search({
-    index  : 'claims',
-    _source: ['name', 'value.stream.metadata.title', 'value.stream.metadata.author'],
-    body   : {
-      'query': {
-        'bool': {
-          'must': {
-            'query_string': {
-              'query' : '*' + input.s.trim() + '*',
-              'fields': [
-                'name',
-                'value.stream.metadata.title',
-                'value.stream.metadata.author',
-              ],
-            },
-          },
-        },
-      },
-      size: input.size,
-      from: input.from,
+    index: getIndex(query) || 'claims',
+    routing: getRoutingKey(query),
+    ignore_unavailable: true, // ignore error when date index does not exist
+    body: {
+      size: query.size || 5,
+      from: query.from || 0,
+      query: {
+        bool: {
+          must: getAutoCompleteQuery(query),
+          filter: getFilter(query)
+        }
+      }
     },
+    size: query.size,
+    from: query.from,
   });
 }
 
diff --git a/server/utils/importer/index.js b/server/utils/importer/index.js
index a74b585..f20dc0d 100644
--- a/server/utils/importer/index.js
+++ b/server/utils/importer/index.js
@@ -124,7 +124,7 @@ export async function sync () {
     // Done adding, update our claimTrie cache to latest and wait a bit...
     await saveJSON(path.join(appRoot.path, 'claimTrieCache.json'), latestClaimTrie);
     status.info = 'upToDate';
-    await sleep(300000);
+    await sleep(600000);
     sync();
   } catch (err) {
     winston.log(err);
-- 
2.45.3