【问题标题】:Spell check Ngram for elastic Search not working with rails用于弹性搜索的拼写检查 Ngram 不适用于 rails
【发布时间】:2017-07-31 06:00:40
【问题描述】:

我在我的模型中使用了拼写检查,这样如果用户输入“Rental”之类的数据,那么它应该将正确的数据提取为“Rental”

document.rb 代码

require 'elasticsearch/model'

class Document < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  belongs_to :user

  Document.import force: true


  def self.search(query)
  __elasticsearch__.search({
      query: {
        multi_match: {
          query: query,
          fields: ['name^10', 'service']
      }
    }
    })
  end


  settings index: { 
    "number_of_shards": 1, 
    analysis: {
      analyzer: {
        edge_ngram_analyzer: { type: "custom", tokenizer: "standard", filter: 
          ["lowercase", "edge_ngram_filter", "stop", "kstem" ] },
            }
        },
        filter: {
                  edge_ngram_filter: { type: "edgeNGram", min_gram: "3", max_gram: 
                  "20" } 
      }
    } do
    mapping do
      indexes :name, type: "string", analyzer: "edge_ngram_analyzer"
      indexes :service, type: "string", analyzer: "edge_ngram_analyzer"
    end 
  end
end

搜索控制器代码:

def search
  if params[:query].nil?
    @documents = []
  else
    @documents = Document.search params[:query]
  end
end

但是,如果我输入 Rentaal 或任何拼写错误的单词,它不会显示任何内容。 在我的控制台中

     @documents.results.to_a 

给出一个空数组。

我在这里做错了什么?如果需要更多数据,请告诉我。

【问题讨论】:

    标签: ruby-on-rails elasticsearch elasticsearch-rails


    【解决方案1】:

    尝试在您的multi_match 查询中添加fuzziness

    {
          "query": {
            "multi_match": {
              "query": "Rentaal",
              "fields": ["name^10", "service"],
              "fuzziness": "AUTO"
          }
        }
    }
    

    说明

    Kstem 过滤器用于将单词简化为它们的根形式,但它不能像您预期的那样工作 - 它可以正确处理像 RentaRent 这样的短语,但不会处理您提供的拼写错误。

    您可以检查 stemming 如何使用以下查询:

    curl -X POST \
      'http://localhost:9200/my_index/_analyze?pretty=true' \
      -d '{
      "analyzer" : "edge_ngram_analyzer",
      "text" : ["rentaal"]
    }'
    

    结果我看到了:

    {
        "tokens": [
            {
                "token": "ren"
            },
            {
                "token": "rent"
            },
            {
                "token": "renta"
            },
            {
                "token": "rentaa"
            },
            {
                "token": "rentaal"
            }
        ]
    }
    

    因此,应用模糊性可以更好地处理典型的拼写错误。

    【讨论】:

    • 很抱歉,这么晚了,但我没有太多时间早点 - 我更新了我的答案,解释了为什么 kstem 在你的情况下还不够,将来可能会有用。
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多