簡體   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