【问题标题】:ElasticSearch query with prefix for aggregation带有聚合前缀的 ElasticSearch 查询
【发布时间】:2019-12-04 16:31:29
【问题描述】:

我正在尝试在“必须”子句中为我的 ES 查询添加前缀条件。 我当前的查询看起来像这样:

body = {
            "query": {
                "bool": {
                    "must":
                        { "term": { "article_lang": 0 }}
                    ,
                    "filter": {
                        "range": {
                            "created_time": {
                                "gte": "now-3h"
                            }
                        }
                    }
                }
            },
            "aggs": {
                "articles": {
                    "terms": {
                        "field": "article_id.keyword",
                        "order": {
                            "score": "desc"
                        },
                        "size": 1000
                    },
                    "aggs": {
                        "score": {
                            "sum": {
                                "field": "score"
                            }
                        }
                    }
                }
            }
        }

我需要在我的查询中添加一个强制条件来过滤 id 以“article-”开头的文章。

所以,到目前为止,我已经尝试过了:

{
            "query": {
                "bool": {
                    "should": [
                        { "term": { "article_lang": 0 }},
                        { "prefix": { "article_id": {"value": "article-"} }}
                    ],
                    "filter": {
                        "range": {
                            "created_time": {
                                "gte": "now-3h"
                            }
                        }
                    }
                }
            },
            "aggs": {
                "articles": {
                    "terms": {
                        "field": "article_id.keyword",
                        "order": {
                            "score": "desc"
                        },
                        "size": 1000
                    },
                    "aggs": {
                        "score": {
                            "sum": {
                                "field": "score"
                            }
                        }
                    }
                }
            }
        }

我对 ES 还很陌生,从在线文档中,我知道“应该”用于“或”条件,“必须”用于“与”。这将返回一些数据,但根据条件,它将由 article_lang=0 或以 article- 开头的文章组成。当我使用“必须”时,它不会返回任何内容。

我确定有 id 以这个前缀开头的文章,因为目前,我们正在迭代这个结果以过滤掉这些文章。我在这里错过了什么?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-query


    【解决方案1】:

    在您的prefix 查询中,您需要使用article_id.keyword 字段,而不是article_id。此外,您应该更喜欢filter 而不是must,因为您只是在进行是/否匹配(也称为过滤器)

    {
      "query": {
        "bool": {
          "filter": [                               <-- change this
            {
              "term": {
                "article_lang": 0
              }
            },
            {
              "prefix": {
                "article_id.keyword": {             <-- and this
                  "value": "article-"
                }
              }
            }
          ],
          "filter": {
            "range": {
              "created_time": {
                "gte": "now-3h"
              }
            }
          }
        }
      },
      "aggs": {
        "articles": {
          "terms": {
            "field": "article_id.keyword",
            "order": {
              "score": "desc"
            },
            "size": 1000
          },
          "aggs": {
            "score": {
              "sum": {
                "field": "score"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 酷,很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2020-09-05
    • 2020-07-21
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多