2014-12-23 147 views
2

我想弄清楚如何做这个例子。比方说,我有3个文件结构如下:没有嵌套文档的文档数

{ 
    "name": "test one", 
    "images": [ 
     { 
      "id": 1 
     } 
    ] 
} 


{ 
    "name": "test two", 
    "images": [] 
} 


{ 
    "name": "test three", 
    "images": [ 
     { 
      "id": 2 
     } 
    ] 
} 

我想要么得到的文件计数与该images对象(在这种情况下是2),或(较不优选)文件数对象在images字段(在本例中为1)。这是为了聚合查询之一,万一这不明显。我试着约100种不同的聚合类型,包括这个

... 
"withoutPhotos": { 
    "nested": { 
    "path": "images" 
    }, 
    "aggs": { 
    "noPhoto": { 
     "missing": { 
     "field": "images.id" 
     } 
    } 
    } 
} 

此,

... 
"withoutPhotos": { 
    "missing": { 
    "field": "images" 
    } 
} 

和他人的丰富性。有任何想法吗?

回答

0

这里有一个查询返回缺少images.id场的结果(看起来非常相似,你的):

curl -XGET 'http://localhost:9200/index/test/_search?search_type=count&pretty' -d ' 
> { "query" :{ 
> "match_all": { } 
> }, 
> "aggs": { 
>  "noPhoto": { "missing": {"field": "images.id"} } 
> } 
> }' 
{ 
    "took" : 4, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 3, 
    "max_score" : 0.0, 
    "hits" : [ ] 
    }, 
    "aggregations" : { 
    "noPhoto" : { 
     "doc_count" : 1 
    } 
    } 
} 

这里有一个查询返回的实例数images.id - 不能完全肯定如果这是你想要的(它返回一个字段数而不是文件数)。

[email protected]:~$ curl -XGET 'http://localhost:9200/index/test/_search?search_type=count&pretty' -d ' 
> { "query" :{ 
> "match_all": { } 
> }, 
> "aggs": { 
> "images_count" : { "value_count" : { "field" : "images.id" } } 
> } 
> }' 
{ 
    "took" : 4, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 3, 
    "max_score" : 0.0, 
    "hits" : [ ] 
    }, 
    "aggregations" : { 
    "images_count" : { 
     "value" : 2 
    } 
    } 
} 

其他选项是将某些东西放在查找“images.id”的查询中 - 例如,通配符。

0

我会建议这种方法。

curl -XGET 'http://localhost:9200/test/test/_search?search_type=count&pretty' -d ' 
{ "query" :{ 
    "match_all": { } 
    }, 
    "aggs": { 
    "noImage": { "missing": {"field": "images.id"} } 
    } 
}' 

结果

{ 
    "took" : 4, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 3, 
    "max_score" : 0.0, 
    "hits" : [ ] 
    }, 
    "aggregations" : { 
    "noImage" : { 
     "doc_count" : 1 
    } 
    } 
} 

这里hits.total将在该索引的文档总数。 aggregations.noImage.doc_count是不具有图像的文档数量。 因此具有图像场的文档的数量将是hits.total - aggregations.noImage.doc_count

  1. 文档具有图像= hits.total - aggregations.noImage.doc_count
  2. 文档不具有图像= aggregations.noImage.doc_count