弹性搜索“您的意思是”自动更正单词实现不使用rails

时间:2017-07-28 06:25:18

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

我正在尝试使用弹性搜索文档类为我的rails应用程序实现完整的搜索文本引擎。它应该有拼写错误的单词的自动更正。

这是我的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: {
      string_lowercase: { tokenizer: 'keyword', filter: %w(lowercase 
      ascii_folding) },
      did_you_mean: { filter: ['lowercase'], char_filter: ['html_strip'],
      type: 'custom', tokenzier: 'standard'},
      autocomplete: {filter: ["lowercase", "autocompleteFilter"], char_filter: 
      [ "html_strip"], type: "custom", tokenizer: "standard"},
      default: {filter: [ "lowercase", "stopwords", "stemmer"], char_filter: [
      "html_strip"], type: "custom",tokenizer: "standard"}
        }
       },
      filter: { ascii_folding: { type: 'asciifolding', preserve_original: true 
      }, 
                stemmer: {type: 'stemmer', language: 'english'},
                autocompleteFilter: { max_shingle_size: 5, min_shingle_size:2, 
                type: 'shingle'}, 
                stopwords: {type: 'stop', stopwords: ['_english_'] }
              }
     } do
         mapping do{
      document: {
        properties: {
          autocomplete: {
            type: "string",
            analyzer: "autocomplete"
          },
          name: {
            type: "string",
            copy_to: ["did_you_mean","autocomplete"]
          },
          did_you_mean: {
            type: "string",
            analyzer: "didYouMean"
          },
          service: {
            type: "string",
            copy_to: ["autocomplete", "did_you_mean"]
          }
        }
      }
    } 
   end
   end

它可以帮助我搜索数据。但是,你的意思是短语不在这里工作。 我还能做些什么来改进这段代码?我第一次使用弹性搜索。

0 个答案:

没有答案