Elastic Search "Did you mean" for auto correction of words implementation not working with rails

899 Views Asked by At

I am trying to implement full search text engine for my rails app using elastic search for a document class. It should have an auto correction for misspelled words.

This is my 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

It helps me search data. However, the did you mean phrase is not working here. What can I do further to improve this code?I am using elastic search for the very first time.

0

There are 0 best solutions below