我有一个“文档”(活动记录),它有一个名为deviations的属性,该属性的值类似于“Bin X”“Bin $”“Bin q”“Bin %”等。
我尝试使用tire/elasticsearch来搜索属性,我使用空格分析器来索引deviation属性,下面是创建索引的代码:
settings :analysis => {
:filter => {
:ngram_filter => {
:type => "nGram",
:min_gram => 2,
:max_gram => 255
},
:deviation_filter => {
:type => "word_delimiter",
:type_table => ['$ => ALPHA']
}
},
:analyzer => {
:ngram_analyzer => {
:type => "custom",
:tokenizer => "standard",
:filter => ["lowercase", "ngram_filter"]
},
:deviation_analyzer => {
:type => "custom",
:tokenizer => "whitespace",
:filter => ["lowercase"]
}
}
} do
mapping do
indexes :id, :type => 'integer'
[:equipment, :step, :recipe, :details, :description].each do |attribute|
indexes attribute, :type => 'string', :analyzer => 'ngram_analyzer'
end
indexes :deviation, :analyzer => 'whitespace'
end
end
当查询字符串不包含特殊字符时,搜索似乎可以正常工作。例如,Bin X
将仅返回包含单词Bin
AND X
的记录。但是,搜索类似Bin $
或Bin %
的内容将显示包含单词Bin
的所有结果,几乎忽略符号(带有符号的结果在搜索中显示的位置高于不带符号的结果)。
下面是我创建的搜索方法
def self.search(params)
tire.search(load: true) do
query { string "#{params[:term].downcase}:#{params[:query]}", default_operator: "AND" }
size 1000
end
end
下面是我构建搜索表单的方法:
<div>
<%= form_tag issues_path, :class=> "formtastic issue", method: :get do %>
<fieldset class="inputs">
<ol>
<li class="string input medium search query optional stringish inline">
<% opts = ["Description", "Detail","Deviation","Equipment","Recipe", "Step"] %>
<%= select_tag :term, options_for_select(opts, params[:term]) %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil, class: "btn" %>
</li>
</ol>
</fieldset>
<% end %>
</div>
1条答案
按热度按时间8dtrkrch1#
你可以净化你的查询字符串,这里有一个净化器,它可以处理我试过的所有查询字符串: