【问题标题】:Elasticsearch and Rails: Using ngram to search for part of a wordElasticsearch 和 Rails:使用 ngram 搜索单词的一部分
【发布时间】:2014-08-13 23:50:17
【问题描述】:

我正在尝试在我的项目中使用 Elasticsearch-Gem。据我了解:现在已经不需要轮胎宝石了,还是我错了?

在我的项目中,我有一个搜索(显然),它目前适用于一个模型。现在我试图避免使用通配符,因为它们不能很好地扩展,但我似乎无法让 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"
  }

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

【问题讨论】:

  • 始终值得通过查询 elasticsearch 来检查您的设置和映射是否符合您的预期:curl -XGET 'http://localhost:9200/pictures/_settings?pretty=true'curl -XGET 'http://localhost:9200/pictures/_mapping?pretty=true
  • 你解决了吗?我也被困在这一点上。我可以通过 CURL 让它工作,但不能使用 Elasticsearch-Rails。
  • 很想知道是否有人发现了这一点。文档非常糟糕——我不明白他们为什么使用这种 mapping do ... 语法,而不是像你传递给 ES 那样只给出一个哈希值?

标签: ruby-on-rails ruby search elasticsearch rails-activerecord


【解决方案1】:

我已经放弃在模型类中定义模式。其实也没什么意义。

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

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

在模型中

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的提示。你在哪里设置这些配置?它们是在您的 rails 类中配置的,还是您从某个地方导入这些设置?
    • 我用的不是Rails,而是Java...但是最后你应该能够发送一些json到ES,不管用什么语言来存档结果。我在答案中发布的代码来自读取映射:GET /en_suggestions/_mapping?pretty=true 和设置:GET /en_suggestions/_settings?pretty=true 我正在使用 Google Chrome 的“Sense”来发出此命令并在将其成型为编程代码之前进行试验。
    • 谢谢,我会看看这个以及“Sense”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多