【问题标题】:How to filter aggregation results in elasticsearch (v 6.3)如何在 elasticsearch (v 6.3) 中过滤聚合结果
【发布时间】:2018-08-14 17:25:11
【问题描述】:

我有一个字段商品行数组,例如:[3,35,1,11,12],[3,12]。我正在尝试查询该字段以获取自动完成结果,当我与 3 匹配时,我需要输出为 3 和 35。我的索引适用于所有场景,除非我使用数组数据类型。

我需要过滤聚合结果以给出 3 和 35,这是我无法检索的。我需要使用 facet_filter 或带有前缀的过滤器。类似于 solr 中的 facet.prefix。 让我知道是否需要更改查询或映射? 查询:

GET contracts/doc/_search
{
 "size":0,
 "query":{
   "bool":{
     "must":{
       "match":{
         "commodity_line.autocomplete":"3"

       }
     }

   }
 },
"aggs" : {
  "names":{
    "terms":{
      "field":"commodity_line.keyword"
    }
  }

    }
}

映射:

   PUT contracts
        {

           "settings":{
              "analysis":{
                 "filter":{


                "gramFilter": {

                     "type":     "edge_ngram",
                    "min_gram" : 1,
                    "max_gram" : 20,
                  "token_chars": [
                    "letter",
                   "symbol",
                    "digit"
                  ]
                }

              },
                 "analyzer":{


                      "autocomplete": { 
                  "type": "custom",
                  "tokenizer": "standard",
                  "filter": [
                    "lowercase",
                    "trim",
                    "gramFilter",
                     "asciifolding"


                  ]
                }




                 }
              }
              }

           ,
           "mappings":{
              "doc":{
                 "properties":{
         "commodity_line" :{
                       "type":"text",
                      "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      },

                      "autocomplete":{
                        "type":"text",
                        "analyzer":"autocomplete", 
                      "search_analyzer":"standard"
                      }
                      }


                   }
        }
        }
           }
        }

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    我找到了解决办法, 我必须将其与前缀匹配,而不是过滤结果。

    "aggs" : {
      "names":{
        "terms":{
          "field":"commodity_line.keyword",
     "include" : "3.*"
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多