From 6d51deacbcf326b992bfeee1d5b03ba74daf889b Mon Sep 17 00:00:00 2001 From: Ben Tregenna Date: Thu, 8 Aug 2019 15:51:39 +0100 Subject: [PATCH] Added extra weighting to countries to get them to appear first ticket #74 --- lib/modules/search.rb | 4 +++ lib/modules/search/query.rb | 44 --------------------------------- test/integration/search_test.rb | 28 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/lib/modules/search.rb b/lib/modules/search.rb index f194d3bc7..66226ffef 100644 --- a/lib/modules/search.rb +++ b/lib/modules/search.rb @@ -64,6 +64,10 @@ def query { size: options[:size] || RESULTS_SIZE, from: options[:offset] || offset, + # This line helps countries come first in search, may need tweaking as initial weights are dependent on the relative + # frequency of terms in the countries and PA indices which is hard to anticipate! + indices_boost: [{COUNTRY_INDEX => 3}, {PA_INDEX => 1} ], + query: Search::Query.new(search_term, options).to_h, }.tap( &method(:optional_queries) ) end diff --git a/lib/modules/search/query.rb b/lib/modules/search/query.rb index a7cd9fa31..5fff8af98 100644 --- a/lib/modules/search/query.rb +++ b/lib/modules/search/query.rb @@ -4,48 +4,6 @@ def initialize search_term, options={} @options = options end - def to_h_new - pa_query = {} - country_query = {} - if @term.present? - pa_query["bool"] ||= {} - pa_query["bool"]["must"] = { - "bool" => Search::Matcher.from_params(@term) - } - # ^3 weights the field higher as ISO3 exact matches should beat country matches which should beat region matches - country_query["bool"] ||= { - must: { - multi_match: { - query: @term, - fields: ["name^2", "iso_3^3", "region_for_index"] - } - } - } - end - - - - if @options[:filters].present? - pa_query["bool"] ||= {} - pa_query["bool"]["filter"] = { - "bool" => { - "must" => Search::Filter.from_params(@options[:filters]) - } - } - end - - query = { - bool: { - should: [ - pa_query, - country_query - - ] - } - } - - end - def to_h base_query = {} @@ -56,8 +14,6 @@ def to_h } end - - if @options[:filters].present? base_query["bool"] ||= {} base_query["bool"]["filter"] = { diff --git a/test/integration/search_test.rb b/test/integration/search_test.rb index 67c15a669..fab5cef23 100644 --- a/test/integration/search_test.rb +++ b/test/integration/search_test.rb @@ -345,5 +345,33 @@ def assert_aggregation expected, name, value, aggs assert_equal 1, search.results.count end + test 'search should put countries first' do + region = FactoryGirl.create(:region, id: 987, name: 'Europe') + country = FactoryGirl.create(:country, id: 123, iso_3: 'BEL', name: 'Belgium', region: region) + pa1 = FactoryGirl.create(:protected_area, name: "Belgium Forest", wdpa_id: 1, countries: [country]) + pa2 = FactoryGirl.create(:protected_area, name: "Forests of Belgium", wdpa_id: 2, countries: [country]) + + assert_index 1, 2 + search = Search.search 'belgium', {} + assert_equal 3, search.results.count + assert_equal 'Belgium', search.results[0].name + end + + test 'search should put france countries first' do + region = FactoryGirl.create(:region, id: 987, name: 'Europe') + france = FactoryGirl.create(:country, id: 123, iso_3: 'FRA', name: 'France', region: region) + usa = FactoryGirl.create(:country, id: 124, iso_3: 'USA', name: 'United States of America', region: region) + pa1 = FactoryGirl.create(:protected_area, name: "Oise-Pays de France", wdpa_id: 1, countries: [france]) + pa2 = FactoryGirl.create(:protected_area, name: "Frances Mesa", wdpa_id: 2, countries: [usa]) + (1..20).each do |ii| + FactoryGirl.create(:protected_area, name: "Area #{ii}", wdpa_id: 10+ii, countries:[usa]) + end + + assert_index 2, 22 + search = Search.search 'france', {} + assert_equal 3, search.results.count + assert_equal 'France', search.results[0].name + end + end