【问题标题】:Elasticsearch terms aggregation on a not analyzed field with filters带有过滤器的未分析字段上的 Elasticsearch 术语聚合
【发布时间】:2016-10-30 02:54:26
【问题描述】:

我的索引上有一个not analyzed 字段:

"city": { "type": "string", "index": "not_analyzed" }

我有如下聚合:

"aggs": {
    "city": {
        "terms": {
            "field": "city"
        }
    }
}

这给了我这样的输出:

"aggregations": {
    "city": {
        "doc_count_error_upper_bound": 51,
        "sum_other_doc_count": 12478,
        "buckets": [
            {
                "key": "New York",
                "doc_count": 28420
            },
            {
                "key": "London",
                "doc_count": 23456
            },
            {
                "key": "São Paulo",
                "doc_count": 12727
            }
        ]
    }
}

我需要在处理聚合之前添加一个match_phrase_prefix 查询,以根据用户文本过滤我的结果,如下所示:

{
    "size": 0,
    "query": {
        "match_phrase_prefix": {
            "city": "sao"
        }
    },
    "aggs": {
        "city": {
                "terms": {
                    "field": "city"
                }
            }
    }
}

结果是……什么都没有!

"aggregations": {
    "city": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": []
    }
}

我期待São Paulo city 的聚合结果。显然问题是我的字段应该有 lowercaseasciifolding 过滤器才能匹配(São/sao),但我无法分析我的字段,因为我不想有像 São 这样的聚合结果、PauloNewYork(在分析字段上会发生这种情况)。

我能做什么?我尝试了很多与映射/查询/聚合的组合,但我无法让它工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    因为它是not_analyzed,所以查询词是case-sensitive。 您可以将multi-field 映射到cityanalyzed and non-analyzed fields

    例子:

    put <index>/<type>/_mapping
    {
       "properties": {
          "city": {
             "type": "string",
             "fields": {
                "raw": {
                   "type": "string",
                   "index": "not_analyzed"
                }
             }
          }
       }
    }
    
    post <index>/<type>/_search
    {
        "size": 0,
        "query": {
            "match_phrase_prefix": {
                "city": "Sao"
            }
        },
        "aggs": {
            "city": {
                    "terms": {
                        "field": "city.raw"
                    }
                }
        }
    }
    

    【讨论】:

    • 该字段必须匹配“sao”(小写和 asciifolding)并且我的 aggs 必须返回像“São Paulo”这样的存储桶,而不是“São”和“Paulo”。跨度>
    • 您需要使用multi fields 查看编辑后的答案
    • 至少在我的情况下,当您放置该映射而不是执行后查询时,它一开始不起作用。但是当我今天早上执行后查询时它现在可以工作了,我想我们需要在应用该映射问题后重新启动弹性服务。我不知道也许我们不知道。非常感谢@keety
    • 这个ES documentation page 似乎相关。
    【解决方案2】:
    "city": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
    
    • 现在分析“城市”

    • “city.keyword”未分析

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2014-05-12
      • 2015-07-12
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多