elasticsearch:只获取url中具有某个非空键的元素

时间:2012-05-06 21:27:47

标签: elasticsearch

我想过滤弹性搜索查询的结果,只检索那些具有非空字段的结果

例如。提供以下数据

{
  total: 4912,
  max_score: 1,
  hits: [
{
  {
    _index: "gcba",
    _type: "bafici",
    _id: "5a93472b-5db4-4ff9-8c8a-d13158e72d5f-62",
    _score: 1,
    _source: {
      id_film: "23",
      title: "great film",
    }
  },
  {
    _index: "gcba",
    _type: "bafici",
    _id: "2732fbf4-4e55-4794-8e98-e5d5fa6a0419-40",
    _score: 1,
    _source: {
      name: "conference",
      [...]
    }
  }
}

我想发布类似

的内容
.../_search?from=1&size=100&q=id_film:'*'

仅获取具有id_film值的元素

3 个答案:

答案 0 :(得分:5)

ES只会在执行通配符查询时返回默认具有该特定字段的文档:

% curl -XPUT http://localhost:9200/foo/bar/1 -d '{"id":1,"id_film":23}'
{"ok":true,"_index":"foo","_type":"bar","_id":"1","_version":1}%

% curl -XPUT http://localhost:9200/foo/bar/2 -d '{"id":2,"foo":23}'
{"ok":true,"_index":"foo","_type":"bar","_id":"2","_version":1}%

% curl "http://localhost:9200/foo/_search?q=id_film:*&pretty=true"
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "foo",
      "_type" : "bar",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"id":1,"id_film":23}
    } ]
  }
}%

答案 1 :(得分:4)

您还可以使用存在(或缺失)过滤器。见这里:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html

唯一的问题是,它是一个过滤器,而不是一个查询。要使其使用搜索方法,您需要查询match_all并作为过滤器存在。 (或者,使用在其中指定的此过滤器的constant_score查询)

答案 2 :(得分:0)

Exists Query的文档有很好的插图。