diff options
-rw-r--r-- | src/client/SocketStub.ts | 2 | ||||
-rw-r--r-- | src/fields/Document.ts | 6 | ||||
-rw-r--r-- | src/server/Search.ts | 27 | ||||
-rw-r--r-- | src/server/ServerUtil.ts | 4 | ||||
-rw-r--r-- | src/server/database.ts | 12 | ||||
-rw-r--r-- | src/server/index.ts | 12 |
6 files changed, 56 insertions, 7 deletions
diff --git a/src/client/SocketStub.ts b/src/client/SocketStub.ts index 5e2ca6a98..df5d12827 100644 --- a/src/client/SocketStub.ts +++ b/src/client/SocketStub.ts @@ -37,7 +37,7 @@ export class SocketStub { // document.fields.forEach((f, key) => (this.FieldStore.get(document.Id) as Document)._proxies.set(key.Id, (f as Field).Id)); console.log("sending " + document.Title); - Utils.Emit(Server.Socket, MessageStore.AddDocument, new DocumentTransfer(document.ToJson())); + // Utils.Emit(Server.Socket, MessageStore.AddDocument, new DocumentTransfer(document.ToJson())); } public static SEND_FIELD_REQUEST(fieldid: FieldId): Promise<Opt<Field>>; diff --git a/src/fields/Document.ts b/src/fields/Document.ts index 628fe684c..884e7374b 100644 --- a/src/fields/Document.ts +++ b/src/fields/Document.ts @@ -410,11 +410,11 @@ export class Document extends Field { return copy; } - ToJson(): { type: Types; data: [string, string][]; _id: string } { - let fields: [string, string][] = []; + ToJson(): { type: Types; data: { key: string, field: string }[]; _id: string } { + let fields: { key: string, field: string }[] = []; this._proxies.forEach((field, key) => { if (field) { - fields.push([key, field]); + fields.push({ key, field }); } }); 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/ServerUtil.ts b/src/server/ServerUtil.ts index 0973f82b1..dc8687b7e 100644 --- a/src/server/ServerUtil.ts +++ b/src/server/ServerUtil.ts @@ -73,9 +73,9 @@ export class ServerUtils { return InkField.FromJson(id, data); case Types.Document: let doc: Document = new Document(id, false); - let fields: [string, string][] = data as [string, string][]; + let fields: { key: string, field: string }[] = data as { key: string, field: string }[]; fields.forEach(element => { - doc._proxies.set(element[0], element[1]); + doc._proxies.set(element.key, element.field); }); return doc; default: diff --git a/src/server/database.ts b/src/server/database.ts index 3290edde0..4011f26bd 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -71,6 +71,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 a6fe6fa2c..fef26f78a 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,6 +32,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'); @@ -118,6 +119,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( @@ -258,6 +265,9 @@ function getFields([ids, callback]: [string[], (result: any) => 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); |