diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/debug/Test.tsx | 7 | ||||
-rw-r--r-- | src/server/Search.ts | 63 | ||||
-rw-r--r-- | src/server/index.ts | 46 |
3 files changed, 113 insertions, 3 deletions
diff --git a/src/debug/Test.tsx b/src/debug/Test.tsx index 04ef00722..57221aa39 100644 --- a/src/debug/Test.tsx +++ b/src/debug/Test.tsx @@ -6,6 +6,7 @@ import { ImageField } from '../new_fields/URLField'; import { Doc } from '../new_fields/Doc'; import { List } from '../new_fields/List'; + const schema1 = createSchema({ hello: "number", test: "string", @@ -41,7 +42,7 @@ class Test extends React.Component { doc.fields = "test"; doc.test = "hello doc"; doc.url = url; - doc.testDoc = doc2; + //doc.testDoc = doc2; const test1: TestDoc = TestDoc(doc); @@ -70,7 +71,9 @@ class Test extends React.Component { } render() { - return <button onClick={this.onClick}>Click me</button>; + return <div><button onClick={this.onClick}>Click me</button> + {/* <input onKeyPress={this.onEnter}></input> */} + </div>; } } diff --git a/src/server/Search.ts b/src/server/Search.ts new file mode 100644 index 000000000..4911edd1d --- /dev/null +++ b/src/server/Search.ts @@ -0,0 +1,63 @@ +import * as rp from 'request-promise'; +import { Database } from './database'; +import { thisExpression } from 'babel-types'; + +export class Search { + public static Instance = new Search(); + private url = 'http://localhost:8983/solr/'; + private client: any; + + constructor() { + console.log("Search Instantiated!"); + var SolrNode = require('solr-node'); + this.client = new SolrNode({ + host: 'localhost', + port: '8983', + core: 'dash', + protocol: 'http' + }); + var strQuery = this.client.query().q('text:test'); + + console.log(strQuery); + + // Search documents using strQuery + // client.search(strQuery, (err: any, result: any) => { + // if (err) { + // console.log(err); + // return; + // } + // console.log('Response:', result.response); + // }); + } + + + public async updateDocument(document: any) { + console.log("UPDATE: ", JSON.stringify(document)); + return rp.post(this.url + "dash/update", { + 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); + return ids; + } + + 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/index.ts b/src/server/index.ts index 6801b3132..2381f9840 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'; @@ -21,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, Diff } from "./Message"; +import { MessageStore, Transferable, Types, Diff } from "./Message"; import { RouteStore } from './RouteStore'; const app = express(); const config = require('../../webpack.config'); @@ -31,6 +32,9 @@ const serverPort = 4321; import expressFlash = require('express-flash'); import flash = require('connect-flash'); import c = require("crypto"); +import { Search } from './Search'; +import { debug } from 'util'; +import _ = require('lodash'); const MongoStore = require('connect-mongo')(session); const mongoose = require('mongoose'); @@ -117,8 +121,17 @@ app.get("/pull", (req, res) => res.redirect("/"); })); +// SEARCH + // GETTERS +app.get("/search", async (req, res) => { + let query = req.query.query || "hello"; + let results = await Search.Instance.search(query); + console.log(results); + res.send(results); +}); + // anyone attempting to navigate to localhost at this port will // first have to login addSecureRoute( @@ -240,6 +253,7 @@ server.on("connection", function (socket: Socket) { async function deleteFields() { await Database.Instance.deleteAll(); + await Search.Instance.clear(); await Database.Instance.deleteAll('newDocuments'); } @@ -248,6 +262,7 @@ async function deleteAll() { await Database.Instance.deleteAll('newDocuments'); await Database.Instance.deleteAll('sessions'); await Database.Instance.deleteAll('users'); + await Search.Instance.clear(); } function barReceived(guid: String) { @@ -266,6 +281,10 @@ 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 }); + console.log("set field"); + } } function GetRefField([id, callback]: [string, (result?: Transferable) => void]) { @@ -276,13 +295,38 @@ function GetRefFields([ids, callback]: [string[], (result?: Transferable[]) => v Database.Instance.getDocuments(ids, callback, "newDocuments"); } + +const suffixMap: { [type: string]: string } = { + "number": "_n", + "string": "_t" +}; function UpdateField(socket: Socket, diff: Diff) { Database.Instance.update(diff.id, diff.diff, () => socket.broadcast.emit(MessageStore.UpdateField.Message, diff), false, "newDocuments"); + const docfield = diff.diff; + const update: any = { id: diff.id }; + console.log("FIELD: ", docfield); + let dynfield = false; + for (let key in docfield) { + if (!key.startsWith("fields.")) continue; + const val = docfield[key]; + const suffix = suffixMap[typeof val]; + if (suffix !== undefined) { + key = key.substring(7); + Object.values(suffixMap).forEach(suf => update[key + suf] = null); + update[key + suffix] = { set: val }; + dynfield = true; + } + } + if (dynfield) { + console.log("dynamic field detected!"); + Search.Instance.updateDocument(update); + } } function CreateField(newValue: any) { Database.Instance.insert(newValue, "newDocuments"); + console.log("created field"); } server.listen(serverPort); |