1

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

这是我的文档.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

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

4

0 回答 0