【问题标题】:Elasticsearch-rails results accuracy not as expectedElasticsearch-rails 结果准确性不如预期
【发布时间】:2017-01-09 11:05:29
【问题描述】:

我正在使用 elasticsearch-rails 和 elasticsearch-model gem 在我的 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恤都应该匹配。任何进一步研究的链接也很有帮助。谢谢

【问题讨论】:

    标签: ruby-on-rails elasticsearch-rails


    【解决方案1】:

    您要查找的关键字是模糊性。

    请参阅此博客文章,例如: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 表示允许的最大 Levenshtein 距离,它 接受 0 到 2 之间的整数(其中 2 表示最模糊的搜索 可能)或将生成编辑距离的字符串“AUTO” 基于查询中术语的字符长度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-15
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2023-02-07
      相关资源
      最近更新 更多