From 4423fc2cfc96a6ae11a0a82bad74df973bf8a5a0 Mon Sep 17 00:00:00 2001 From: filipnyquist <filip@lbry.io> Date: Mon, 23 Jul 2018 12:06:47 +0200 Subject: [PATCH] This PR adds channels into the index, this combined with the added channel filtering parameter allows us to search specific channels for content. While adding the code for filtering I split the search query into individual parts so it easier can be edited and read. --- server/controllers/lighthouse.js | 236 +++++++++++++++++-------------- server/utils/chainquery/index.js | 20 +-- 2 files changed, 139 insertions(+), 117 deletions(-) diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js index c28faf5..c3f6d16 100644 --- a/server/controllers/lighthouse.js +++ b/server/controllers/lighthouse.js @@ -34,6 +34,124 @@ function getResults (input) { if (input.from + input.size > 10000) { input.from = 10000 - input.size; } + // Search is split up into different parts, all search parts goes under this line. + let channelSearch; + if (input.channel !== undefined) { // If we got a channel argument, lets filter out only that channel + channelSearch = { + 'bool': { + 'must': { + 'query_string': { + 'fields': ['channel'], + 'query' : input.channel, + }, + }, + }, + }; + } + const conBoost = { // Controlling claims should get higher placement in search results. + 'match': { + 'bid_state': { + 'query': 'Controlling', + 'boost': 20, + }, + }, + }; + const matPhraseName = { // Match search text as phrase - Name + 'match_phrase': { + 'name': { + 'query': input.s.trim(), + 'boost': 10, + }, + }, + }; + const matTextName = { // Match search text - Name + 'match': { + 'name': { + 'query': input.s.trim(), + 'boost': 5, + }, + }, + }; + const conTermName = { // Contains search term - Name + 'query_string': { + 'query' : '*' + input.s.trim() + '*', + 'fields': [ + 'name', + ], + 'boost': 3, + }, + }; + const atdSearch = { // ATD search(author, title, desc) + 'nested': { + 'path' : 'value', + 'query': { + 'bool': { + 'should': [ + { // Contains search term in Author, Title, Description + 'query_string': { + 'query' : '*' + input.s.trim() + '*', + 'fields': [ + 'value.stream.metadata.author', + 'value.stream.metadata.title', + 'value.stream.metadata.description', + ], + 'boost': 1, + }, + }, + { // Match search term - Author + 'match': { + 'value.stream.metadata.author': { + 'query': input.s.trim(), + 'boost': 2, + }, + }, + }, + { // Match search text as phrase - Author + 'match_phrase': { + 'value.stream.metadata.author': { + 'query': input.s.trim(), + 'boost': 3, + }, + }, + }, + { // Match search term - Title + 'match': { + 'value.stream.metadata.title': { + 'query': input.s.trim(), + 'boost': 2, + }, + }, + }, + { // Match search text as phrase - Title + 'match_phrase': { + 'value.stream.metadata.title': { + 'query': input.s.trim(), + 'boost': 3, + }, + }, + }, + { // Match search term - Description + 'match': { + 'value.stream.metadata.description': { + 'query': input.s.trim(), + 'boost': 2, + }, + }, + }, + { // Match search text as phrase - Description + 'match_phrase': { + 'value.stream.metadata.description': { + 'query': input.s.trim(), + 'boost': 3, + }, + }, + }, + ], + }, + }, + }, + }; + // End of search parts return eclient.search({ index : 'claims', _source: ['name', 'value', 'claimId'], @@ -41,115 +159,17 @@ function getResults (input) { 'query': { 'bool': { 'should': [ - { - 'match': { - 'bid_state': { - // Controlling claims should get higher placement in search results. - 'query': 'Controlling', - 'boost': 20, - }, - }, - }, + conBoost, ], 'must': [ + channelSearch, { 'bool': { 'should': [ - { // Match search text as phrase - Name - 'match_phrase': { - 'name': { - 'query': input.s.trim(), - 'boost': 10, - }, - }, - }, - { // Match search text - Name - 'match': { - 'name': { - 'query': input.s.trim(), - 'boost': 5, - }, - }, - }, - { // Contains search term - Name - 'query_string': { - 'query' : '*' + input.s.trim() + '*', - 'fields': [ - 'name', - ], - 'boost': 3, - }, - }, - { - 'nested': { - 'path' : 'value', - 'query': { - 'bool': { - 'should': [ - { // Contains search term in Author, Title, Description - 'query_string': { - 'query' : '*' + input.s.trim() + '*', - 'fields': [ - 'value.stream.metadata.author', - 'value.stream.metadata.title', - 'value.stream.metadata.description', - ], - 'boost': 1, - }, - }, - { // Match search term - Author - 'match': { - 'value.stream.metadata.author': { - 'query': input.s.trim(), - 'boost': 2, - }, - }, - }, - { // Match search text as phrase - Author - 'match_phrase': { - 'value.stream.metadata.author': { - 'query': input.s.trim(), - 'boost': 3, - }, - }, - }, - { // Match search term - Title - 'match': { - 'value.stream.metadata.title': { - 'query': input.s.trim(), - 'boost': 2, - }, - }, - }, - { // Match search text as phrase - Title - 'match_phrase': { - 'value.stream.metadata.title': { - 'query': input.s.trim(), - 'boost': 3, - }, - }, - }, - { // Match search term - Description - 'match': { - 'value.stream.metadata.description': { - 'query': input.s.trim(), - 'boost': 2, - }, - }, - }, - { // Match search text as phrase - Description - 'match_phrase': { - 'value.stream.metadata.description': { - 'query': input.s.trim(), - 'boost': 3, - }, - }, - }, - ], - }, - }, - }, - }, + matPhraseName, + matTextName, + conTermName, + atdSearch, ], }, }, @@ -255,10 +275,10 @@ class LighthouseControllers { }); } - /** - * Autocomplete API Endpoint. - * @param {ctx} Koa Context - */ + /** + * Autocomplete API Endpoint. + * @param {ctx} Koa Context + */ async autoComplete (ctx) { await getAutoComplete(ctx.query).then(function (result) { let results = result.hits.hits; diff --git a/server/utils/chainquery/index.js b/server/utils/chainquery/index.js index 61afa6a..a961c84 100644 --- a/server/utils/chainquery/index.js +++ b/server/utils/chainquery/index.js @@ -156,15 +156,17 @@ function getClaimsSince (time) { return new Promise((resolve, reject) => { let query = `` + `SELECT ` + - `name, ` + - `value_as_json as value, ` + - `bid_state, ` + - `effective_amount, ` + - `claim_id as claimId ` + - // `,transaction_by_hash_id, ` + // txhash and vout needed to leverage old format for comparison. - // `vout ` + - `FROM claim ` + - `WHERE modified >='` + time + `'`; + `c.name,` + + `p.name as channel,` + + `c.bid_state,` + + `c.effective_amount,` + + `c.claim_id as claimId,` + + `c.value_as_json as value ` + + // `,transaction_by_hash_id, ` + // txhash and vout needed to leverage old format for comparison. + // `vout ` + + `FROM claim c ` + + `LEFT JOIN claim p on p.claim_id = c.publisher_id ` + + `WHERE c.modified >='` + time + `'`; // Outputs full query to console for copy/paste into chainquery (debugging) // console.log(query); rp(`https://chainquery.lbry.io/api/sql?query=` + query)