Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make sure search does not break with malformed query

Uses simple match Elasticsearch query instead of query_string which
supports custom syntax which is not necessary and can result in an
exception sometimes
  • Loading branch information...
commit 0b0d7b07975d5f53c088ebeb6f2e4aeb26fcf061 1 parent 560056c
@saulius saulius authored
View
2  app/services/search_service/base_search.rb
@@ -5,7 +5,7 @@ class BaseSearch
sections: [], chapters: [], headings: [], commodities: []
},
reference_match: {
- sections: [], chapters: [], headings: []
+ sections: [], chapters: [], headings: [], commodities: []
}
}
View
22 app/services/search_service/fuzzy_search/goods_nomenclature_query.rb
@@ -13,9 +13,9 @@ def query(query_opts = {})
{
# match the search phrase
query: {
- query_string: {
+ multi_match: {
query: query_string,
- fields: ["description"]
+ fields: ['description']
}.merge(query_opts)
}
},
@@ -49,6 +49,24 @@ def query(query_opts = {})
}
}
]
+ },
+ {
+ and: [
+ {
+ missing: {
+ field: "validity_start_date",
+ null_value: true,
+ existence: true
+ }
+ },
+ {
+ missing: {
+ field: "validity_end_date",
+ null_value: true,
+ existence: true
+ }
+ }
+ ]
}
]
}
View
7 app/services/search_service/fuzzy_search/reference_query.rb
@@ -9,10 +9,9 @@ def query(*)
query: {
filtered: {
query: {
- query_string: {
- fields: ['title'],
- analyzer: 'snowball',
- query: query_string
+ multi_match: {
+ query: query_string,
+ fields: ['title']
}
},
filter: {
View
48 spec/services/search_service_spec.rb
@@ -339,6 +339,54 @@
end
end
end
+
+ describe 'querying with ambiguous characters' do
+ # Ensure we use match (not query_string query)
+ # query string interprets queries according to Lucene syntax
+ # and we don't need these advanced features
+
+ let(:result) {
+ SearchService.new(t: "!!! [t_e_s_t][",
+ as_of: "1970-01-01")
+ }
+
+ specify 'search does not raise an exception' do
+ expect { result.to_json }.not_to raise_error
+ end
+
+ specify 'search returns empty resilt' do
+ expect(result.to_json).to match_json_expression SearchService::BaseSearch::BLANK_RESULT.merge(type: 'fuzzy_match')
+ end
+ end
+
+ context 'searching for sections' do
+ # Sections do not have validity periods
+ # We have to ensure there is special clause in Elasticsearch
+ # query that takes that into account and they get found
+ let(:title) { "example title" }
+ let!(:section) { create :section, title: title }
+ let(:result) {
+ SearchService.new(t: title,
+ as_of: "1970-01-01")
+ }
+ let(:response_pattern) {
+ {
+ type: 'fuzzy_match',
+ goods_nomenclature_match: {
+ sections: [
+ { "_source" => {
+ "title" => title
+ }.ignore_extra_keys!
+ }.ignore_extra_keys!
+ ].ignore_extra_values!
+ }.ignore_extra_keys!
+ }.ignore_extra_keys!
+ }
+
+ it 'finds relevant sections' do
+ expect(result.to_json).to match_json_expression response_pattern
+ end
+ end
end
context 'reference search' do
Please sign in to comment.
Something went wrong with that request. Please try again.