aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonika Hedman <monika_hedman@brown.edu>2019-04-30 16:17:39 -0400
committerMonika Hedman <monika_hedman@brown.edu>2019-04-30 16:17:39 -0400
commitdf9fa3e362a6bdd616bc0b46ef9b425cfc5a010d (patch)
tree12d9707bb7035b7b901a22bb1ae2c4357113b385
parent53e183fbc116c406ca86889a9fb3e2de61520b60 (diff)
before pull
-rw-r--r--src/client/views/SearchBox.scss1
-rw-r--r--src/fields/Document.ts15
-rw-r--r--src/server/Search.ts38
-rw-r--r--src/server/database.ts12
-rw-r--r--src/server/index.ts19
5 files changed, 73 insertions, 12 deletions
diff --git a/src/client/views/SearchBox.scss b/src/client/views/SearchBox.scss
index e1a1de142..92363e681 100644
--- a/src/client/views/SearchBox.scss
+++ b/src/client/views/SearchBox.scss
@@ -9,7 +9,6 @@
align-items: center;
.submit-search {
- display: inline-block;
text-align: right;
color: $dark-color;
-webkit-transition: right 0.4s;
diff --git a/src/fields/Document.ts b/src/fields/Document.ts
index 2797efc09..6f6533754 100644
--- a/src/fields/Document.ts
+++ b/src/fields/Document.ts
@@ -29,15 +29,14 @@ export class Document extends Field {
}
static FromJson(data: any, id: string, save: boolean): Document {
let doc = new Document(id, save);
- let fields = data as [string, string][];
- fields.forEach(pair => doc._proxies.set(pair[0], pair[1]));
+ let fields = data as { key: string, field: string }[];
+ fields.forEach(pair => doc._proxies.set(pair.key, pair.field));
return doc;
}
- UpdateFromServer(data: [string, string][]) {
- for (const key in data) {
- const element = data[key];
- this._proxies.set(element[0], element[1]);
+ UpdateFromServer(data: { key: string, field: string }[]) {
+ for (const kv of data) {
+ this._proxies.set(kv.key, kv.field);
}
}
@@ -448,9 +447,9 @@ export class Document extends Field {
}
ToJson() {
- let fields: [string, string][] = [];
+ let fields: { key: string, field: string }[] = [];
this._proxies.forEach((field, key) =>
- field && fields.push([key, field]));
+ field && fields.push({ key, field }));
return {
type: Types.Document,
diff --git a/src/server/Search.ts b/src/server/Search.ts
new file mode 100644
index 000000000..7d8602346
--- /dev/null
+++ b/src/server/Search.ts
@@ -0,0 +1,38 @@
+import * as rp from 'request-promise';
+import { Database } from './database';
+
+export class Search {
+ public static Instance = new Search();
+ private url = 'http://localhost:8983/solr/';
+
+ public updateDocument(document: any): rp.RequestPromise {
+ return rp.post(this.url + "dash/update/json/docs", {
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(document)
+ });
+ }
+
+ public async search(query: string) {
+ const searchResults = JSON.parse(await rp.get(this.url + "dash/select", {
+ qs: {
+ q: query
+ }
+ }));
+ const fields = searchResults.response.docs;
+ const ids = fields.map((field: any) => field.id);
+ const docs = await Database.Instance.searchQuery(ids);
+ const docIds = docs.map((doc: any) => doc._id);
+ return docIds;
+ }
+
+ public async clear() {
+ return rp.post(this.url + "dash/update", {
+ body: {
+ delete: {
+ query: "*:*"
+ }
+ },
+ json: true
+ });
+ }
+} \ No newline at end of file
diff --git a/src/server/database.ts b/src/server/database.ts
index 5457e4dd5..d5905c7b3 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -72,6 +72,18 @@ export class Database {
});
}
+ public searchQuery(ids: string[], collectionName = Database.DocumentsCollection): Promise<any> {
+ return new Promise<any>(resolve => {
+ this.db && this.db.collection(collectionName).find({ "data.field": { "$in": ids } }).toArray((err, docs) => {
+ if (err) {
+ console.log(err.message);
+ console.log(err.errmsg);
+ }
+ resolve(docs);
+ });
+ });
+ }
+
public print() {
console.log("db says hi!");
}
diff --git a/src/server/index.ts b/src/server/index.ts
index 70a7d266c..cb4268a2d 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -11,6 +11,7 @@ import { ObservableMap } from 'mobx';
import * as passport from 'passport';
import * as path from 'path';
import * as request from 'request';
+import * as rp from 'request-promise';
import * as io from 'socket.io';
import { Socket } from 'socket.io';
import * as webpack from 'webpack';
@@ -22,7 +23,7 @@ import { getForgot, getLogin, getLogout, getReset, getSignup, postForgot, postLo
import { DashUserModel } from './authentication/models/user_model';
import { Client } from './Client';
import { Database } from './database';
-import { MessageStore, Transferable } from "./Message";
+import { MessageStore, Transferable, Types } from "./Message";
import { RouteStore } from './RouteStore';
const app = express();
const config = require('../../webpack.config');
@@ -32,6 +33,7 @@ const serverPort = 4321;
import expressFlash = require('express-flash');
import flash = require('connect-flash');
import c = require("crypto");
+import { Search } from './Search';
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
@@ -120,6 +122,12 @@ app.get("/pull", (req, res) =>
// GETTERS
+app.get("/search", async (req, res) => {
+ let query = req.query.query || "hello";
+ let results = await Search.Instance.search(query);
+ res.send(results);
+});
+
// anyone attempting to navigate to localhost at this port will
// first have to login
addSecureRoute(
@@ -234,14 +242,16 @@ server.on("connection", function (socket: Socket) {
Utils.AddServerHandler(socket, MessageStore.DeleteAll, deleteFields);
});
-function deleteFields() {
- return Database.Instance.deleteAll();
+async function deleteFields() {
+ await Database.Instance.deleteAll();
+ await Search.Instance.clear();
}
async function deleteAll() {
await Database.Instance.deleteAll();
await Database.Instance.deleteAll('sessions');
await Database.Instance.deleteAll('users');
+ await Search.Instance.clear();
}
function barReceived(guid: String) {
@@ -260,6 +270,9 @@ function getFields([ids, callback]: [string[], (result: Transferable[]) => void]
function setField(socket: Socket, newValue: Transferable) {
Database.Instance.update(newValue.id, newValue, () =>
socket.broadcast.emit(MessageStore.SetField.Message, newValue));
+ if (newValue.type === Types.Text) {
+ Search.Instance.updateDocument({ id: newValue.id, data: (newValue as any).data });
+ }
}
server.listen(serverPort);