Skip to content
代码片段 群组 项目
未验证 提交 3c123477 编辑于 作者: Siddharth Dungarwal's avatar Siddharth Dungarwal 提交者: GitLab
浏览文件

Merge branch 'cherry-pick-df002158' into '17-9-stable-ee'

[Backport 17.9] Fix frozen array error with custom analyzers

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/184083



Merged-by: default avatarSiddharth Dungarwal <sdungarwal@gitlab.com>
Approved-by: default avatarSiddharth Dungarwal <sdungarwal@gitlab.com>
Approved-by: default avatarJohn McDonnell <jmcdonnell@gitlab.com>
Co-authored-by: default avatarTerri Chu <tchu@gitlab.com>
No related branches found
No related tags found
1 合并请求!3051Prepare 17.9.3 release for gitlab-jh
...@@ -39,8 +39,9 @@ def by_full_text(query:, options:) ...@@ -39,8 +39,9 @@ def by_full_text(query:, options:)
end end
def by_multi_match_query(fields:, query:, options:) def by_multi_match_query(fields:, query:, options:)
fields = ::Elastic::Latest::CustomLanguageAnalyzers.add_custom_analyzers_fields(fields) query_fields = fields.dup
fields = remove_fields_boost(fields) if options[:count_only] query_fields = ::Elastic::Latest::CustomLanguageAnalyzers.add_custom_analyzers_fields(query_fields)
query_fields = remove_fields_boost(query_fields) if options[:count_only]
bool_expr = ::Search::Elastic::BoolExpr.new bool_expr = ::Search::Elastic::BoolExpr.new
...@@ -57,8 +58,8 @@ def by_multi_match_query(fields:, query:, options:) ...@@ -57,8 +58,8 @@ def by_multi_match_query(fields:, query:, options:)
end end
multi_match_bool = ::Search::Elastic::BoolExpr.new multi_match_bool = ::Search::Elastic::BoolExpr.new
multi_match_bool.should << multi_match_query(fields, query, options.merge(operator: :and)) multi_match_bool.should << multi_match_query(query_fields, query, options.merge(operator: :and))
multi_match_bool.should << multi_match_phrase_query(fields, query, options) multi_match_bool.should << multi_match_phrase_query(query_fields, query, options)
multi_match_bool.minimum_should_match = 1 multi_match_bool.minimum_should_match = 1
if options[:count_only] if options[:count_only]
...@@ -75,14 +76,15 @@ def by_multi_match_query(fields:, query:, options:) ...@@ -75,14 +76,15 @@ def by_multi_match_query(fields:, query:, options:)
query_hash = { query: { bool: bool_expr } } query_hash = { query: { bool: bool_expr } }
query_hash[:track_scores] = true unless query.present? query_hash[:track_scores] = true unless query.present?
query_hash[:highlight] = apply_highlight(fields) unless options[:count_only] query_hash[:highlight] = apply_highlight(query_fields) unless options[:count_only]
query_hash query_hash
end end
def by_simple_query_string(fields:, query:, options:) def by_simple_query_string(fields:, query:, options:)
fields = ::Elastic::Latest::CustomLanguageAnalyzers.add_custom_analyzers_fields(fields) query_fields = fields.dup
fields = remove_fields_boost(fields) if options[:count_only] query_fields = ::Elastic::Latest::CustomLanguageAnalyzers.add_custom_analyzers_fields(query_fields)
query_fields = remove_fields_boost(query_fields) if options[:count_only]
bool_expr = ::Search::Elastic::BoolExpr.new bool_expr = ::Search::Elastic::BoolExpr.new
if query.present? if query.present?
...@@ -98,11 +100,11 @@ def by_simple_query_string(fields:, query:, options:) ...@@ -98,11 +100,11 @@ def by_simple_query_string(fields:, query:, options:)
end end
if options[:count_only] if options[:count_only]
bool_expr.filter << simple_query_string(fields, query, options) bool_expr.filter << simple_query_string(query_fields, query, options)
elsif options[:keyword_match_clause] == :should elsif options[:keyword_match_clause] == :should
bool_expr.should << simple_query_string(fields, query, options) bool_expr.should << simple_query_string(query_fields, query, options)
else else
bool_expr.must << simple_query_string(fields, query, options) bool_expr.must << simple_query_string(query_fields, query, options)
end end
else else
bool_expr.must = { match_all: {} } bool_expr.must = { match_all: {} }
...@@ -111,7 +113,7 @@ def by_simple_query_string(fields:, query:, options:) ...@@ -111,7 +113,7 @@ def by_simple_query_string(fields:, query:, options:)
query_hash = { query: { bool: bool_expr } } query_hash = { query: { bool: bool_expr } }
query_hash[:track_scores] = true unless query.present? query_hash[:track_scores] = true unless query.present?
query_hash[:highlight] = apply_highlight(fields) unless options[:count_only] query_hash[:highlight] = apply_highlight(query_fields) unless options[:count_only]
query_hash query_hash
end end
......
...@@ -83,6 +83,20 @@ ...@@ -83,6 +83,20 @@
expect(by_simple_query_string[:query][:bool][:must]).to eql(expected_must) expect(by_simple_query_string[:query][:bool][:must]).to eql(expected_must)
end end
context 'when fields is a frozen array' do
let(:fields) { %w[iid^3 title^2 description].freeze }
it 'applies custom analyzer fields' do
expected_must = [
{ simple_query_string: { _name: 'my_type:match:search_terms',
fields: %w[iid^3 title^2 description title.smartcn description.smartcn],
query: 'foo bar', lenient: true, default_operator: :and } }
]
expect(by_simple_query_string[:query][:bool][:must]).to eql(expected_must)
end
end
end end
it 'applies highlight in query' do it 'applies highlight in query' do
...@@ -195,6 +209,26 @@ ...@@ -195,6 +209,26 @@
expect(by_multi_match_query[:query][:bool][:must]).to eql(expected_must) expect(by_multi_match_query[:query][:bool][:must]).to eql(expected_must)
end end
context 'when fields is a frozen array' do
let(:fields) { %w[iid^3 title^2 description].freeze }
it 'applies custom analyzer fields to multi_match_query' do
expected_must = [{ bool: {
should: [
{ multi_match: { _name: 'my_type:multi_match:and:search_terms',
fields: %w[iid^3 title^2 description title.smartcn description.smartcn],
query: 'foo bar', operator: :and, lenient: true } },
{ multi_match: { _name: 'my_type:multi_match_phrase:search_terms',
type: :phrase, fields: %w[iid^3 title^2 description title.smartcn description.smartcn],
query: 'foo bar', lenient: true } }
],
minimum_should_match: 1
} }]
expect(by_multi_match_query[:query][:bool][:must]).to eql(expected_must)
end
end
end end
it 'applies highlight in query' do it 'applies highlight in query' do
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册