简体   繁体   English

Elasticsearch-rails结果准确性不如预期

[英]Elasticsearch-rails results accuracy not as expected

I am using elasticsearch-rails and elasticsearch-model gem for searching words in my rails app. 我正在使用elasticsearch-rails和elasticsearch-model gem在Rails应用程序中搜索单词。

Here is my model where I want to search to take place 这是我要搜索的模型

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

My questions are how do I do search word? 我的问题是如何搜索单词? tshirt, T-shirt, Tshirts should all match. T恤,T恤,T恤都应该匹配。 Any links for further research is also helpful. 任何进一步研究的链接也是有帮助的。 Thank you 谢谢

The keyword you are looking for is fuzziness. 您要寻找的关键字是模糊性。

See this blog post for example: http://dev.mikamai.com/post/85901585484/elasticsearch-on-rails-a-primer 例如,请参阅此博客文章: 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"

fuzziness represents the maximum allowed Levenshtein distance, it accepts an integer between 0 and 2 (where 2 means the fuzziest search possible) or the string “AUTO” which will generate an edit distance based on the charachers length of the terms in the query. 模糊性表示允许的最大Levenshtein距离,它接受0到2之间的整数(其中2表示可能最模糊的搜索)或字符串“ AUTO”,该字符串将根据查询中术语的字符长度生成编辑距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM