12

偏差と呼ばれる属性を持つ「ドキュメント」(アクティブレコード) があります。属性には、「Bin X」「Bin $」「Bin q」「Bin %」などの値があります。

Tire/elasticsearch を使用して属性を検索しようとしています。空白アナライザーを使用して偏差属性のインデックスを作成しています。インデックスを作成するための私のコードは次のとおりです。

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

クエリ文字列に特殊文字が含まれていない場合、検索は正常に機能しているようです。たとえば、 ANDBin Xという単語が含まれるレコードのみを返します。ただし、orのようなものを検索すると、記号をほとんど無視した単語を含むすべての結果が表示されます (記号を含む結果は、含まない検索で上位に表示されます)。BinXBin $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>
4

1 に答える 1