【问题标题】:ElasticSearch with Tire won't match nGrams on 'string' search, only 'text'带有轮胎的 ElasticSearch 与“字符串”搜索中的 nGram 不匹配,仅匹配“文本”
【发布时间】:2012-08-20 04:55:05
【问题描述】:

所以我一直在尝试将 nGram 匹配添加到我的 ElasticSearch 索引中,但我遇到了以下问题。

执行标准 string 查询只会返回完全匹配。对特定 test 字段运行 ma​​tch 查询会产生预期的 nGram 匹配。

我根据 these(1) examples(2) 为我的字段设置 nGram 过滤器和分析器。映射代码如下:

tire.settings :number_of_shards => 1,
      :number_of_replicas => 1,
      :analysis => {
        :analyzer => {
          "str_search_analyzer" => {
            "tokenizer" => "keyword",
            "filter" => "lowercase"
          },
          "str_index_analyzer" => {
            "tokenizer" => "keyword",
            "filter" => ["lowercase","substring"]
          }
        },
        :filter => {
          :substring => {
            "type" => "nGram",
            "min_gram" => 1,
            "max_gram" => 10
          }
        }
      } do
      mapping do
        indexes :test, :type=>'string',
                :search_analyzer => :str_search_analyzer,
                :index_analyzer=>:str_index_analyzer
      end
    end

  def to_indexed_json
    #adding known word plus random string for testing
    { 
      :test => "pizza" + (0...10).map{ ('a'..'z').to_a[rand(26)] }.join
    }.to_json
  end

ElasticSearch 查询

产生结果的查询:

curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"text":{"test":{"query":"piz"}}}}'

不产生任何结果的查询:

curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"query_string":{"query":"pizz"}}}'

有没有办法让一般的 query_string 搜索来查看所有索引字段并匹配 ngram,而不必对特定列进行文本/匹配搜索?

【问题讨论】:

    标签: ruby-on-rails elasticsearch tire n-gram


    【解决方案1】:

    这是预期的行为。默认情况下,“query_string”查询在“_all”字段上执行。由于该字段是使用 StandardAnalyzer 索引的,因此它的索引标记将不同于“test”字段(您配置为使用 nGram 分析器)的索引标记。

    您可以通过多种方式改变这种行为:

    1. 更改索引设置中的映射并为“_all”字段配置 nGram 分析器
    2. 将“_analyzer”字段作为文档的一部分发送(它将被拾取并用于所有未为其配置显式分析器的字段)
    3. 使用“fields”属性指定您希望在哪些字段上执行“query_string”

    在以上所有三个选项中,最推荐#3。显式指定字段可以让您更好地控制数据(如何索引和查询数据)。

    【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 2017-12-21
    • 2013-01-01
    • 1970-01-01
    • 2015-01-18
    • 2012-02-01
    • 2017-07-04
    • 2011-08-07
    相关资源
    最近更新 更多