aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Search.ts27
-rw-r--r--src/server/database.ts12
-rw-r--r--src/server/index.ts14
3 files changed, 51 insertions, 2 deletions
diff --git a/src/server/Search.ts b/src/server/Search.ts
new file mode 100644
index 000000000..f9babc433
--- /dev/null
+++ b/src/server/Search.ts
@@ -0,0 +1,27 @@
+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;
+ }
+} \ No newline at end of file
diff --git a/src/server/database.ts b/src/server/database.ts
index 7914febf8..5c70da931 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 3cbe1ca76..bea84c6ed 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -22,7 +22,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,10 +32,11 @@ 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');
-const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));;
+const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));
const mongoUrl = 'mongodb://localhost:27017/Dash';
mongoose.connect(mongoUrl);
@@ -120,6 +121,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(
@@ -260,6 +267,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);