0

考虑下面的停止字段是时间戳字段。

我想过滤具有以下条件的数据:

  1. 停止字段不存在
  2. 或者,停止字段值现在 >=

我知道,我应该使用 must_not 但不知道如何使用。

我想对子类型进行一些评分并使用此分数对父级进行排序,然后使用停止字段过滤掉父级。

GET indexName/parentType/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "child-type",
            "score_mode": "max",
            "query": {
              "function_score": {
                "functions": [
                  {
                    "script_score": {
                      "script": {
                        "file": "score-analytics",
                        "lang": "expression"
                      }
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "stop": {
                    "gte": "now"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
4

2 回答 2

0

如果您使用must_not bool条件来过滤该字段不存在,该怎么办。您的查询可能如下所示:

"query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "stop" <-- give the field which shouldn't exist
              }
            }
          ]
        }
      }
    }
  }

以上是一个示例,以便您可以复制。对于第二个条件,似乎就像您所做的那样使用范围查询。我几乎不能保证有更好的方法来获取时间戳range。希望能帮助到你!

于 2016-12-27T07:02:18.537 回答
0

您需要使用存在过滤器:

"bool": 
      { "should":[
         { "range": 
             { "stop": 
                 { "gte": "now" }
             }
         },
         { "query": 
           { "exists" : 
              { "field" : "stop" } 
            }  
          }
 ] }
于 2016-12-27T06:14:58.713 回答