不得查询elasticsearch

时间:2016-06-22 14:17:45

标签: elasticsearch

我有我的要求:

{
    "size": 10, 
    "query": {
        "filtered": {
           "query": {
                "bool": {
                    "must": [
                        {"term": {"event": "matchmaking_done"}}
                ]
            }
        },
           "filter": {
                "range": {
                    "@timestamp": {
                        "gt"  : "2016-06-01T00:00:00.000Z",
                        "lte" : "2016-06-01T00:05:00.000Z"
                    }
                }
            }
        }
    },
    "aggs" : {
        "user-ids" : {
            "terms" : { "field" : "user_id",
                "size": 0
            }
        }
    }
}

我需要添加到此请求参数中 - 不包含字段pvp_league!我尝试添加must_not,但无法理解如何正确执行此操作。 求救!

2 个答案:

答案 0 :(得分:1)

您自己做了回答,但ES 2.x执行此操作的方法是使用filtered查询,因为它已被弃用,并且将在ES 5.0中删除。 ES 2.x引入了"过滤器"的概念。上下文而不是每个查询只是一个查询或过滤器;现在每个查询都是过滤器或查询(得分),只是取决于它所使用的上下文。

对于您的查询,由于简化的bool / filter语法,因此这会变得更简单:

{  
  "size":10,
  "query":{  
    "bool":{  
      "must":[  
        {  
          "term":{  
            "event":"matchmaking_done"
          }
        }
      ],
      "must_not":[  
        {  
          "exists":{  
            "field":"pvp_league"
          }
        }
      ],
      "filter":[  
        {  
          "range":{  
            "@timestamp":{  
              "gt":"2016-06-01T00:00:00.000Z",
              "lte":"2016-06-01T00:05:00.000Z"
            }
          }
        }
      ]
    }
  },
  "aggs":{  
    "user-ids":{  
      "terms":{  
        "field":"user_id",
        "size":0
      }
    }
  }
}

非常重要的是,为"size" : 0聚合指定terms,您要求所有唯一字词,最多INT_MAX。这不是一个可扩展的请求(适用于10个user_ids,甚至100个,但不是10000个用户)。

除了不那么糟糕之外,您的请求根本不需要查询上下文,因为它的搜索方面没有关心相关性。您的术语查询"event" : "matchmaking_done")要么匹配,要么不匹配。既然你想要它匹配与否,但你并不真正关心秩序本身,你应该在过滤器上下文中使用它。这会将请求更改为:

{  
  "size": 10,
  "query": {  
    "bool": {  
      "must_not": [  
        {
          "exists": {
            "field": "pvp_league"
          }
        }
      ],
      "filter":[
        {
          "range": {
            "@timestamp": {
              "gt":"2016-06-01T00:00:00.000Z",
              "lte":"2016-06-01T00:05:00.000Z"
            }
          }
        },
        {
          "term": {
            "event": "matchmaking_done"
          }
        }
      ]
    }
  },
  "aggs": {
    "user-ids": {
      "terms": {
        "field": "user_id",
        "size": 0
      }
    }
  }
}

答案 1 :(得分:0)

我找到了解决方案!它看起来像这样:

{
    "size": 10, 
    "query": {
        "filtered": {
           "query": {
                "bool": {
                    "must": [
                        {"term": {"event": "matchmaking_done"}}
                ],
                "must_not": [
                    {"filtered": {
                        "filter": {
                            "exists": {
                                "field": "pvp_league"
                            }
                        }
                    }
                }
            ]
            }
        },
           "filter": {
                "range": {
                    "@timestamp": {
                        "gt"  : "2016-06-01T00:00:00.000Z",
                        "lte" : "2016-06-01T00:05:00.000Z"
                    }
                }
            }
        }
    },
    "aggs" : {
        "user-ids" : {
            "terms" : { "field" : "user_id",
                "size": 0
            }
        }
    }
}
相关问题