Skip to content

Commit

Permalink
Added extra weighting to countries to get them to appear first
Browse files Browse the repository at this point in the history
ticket #74
  • Loading branch information
Ben Tregenna authored and Ben Tregenna committed Aug 8, 2019
1 parent d7666c5 commit 6d51dea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
4 changes: 4 additions & 0 deletions lib/modules/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 0 additions & 44 deletions lib/modules/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -56,8 +14,6 @@ def to_h
}
end



if @options[:filters].present?
base_query["bool"] ||= {}
base_query["bool"]["filter"] = {
Expand Down
28 changes: 28 additions & 0 deletions test/integration/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6d51dea

Please sign in to comment.