From ff28e70251500c79061c1b6c0d75651569489545 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Tue, 28 Aug 2018 21:14:49 +0300 Subject: [PATCH] feat(search): search among info indexes --- src/app/torrent.js | 7 +++++++ src/background/mysql.js | 13 ++++++++++++- src/background/spider.js | 14 ++++++++++++-- tests/sphinx.api.test.js | 9 +++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/app/torrent.js b/src/app/torrent.js index d3be55b..ac2bdf5 100644 --- a/src/app/torrent.js +++ b/src/app/torrent.js @@ -302,6 +302,13 @@ export default class Torrent extends Component { node.onclick = () => { return false } }}>
+ { + torrent.info && torrent.info.name + && +
+ {torrent.info.name} +
+ }
{ diff --git a/src/background/mysql.js b/src/background/mysql.js index d241968..48f5aba 100644 --- a/src/background/mysql.js +++ b/src/background/mysql.js @@ -133,8 +133,19 @@ const expand = (sphinx) => { let data = ''; const parseValues = (values) => { if(sphinxIndex) + { for(const k in sphinxIndex) - values[k] = values[sphinxIndex[k]] + { + if(typeof sphinxIndex[k] === 'string') + { + values[k] = values[sphinxIndex[k]] + } + else if (typeof sphinxIndex[k] === 'function') + { + values[k] = sphinxIndex[k](values) + } + } + } let valuesData = '' names = '' diff --git a/src/background/spider.js b/src/background/spider.js index c3e55ab..77c897b 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -138,7 +138,7 @@ module.exports = function (send, recive, dataDirectory, version, env) this.sphinx.replaceValues('torrents', {hash, info: data}, { particial: true, key: 'hash', - sphinxIndex: {nameIndex: 'name'}, + sphinxIndex: {nameIndex: (obj) => buildTorrentIndex(obj)}, merge: ['info'], mergeCallback: (n, obj) => { if(n != 'info') @@ -442,6 +442,16 @@ module.exports = function (send, recive, dataDirectory, version, env) return torrent } + const buildTorrentIndex = (torrent) => { + let index = torrent.name + if(torrent.info && typeof torrent.info.name === 'string' && torrent.info.name.length > 0) + { + if(torrent.info.name.length < 800) + index += ' ' + torrent.info.name + } + return index + } + const insertTorrentToDB = (torrent, silent) => new Promise((resolve) => { if(!torrent) { @@ -533,7 +543,7 @@ module.exports = function (send, recive, dataDirectory, version, env) addFilesToDatabase() } - torrent.nameIndex = torrent.name + torrent.nameIndex = buildTorrentIndex(torrent) sphinxSingle.insertValues('torrents', torrent, function(err, result) { if(result) { diff --git a/tests/sphinx.api.test.js b/tests/sphinx.api.test.js index a763373..42e5109 100644 --- a/tests/sphinx.api.test.js +++ b/tests/sphinx.api.test.js @@ -35,6 +35,15 @@ describe("big table for check", () => { assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":1,"b":2,"c":3,"d":6,"e":5}') }) + it("replace text index as function", async function() { + await sphinx.replaceValues('feed', {id: 1, data: {a: 6}}, {particial: true, sphinxIndex: { feedIndex: (obj) => { + assert.equal(obj.data.a, 6) + return "aabbccdd" + }} }) + assert.equal((await sphinx.query(`select data from feed where id = 1`))[0].data, '{"a":6}') + assert.equal((await sphinx.query(`select id from feed where match('aabbccdd')`))[0].id, 1) + }) + it("insert object to database", async function() { const obj = {a: 1, v: 2} const p = {id: 3, data: obj}