Permalink
Browse files

Merge pull request #144 from alphagov/65968390-narrow-search-synonym-…

…results

Make sure we match all words in synonym search
  • Loading branch information...
2 parents 842a15d + 1a47e0c commit c506b0a1e0be7002f61f0298219a90d6c06f5da1 @jabley jabley committed Feb 20, 2014
Showing with 70 additions and 30 deletions.
  1. +2 −1 app/services/search_service/fuzzy_search/reference_query.rb
  2. +68 −29 spec/services/search_service_spec.rb
View
3 app/services/search_service/fuzzy_search/reference_query.rb
@@ -11,7 +11,8 @@ def query(*)
query: {
multi_match: {
query: query_string,
- fields: ['title']
+ fields: ['title'],
+ operator: 'and' # all terms must be present
}
},
filter: {
View
97 spec/services/search_service_spec.rb
@@ -390,42 +390,81 @@
end
context 'reference search' do
- let!(:heading) { create :heading, :with_description,
- goods_nomenclature_item_id: "2851000000",
- validity_start_date: Date.new(1972,1,1),
- validity_end_date: Date.new(2006,12,31),
- description: 'Test' }
- let!(:search_reference) { create :search_reference,
- referenced: heading,
- title: 'water' }
-
- let(:heading_pattern) {
- {
- type: 'fuzzy_match',
- reference_match: {
- headings: [
- {
- "_source" => {
- reference: { "goods_nomenclature_item_id"=>"2851000000" }.ignore_extra_keys!
+ describe 'validity period function' do
+ let!(:heading) { create :heading, :with_description,
+ goods_nomenclature_item_id: "2851000000",
+ validity_start_date: Date.new(1972,1,1),
+ validity_end_date: Date.new(2006,12,31),
+ description: 'Test' }
+ let!(:search_reference) { create :search_reference,
+ referenced: heading,
+ title: 'water' }
+
+ let(:heading_pattern) {
+ {
+ type: 'fuzzy_match',
+ reference_match: {
+ headings: [
+ {
+ "_source" => {
+ reference: { "goods_nomenclature_item_id"=>"2851000000" }.ignore_extra_keys!
+ }.ignore_extra_keys!
}.ignore_extra_keys!
- }.ignore_extra_keys!
- ].ignore_extra_values!
+ ].ignore_extra_values!
+ }.ignore_extra_keys!
}.ignore_extra_keys!
- }.ignore_extra_keys!
- }
+ }
+
+ it 'returns goods code if search date falls within validity period' do
+ @result = SearchService.new(t: "water",
+ as_of: "2005-01-01").to_json
- it 'returns goods code if search date falls within validity period' do
- @result = SearchService.new(t: "water",
- as_of: "2005-01-01").to_json
+ @result.should match_json_expression heading_pattern
+ end
- @result.should match_json_expression heading_pattern
+ it 'does not return goods code if search date does not fall within validity period' do
+ @result = SearchService.new(t: "water",
+ as_of: "2007-01-01").to_json
+
+ @result.should_not match_json_expression heading_pattern
+ end
end
- it 'does not return goods code if search date does not fall within validity period' do
- @result = SearchService.new(t: "water",
- as_of: "2007-01-01").to_json
+ describe 'reference matching for multi term searches' do
+ let!(:heading1) { create :heading, :with_description,
+ goods_nomenclature_item_id: "2851000000",
+ description: 'Test 1' }
+ let!(:search_reference1) { create :search_reference,
+ referenced: heading1,
+ title: 'acid oil' }
+ let!(:heading2) { create :heading, :with_description,
+ goods_nomenclature_item_id: "2920000000",
+ description: 'Test 2' }
+ let!(:search_reference2) { create :search_reference,
+ referenced: heading2,
+ title: 'other kind of oil' } # not 'acid oil'
+
+ let(:heading_pattern) {
+ {
+ type: 'fuzzy_match',
+ reference_match: {
+ headings: [
+ {
+ "_source" => {
+ reference: { "goods_nomenclature_item_id"=> heading1.goods_nomenclature_item_id }.ignore_extra_keys!
+ }.ignore_extra_keys!
+ }.ignore_extra_keys!
+ ]
+ }.ignore_extra_keys!
+ }.ignore_extra_keys!
+ }
- @result.should_not match_json_expression heading_pattern
+ it 'only matches exact phrases' do
+ @result = SearchService.new(t: 'acid oil',
+ as_of: Date.today).to_json
+
+ @result.should match_json_expression heading_pattern
+ end
end
end
end

0 comments on commit c506b0a

Please sign in to comment.