如何在弹性搜索中获取具有非空字段的文档?

时间:2016-07-13 12:54:54

标签: elasticsearch

我有这个查询,它使用过滤器给我文档。如何确保坐标字段不为空/ null?

 {
    "size":1,
    "_source": ["coordinates"]
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {

                "range": {
                    "timestamp_ms": {
                        "gte": "1468015200000",
                        "lte": "1468022400000"
                    }
                }
            }
        }
    }    
}   

输出:

{
  "took" : 41,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 79112,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "twitter",
      "_type" : "tweet",
      "_id" : "751536344866910208",
      "_score" : 1.0,
      "_source" : {
        "coordinates" : null
      }
    } ]
  }
}

这是映射:

"coordinates" : {
    "properties" : {
        "coordinates" : {
            "type" : "geo_point"
        },
        "type" : {
            "type" : "string",
            "index" : "not_analyzed"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在过滤器/查询中添加过滤器exists

一个例子:

 {
  "size": 1,
  "_source": [
    "coordinates"
  ],
  "query": {
    "filtered": {
      "query": {
        "exists": {
          "field": "coordinates"
        }
      },
      "filter": {
        "range": {
          "timestamp_ms": {
            "gte": "1468015200000",
            "lte": "1468022400000"
          }
        }
      }
    }
  }
}