繁体   English   中英

Elasticsearch和Rails:使用ngram搜索单词的一部分

[英]Elasticsearch and Rails: Using ngram to search for part of a word

我想在我的项目中使用Elasticsearch-Gem。 据我了解:到现在为止,不再需要轮胎宝石,或者我错了?

在我的项目中,我有一个搜索(obivously),目前适用于一个模型。 现在我试图避免使用通配符,因为它们不能很好地扩展,但我似乎无法让ngram-Analyzers正常工作。 如果我搜索整个单词,搜索仍然有效,但不适用于部分内容。

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"
  }

也许有人可以给我一个正确方向的暗示。

我放弃了在模型类中定义模式。 事实上,它也没有多大意义。

所以这就是我所做的。 模式/映射定义db /文件夹和构建它的rake任务。

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

在模型中

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

我正在使用基于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"
               }
            }
         }
      }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM