2017-01-09 48 views
0

我正在使用elasticsearch-rails和elasticsearch-model gem在我的rails應用程序中搜索單詞。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

您正在尋找的關鍵字是模糊性。

參見這個博客帖子例如: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" 

模糊性表示的Levenshtein允許的最大距離,它 接受0和2之間的整數(其中,2指fuzziest搜索 可能的)或字符串「AUTO」,它將基於查詢中術語的字符長度生成編輯距離 。