我有一个名为偏差的属性的“文档”(活动记录)。该属性具有“Bin X”“Bin $”“Bin q”“Bin %”等值。
我正在尝试使用轮胎/弹性搜索来搜索属性。我正在使用空白分析器来索引偏差属性。这是我创建索引的代码:
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
ANDX
的记录。但是,搜索类似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>