Elasticsearch and Rails: using ngram to find part of a word

I am trying to use Elasticsearch-Gem in my project. As far as I understand: by now there is no longer need for Tire-Gem, or am I mistaken?

In my project, I have a search (obivously) that currently applies to a single model. Now I try to avoid wildcards, as they do not scale well, but I cannot get ngram parsers to work correctly. If I search for whole words, the search still works, but not for parts of it.

class Pictures < ActiveRecord::Base

  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings  :analysis => {
          :analyzer => {
            :my_index_analyzer => {
                :tokenizer => "keyword",
                :filter => ["lowercase", "substring"]
            },
            :my_search_analyzer => {
              :tokenizer => "keyword",
              :filter => ["lowercase", "substring"]
            }
          },
          :filter => {
            :substring => {
              :type => "nGram",
              :min_gram => 2,
              :max_gram => 50
            }
          }
    } do  
mapping do
  indexes :title, 
  :properties => {
    :type => "string",
    :index_analyzer => 'my_index_analyzer',
    :search_analyzer => "my_search_analyzer"
  }

Maybe someone can give me a hint in the right direction.

+4
source share
2

. , .

, . / db/folder rake .

https://gist.github.com/geordee/9313f4867d61ce340a08

def as_indexed_json(options={})
  self.as_json(only: [:id, :name, :description, :price])
end
+1

, edgeNGram (, nGram, ) :

{
   "en_suggestions": {
      "settings": {
         "index": {
            "analysis": {
               "filter": {
                  "tpNGramFilter": {
                     "min_gram": "4",
                     "type": "edgeNGram",
                     "max_gram": "50"
                  }
               },
               "analyzer": {
                  "tpNGramAnalyzer": {
                     "type": "custom",
                     "filter": [
                        "tpNGramFilter"
                     ],
                     "tokenizer": "lowercase"
                  }
               }
            }
         }
      }
   }
}

:

{
   "en_suggestions": {
      "mappings": {
         "suggest": {
            "properties": {
               "proposal": {
                  "type": "string",
                  "analyzer": "tpNGramAnalyzer"
               }
            }
         }
      }
   }
}
0

Source: https://habr.com/ru/post/1545759/


All Articles