Elasticsearch does not seem to be working as expec

2019-09-11 09:29发布

I am using elasticsearch and globalize gems for full text searching and what I expect is that I can search for supplier name, localised description using czech/english analyzer.

Example: Supplier Name: "Bonami.cz" Supplier Description_CZ: "Test description in czech."

It works when I search for "Bonami.cz", but it does not work (0 results) when I search for:

  • "Bonami" (part of the word)
  • "test" (description)

Based on documentation, the below methods should work, but apparently I have missed something. I verified the indexes and data is in ElasticSearch.

Also do I need to install somehow the czech/english analyzer before using it in the model?

require 'elasticsearch/model'
require 'activerecord-import'

class Supplier < ActiveRecord::Base
    after_commit lambda { __elasticsearch__.index_document  },  on: :create
    after_commit lambda { __elasticsearch__.update_document },  on: :update

    translates :description, :fallbacks_for_empty_translations => true
    accepts_nested_attributes_for :translations

    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    include Elasticsearch::Model::Globalize::MultipleFields

        mapping do
          indexes :id,             type: 'integer'
          indexes :name,           analyzer: 'czech'
          indexes :description_ma, analyzer: 'czech'
          indexes :description_cs, analyzer: 'czech'
          indexes :description_en, analyzer: 'english'
       end

       def as_indexed_json(options={})
          { id: id,
            name: name,
            description_ma: description_ma,
            description_cs: description_cs,
            description_en: description_en    
         }
       end

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

Any idea, what is wrong? Thanks, Miroslav

UPDATE 1 I inspired with the solution from Rails 4, elasticsearch-rails, but when I try to search now, for any word I always get zero results.

settings index: {
    number_of_shards: 1,
    analysis: {
      filter: {
        trigrams_filter: {
          type: 'ngram',
          min_gram: 2,
          max_gram: 10
        },
        content_filter: {
          type: 'ngram',
          min_gram: 4,
          max_gram: 20
        }
      },
      analyzer: {
        index_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: ['lowercase', 'trigrams_filter']
        },
        search_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'whitespace',
          filter: ['lowercase']
        },
        english: {
          tokenizer: 'standard',
          filter: ['standard', 'lowercase', 'content_filter']
        },
        czech: {
          tokenizer: 'standard',
          filter: ['standard','lowercase','content_filter']
        }
      }
    }
    } do
    mappings dynamic: 'false' do
      indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
      indexes :description_en, index_analyzer: 'english', search_analyzer: 'english'
      indexes :description_ma, index_analyzer: 'czech', search_analyzer: 'czech'
      indexes :description_cs, index_analyzer: 'czech', search_analyzer: 'czech'
    end
end

  def as_indexed_json(options={})
    { id: id,
      name: name,
      description_ma: description_ma,
      description_cs: description_cs,
      description_en: description_en    
    }
  end

   def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^10', 'description_ma', 'description_cs', 'description_en']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
            fields: {
              name: {},
              description_ma: {},
              description_cs: {},
              description_en: {}
            }
        }
      }
    )
  end

This is what I see when I open elastic search URL for the given model:

{"suppliers":{"aliases":{},"mappings":{"supplier":    
{"dynamic":"false","properties":{"description_cs":      
{"type":"string","analyzer":"czech"},"description_en": 
{"type":"string","analyzer":"english"},"description_ma":
{"type":"string","analyzer":"czech"},"name":
{"type":"string","index_analyzer":"index_trigrams_analyzer","search_analyzer":"search_trigrams_analyzer"}}}},"settings":{"index":
{"creation_date":"1445797508427","analysis":{"filter":   
{"trigrams_filter":
{"type":"ngram","min_gram":"2","max_gram":"10"},"content_filter":
{"type":"ngram","min_gram":"4","max_gram":"20"}},"analyzer":{"english":
{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"},"index_trigrams_analyzer":{"type":"custom","filter":["lowercase","trigrams_filter"],"tokenizer":"standard"},"search_trigrams_analyzer":{"type":"custom","filter":["lowercase"],"tokenizer":"whitespace"},"czech":{"filter":["standard","lowercase","content_filter"],"tokenizer":"standard"}}},"number_of_shards":"1","number_of_replicas":"1","version": 
{"created":"1060099"},"uuid":"wX9kf3OQSva24Iwk7sZ8AQ"}},"warmers":{}}}

UPDATE 2

Two steps were missing to have it working as expected => 1. Re-import model data 2. Typo in names of description fields (instead of description_ma/en/cs, I had to use ma/cs/en_description.

     settings index: {
    number_of_shards: 1,
     analysis: {
      filter: {
        trigrams_filter: {
          type: 'ngram',
          min_gram: 2,
          max_gram: 10
        },
        content_filter: {
          type: 'ngram',
          min_gram: 4,
          max_gram: 20
        }
      },
      analyzer: {
        index_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'standard',
          filter: ['lowercase', 'trigrams_filter']
        },
        search_trigrams_analyzer: {
          type: 'custom',
          tokenizer: 'whitespace',
          filter: ['lowercase']
        },
        english: {
          tokenizer: 'standard',
          filter: ['standard', 'lowercase', 'content_filter']
        },
        czech: {
          tokenizer: 'standard',
          filter: ['standard','lowercase','content_filter' ]
        }
      }
    }
    } do
    mappings dynamic: 'false' do
         indexes :name, index_analyzer: 'index_trigrams_analyzer', search_analyzer: 'search_trigrams_analyzer'
      indexes :en_description, index_analyzer: 'english', search_analyzer: 'english'
      indexes :ma_description, index_analyzer: 'czech', search_analyzer: 'czech'
      indexes :cs_description, index_analyzer: 'czech', search_analyzer: 'czech'
    end
   end

  def as_indexed_json(options={})
    { id: id,
      name: name,
      ma_description: ma_description,
      cs_description: cs_description,
      en_description: en_description    
    }
  end

   def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['name^10', 'ma_description', 'cs_description', 'en_description']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
            fields: {
              name: {},
              ma_description: {},
              cs_description: {},
              en_description: {}
            }
        }
      }
    )
  end

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-11 09:38

In order to be able to perform the search you are trying to do you'll to use the Ngram analyzer. (as discussed in the comments)

查看更多
登录 后发表回答