From c32f8e9367d8856118e43485f96e1b691d542c89 Mon Sep 17 00:00:00 2001 From: Mark Beamer Jr Date: Wed, 14 Mar 2018 23:18:15 -0400 Subject: [PATCH] I modified the query to be more complex so that relevancy is considered. Features of the query are below in order of importance(weight): 1) Phrase Match on Name 2) Term Match on Name 3) Contains Term on Name 4) Phrase Match on Author,Title,Description 5) Term Match on Author,Title,Description 6) Contains Term on Author,Title,Description --- server/controllers/lighthouse.js | 81 ++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/server/controllers/lighthouse.js b/server/controllers/lighthouse.js index d31a32d..4e0020f 100644 --- a/server/controllers/lighthouse.js +++ b/server/controllers/lighthouse.js @@ -28,33 +28,94 @@ function getResults (input) { 'query': { '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() + '*', + 'query': input.s.trim(), 'boost': 5, }, }, }, + { // Contains search term - Name + 'query_string': { + 'query' : '*' + input.s.trim() + '*', + 'fields': [ + 'name', + ], + 'boost': 3, + }, + }, { - 'nested': { // Need nested query because value is a nested object on the elastic document + 'nested': { 'path' : 'value', 'query': { 'bool': { 'should': [ - { - 'match': { - 'value.stream.metadata.title': '*' + input.s.trim() + '*', + { // 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': '*' + input.s.trim() + '*', + '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.description': '*' + input.s.trim() + '*', + '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, + }, }, }, ],