diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js index e18ef34..2e9c818 100644 --- a/server/controllers/lighthouse.js +++ b/server/controllers/lighthouse.js @@ -34,10 +34,6 @@ function getResults (input) { if (input.from + input.size > 10000) { input.from = 10000 - input.size; } - let trimmedQuery = input.s.trim(); - let escapedQuery = getWashedQuery(getEscapedQuery(trimmedQuery)); - let washedQuery = getWashedQuery(trimmedQuery); - let effectiveFactor = '0.0000000001'; // 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 @@ -46,7 +42,7 @@ function getResults (input) { 'must': { 'query_string': { 'fields': ['channel'], - 'query' : getEscapedQuery(getWashedQuery(input.channel.trim())), + 'query' : getEscapedQuery(input.channel), }, }, }, @@ -64,7 +60,7 @@ function getResults (input) { 'function_score': { 'script_score': { 'script': { - 'source': `${effectiveFactor} * doc['effective_amount'].value`, + 'source': "0.00000001 * doc['effective_amount'].value", }, }, }, @@ -72,7 +68,7 @@ function getResults (input) { const matPhraseName = { // Match search text as phrase - Name 'match_phrase': { 'name': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 10, }, }, @@ -80,14 +76,14 @@ function getResults (input) { const matTextName = { // Match search text - Name 'match': { 'name': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 5, }, }, }; const conTermName = { // Contains search term - Name 'query_string': { - 'query' : `*${escapedQuery}*`, + 'query' : '*' + getEscapedQuery(input.s) + '*', 'fields': [ 'name', ], @@ -102,7 +98,7 @@ function getResults (input) { 'should': [ { // Contains search term in Author, Title, Description 'query_string': { - 'query' : `*${escapedQuery}*`, + 'query' : '*' + getEscapedQuery(input.s) + '*', 'fields': [ 'value.stream.metadata.author', 'value.stream.metadata.title', @@ -114,7 +110,7 @@ function getResults (input) { { // Match search term - Author 'match': { 'value.stream.metadata.author': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 2, }, }, @@ -122,7 +118,7 @@ function getResults (input) { { // Match search text as phrase - Author 'match_phrase': { 'value.stream.metadata.author': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 3, }, }, @@ -130,7 +126,7 @@ function getResults (input) { { // Match search term - Title 'match': { 'value.stream.metadata.title': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 2, }, }, @@ -138,7 +134,7 @@ function getResults (input) { { // Match search text as phrase - Title 'match_phrase': { 'value.stream.metadata.title': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 3, }, }, @@ -146,7 +142,7 @@ function getResults (input) { { // Match search term - Description 'match': { 'value.stream.metadata.description': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 2, }, }, @@ -154,7 +150,7 @@ function getResults (input) { { // Match search text as phrase - Description 'match_phrase': { 'value.stream.metadata.description': { - 'query': washedQuery, + 'query': input.s.trim(), 'boost': 3, }, }, @@ -188,6 +184,7 @@ function getResults (input) { }, }, ], + 'filter': getFilters(input), }, }, size: input.size, @@ -212,46 +209,44 @@ function getRoutingKey () { function getAutoCompleteQuery (query) { return { - bool: { - should: [ - { // Author, Title, Description - nested: { - path : 'value', - query: { - multi_match: { - query : query.s.trim(), - type : 'phrase_prefix', - slop : 5, - max_expansions: 50, - fields : [ - 'value.stream.metadata.author^3', - 'value.stream.metadata.title^5', - 'value.stream.metadata.description^2', - ], - }, - }, - }, - }, - { // Name - multi_match: { - query : query.s.trim(), - type : 'phrase_prefix', - slop : 5, - max_expansions: 50, - fields : [ - 'name^4', - ], - }, - }, + multi_match: { + query : query.s.trim(), + type : 'phrase_prefix', + slop : 5, + max_expansions: 50, + fields : [ + 'name', + 'value.stream.metadata.author', + 'value.stream.metadata.title', + 'value.stream.metadata.description', ], }, }; -}; +} -function getFilter (query) { +function getFilters (input) { // this is the best place for putting things like filtering on the type of content // Perhaps we can add search param that will filter on how people have categorized / tagged their content - + var filters = []; + if (input.nsfw === 'true' || input.nsfw === 'false') { + const nsfwFilter = {'match': {'value.stream.metadata.nsfw': input.nsfw}}; + filters.push(nsfwFilter); + } + if (filters.length > 0) { + const filterQuery = { + 'nested': { + 'path' : 'value', + 'query': { + 'bool': { + 'must': filters, + }, + }, + }, + }; + return filterQuery; + } else { + return []; + } } function getAutoComplete (query) { @@ -260,12 +255,12 @@ function getAutoComplete (query) { routing : getRoutingKey(query), ignore_unavailable: true, // ignore error when date index does not exist body : { - size : query.size || 10, + size : query.size || 5, from : query.from || 0, query: { bool: { must : getAutoCompleteQuery(query), - filter: getFilter(query), + filter: getFilters(query), }, }, }, @@ -287,25 +282,8 @@ function getStatus () { }); } -function getWashedQuery (query) { - // compress multiple white spaces to 1 - query = query.toLowerCase().replace(/ +/g, ' '); - let badWords = [ 'from', 'with', 'not', 'can', 'all', 'are', 'for', 'but', 'and', 'the' ]; - let words = query.split(' '); - let sentence = []; - words.forEach(w => { - if (!badWords.includes(w)) { sentence.push(w) } - }); - query = sentence.join(' '); - - // remove all words < 3 in length - return query.replace(/(\b(\w{1,2})\b(\s|$))/g, ''); -} - function getEscapedQuery (query) { - // https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters - // The reserved characters are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / - let badCharacters = ['+', '-', '=', '&&', '||', '>', '<', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\', '/']; + let badCharacters = ['+', '-', '&&', '||', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '\\']; let escapedQuery = ''; for (var i = 0; i < query.length; i++) { let char1 = query.charAt(i); @@ -358,19 +336,10 @@ class LighthouseControllers { let results = result.hits.hits; let cResults = []; for (let pResult of results) { - var name = pResult._source.name; - if (name.indexOf(ctx.query.s.trim()) > -1 && name.indexOf('http') === -1) { - cResults.push(name); - } + cResults.push(pResult._source.name); if (pResult._source.value && pResult._source.value.stream !== undefined) { - var title = pResult._source.value.stream.metadata.title; - var author = pResult._source.value.stream.metadata.author; - if (title.indexOf(ctx.query.s.trim()) > -1 && title.indexOf('http') === -1) { - cResults.push(title); - } - if (author.indexOf(ctx.query.s.trim()) > -1 && author.indexOf('http') === -1) { - cResults.push(author); - } + cResults.push(pResult._source.value.stream.metadata.title); + cResults.push(pResult._source.value.stream.metadata.author); } }