1
0
Fork 0
forked from gitea/nas

Add: #452 全文検索で、ダブルクオートがない場合でも単語検索を標準とするオプション (#478)

This commit is contained in:
KMY(雪あすか) 2024-01-18 08:39:33 +09:00 committed by GitHub
parent 77843c0bdc
commit d5940f00d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 7 deletions

View file

@ -242,17 +242,20 @@ class SearchQueryTransformer < Parslet::Transform
class TermClause
attr_reader :operator, :term
def initialize(operator, term)
def initialize(operator, term, current_account: nil)
@operator = Operator.symbol(operator)
@term = term
@account = current_account
end
def to_query
if @term.start_with?('#')
{ match: { tags: { query: @term, operator: 'and' } } }
else
elsif @account&.user&.setting_reverse_search_quote
# Memo for checking when manually merge
# { multi_match: { type: 'most_fields', query: @term, fields: ['text', 'text.stemmed'], operator: 'and' } }
{ match_phrase: { text: { query: @term } } }
else
{ multi_match: { type: 'most_fields', query: @term, fields: ['text', 'text.stemmed'], operator: 'and' } }
end
end
@ -261,15 +264,20 @@ class SearchQueryTransformer < Parslet::Transform
class PhraseClause
attr_reader :operator, :phrase
def initialize(operator, phrase)
def initialize(operator, phrase, current_account: nil)
@operator = Operator.symbol(operator)
@phrase = phrase
@account = current_account
end
def to_query
# Memo for checking when manually merge
# { match_phrase: { text: { query: @phrase } } }
{ match_phrase: { text: { query: @phrase } } }
if @account&.user&.setting_reverse_search_quote
{ multi_match: { type: 'most_fields', query: @phrase, fields: ['text', 'text.stemmed'], operator: 'and' } }
else
{ match_phrase: { text: { query: @phrase } } }
end
end
end
@ -411,11 +419,11 @@ class SearchQueryTransformer < Parslet::Transform
if clause[:prefix] && SUPPORTED_PREFIXES.include?(prefix)
PrefixClause.new(prefix, operator, term, current_account: current_account)
elsif clause[:prefix]
TermClause.new(operator, "#{prefix} #{term}")
TermClause.new(operator, "#{prefix} #{term}", current_account: current_account)
elsif clause[:term]
TermClause.new(operator, term)
TermClause.new(operator, term, current_account: current_account)
elsif clause[:phrase]
PhraseClause.new(operator, term)
PhraseClause.new(operator, term, current_account: current_account)
else
raise "Unexpected clause type: #{clause}"
end