【发布时间】:2016-04-18 16:42:03
【问题描述】:
过去,我曾使用 Solr 处理过多个基于方面和过滤器的搜索,但我正在努力实现与 Elasticsearch 的平等。
我了解聚合是根据查询结果计算的,如果未指定查询,则在全局范围内计算。这很好,但是我希望这些聚合的 计数 基于过滤器的结果。
在 Solr 中这很简单 - 只需指定查询和过滤器 - 但在 Elasticsearch 中,过滤器对聚合没有影响,文档非常混乱。
我想要的以下查询的输出是将suggestions 存储桶限定为查询范围,但其中的结果计数限定为指定的filter:
{
"size": 0,
"query": {
"range": {
"published": {
"gte": "now-1y",
"lt": "now"
}
}
},
"filter": {
{
"term": {
"tag.id": "123"
}
},
{
"term": {
"tag.id": "456"
}
},
},
"aggs": {
"tags": {
"nested": {
"path": "tag"
},
"aggs": {
"suggestions": {
"terms": {
"field": "name",
"size": 10,
"min_doc_count": 1
},
"aggs": {
"id": {
"terms": {
"field": "id",
"size": 1
}
}
}
}
}
}
}
}
并给出示例映射:
{
"mappings":{
"content":{
"properties":{
"id":{
"type":"string",
"index":"not_analyzed"
},
"title":{
"type":"string"
},
"byline":{
"type":"string",
"index":"not_analyzed"
},
"body":{
"type":"string"
},
"publishedDate":{
"type":"date",
"format":"dateOptionalTime"
},
"tag":{
"type":"nested",
"include_in_parent":true,
"properties":{
"id":{
"type":"integer"
},
"name":{
"type":"string"
}
}
}
}
}
}
}
感谢任何帮助。
【问题讨论】:
-
您是否尝试使用filtered aggregation?
-
我有,但我无法获得预期的结果。我会进一步调查。
-
我不太明白您要做什么。你能解释一下吗?
标签: elasticsearch lucene faceted-search