2017-01-09 16 views
0

私はelasticsearch-railsとelasticsearch-model gemを使用して、私のレールアプリで単語を検索しています。ここでElasticsearch-railsの結果が正確でない可能性があります

が、私は場所を取るために検索したい私のモデル

require 'elasticsearch/model' 

    class Article < ActiveRecord::Base 
     include Elasticsearch::Model 
     include Elasticsearch::Model::Callbacks 

     settings index: { number_of_shards: 1 } do 
     mappings dynamic: 'false' do 
      indexes :title, analyzer: 'english', index_options: 'offsets' 
      indexes :text, analyzer: 'english' 
     end 
     end 

     def self.search(query) 
     __elasticsearch__.search(
      { 
      query: { 
       multi_match: { 
       query: query, 
       fields: ['title^10', 'text'] 
       } 
      }, 
      highlight: { 
       pre_tags: ['<em>'], 
       post_tags: ['</em>'], 
       fields: { 
       title: {}, 
       text: {} 
       } 
      } 
      } 
     ) 
     end 
    end 

    # Delete the previous articles index in Elasticsearch 
    Article.__elasticsearch__.client.indices.delete index: Article.index_name rescue nil 

    # Create the new index with the new mapping 
    Article.__elasticsearch__.client.indices.create \ 
     index: Article.index_name, 
     body: { settings: Article.settings.to_hash, mappings: Article.mappings.to_hash } 

    # Index all article records from the DB to Elasticsearch 
    Article.import 

    #Article.import force: true 

である私の質問は、私は言葉を検索しますかどのようにしていますか? Tシャツ、Tシャツ、Tシャツはすべて一致する必要があります。さらなる研究のためのリンクも役立ちます。ありがとうございます

答えて

0

あなたが探しているキーワードはあいまいです。または(2 fuzziest検索 可能を意味する)、それ は0と2の間の整数を受け付け、http://dev.mikamai.com/post/85901585484/elasticsearch-on-rails-a-primer

records = Article.search(query: {match: {_all: {query: 'wold', fuzziness: 2}}}).records 
records.first.title # => "Hello World" 

が曖昧が最大許容レーベンシュタイン距離を表す:

例えば、このブログ記事を参照してください文字列「AUTO」は、クエリ内の用語の文字の長さに基づいて編集距離 を生成します。

関連する問題