From f4eeccca788fb567d95af03e4d458b6ae0d1a7bb Mon Sep 17 00:00:00 2001
From: Mike Lynch <mike@mikelynch.org>
Date: Tue, 3 Dec 2019 12:09:09 +1100
Subject: [PATCH] Adding elasticsearch experiment

---
 TODO.md                          | 10 +++++++
 src/components/ElasticService.js | 50 ++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 TODO.md
 create mode 100644 src/components/ElasticService.js

diff --git a/TODO.md b/TODO.md
new file mode 100644
index 0000000..d6d87d8
--- /dev/null
+++ b/TODO.md
@@ -0,0 +1,10 @@
+TODO
+====
+
+
+
+Sorting out rendering niggles --
+
+in the test data, Darryl Veitch has an affilliation
+
+http://localhost:9000/#view/https://www.uts.edu.au/staff/darryl.veitch
\ No newline at end of file
diff --git a/src/components/ElasticService.js b/src/components/ElasticService.js
new file mode 100644
index 0000000..27470b6
--- /dev/null
+++ b/src/components/ElasticService.js
@@ -0,0 +1,50 @@
+const axios = require('axios');
+
+const ElasticService = {
+  get: async function (config, data) {
+    try {
+      data = encodeURIComponent(data);
+      console.log(`Querying ElasticSearch - ${config.api}?q=id:${data}`);
+      const res = await axios.get(`${config.api}?q=id:${data}`);
+      console.log("ElasticSearch returned: " + res.status);
+      if (res.data) {
+        return {data: res.data, status: res.status};
+      } else {
+        return {data: {}, status: res.status};
+      }
+    } catch (e) {
+      return {data: {}, status: e.message};
+    }
+  },
+
+  // elasticsearch pagination params are 'from' and 'size'
+  // I don't think that the page function has any purpose in the solr query?
+
+  search: async function (config, {start: start, page: page, searchParam: searchParam, text: text, facets: facets, facetLimit: facetLimit}) {
+    try {
+      let param = `select?q=`;      
+      if (text === '' || !text ) {
+        text = '*';
+      }
+      let escText = text.replace(':', "\\:");
+      let query = {
+        match: { "*": text }
+      };
+
+      //`q=${searchParam}:${escText}&from=${start}`;
+      console.log(`Querying ElasticSearch - ${config.api} ${JSON.stringify(query)}`);
+      const res = await axios.post(config.api, { query: query });
+      console.log("ElasticSearch returned: " + res.status);
+      if (res.data) {
+        return {data: res.data['response'], status: res.status};
+      } else {
+        console.log("Empty response");
+        return {data: [], status: res.status};
+      }
+    } catch (e) {
+      return {data: [], status: e.message};
+    }
+  }
+};
+
+module.exports = ElasticService;
-- 
GitLab