2017-08-01 171 views
0

我有我的文档与嵌套位置索引,嵌套聚合过滤

{ 
    "name": "name 1", 
    "locations": [ 
    { 
     "region": "region1", 
     "city": "city1", 
     "suburb": "suburb1" 
    }, 
    { 
     "region": "region2", 
     "city": "city2", 
     "suburb": "suburb2" 
     }, 
     { 
     region": "region1", 
     "city": "city5", 
     "suburb": "suburb4" 
     }] 
} 

我有我的查询作为

{ 
    "query": { 
    "nested": { 
     "path": "locations", 
     "query": { 
      "bool": { 
      "must": [ 
       { 
       "term": { 
        "locations.region.keyword": { 
        "value": "region1" 
        } 
       } 
       } 
      ] 
      } 
     } 
     } 
    } 
    } 

我要为region1合计只有城市。我已尝试nested聚合,nestedfilter聚合,并与reverse嵌套。似乎没有任何工作。问题是因为文件与locations集合中的其他区域一起出现,即使是不属于region1的城市,也会汇总所有内容。

有什么想法?

编辑

映射:

"my_index": { 
    "mappings": { 
    "my_type": { 
     "properties": { 
     "locations": { 
      "type": "nested", 
      "properties": { 
       "city": { 
       "type": "text", 
        "fields": { 
        "keyword": { 
         "type": "keyword", 
         "ignore_above": 256 
        } 
        } 
       }, 
       "region": { 
        "type": "text", 
        "fields": { 
         "keyword": { 
         "type": "keyword", 
         "ignore_above": 256 
         } 
        } 
        }, 
        "suburb": { 
        "type": "text", 
         "fields": { 
         "keyword": { 
          "type": "keyword", 
          "ignore_above": 256 
         } 
         } 
        } 
        } 
       }, 
       "name": { 
        "type": "text", 
        "fields": { 
        "keyword": { 
         "type": "keyword", 
         "ignore_above": 256 
        } 
        } 
       } 
       } 
      } 
      } 
     } 

查询:

{ 
    "size": 0, 
    "query": { 
    "nested": { 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "terms": { 
       "locations.region.keyword": [ 
        "region1" 
       ] 
       } 
      } 
      ] 
     } 
     }, 
     "path": "locations" 
    } 
    }, 
    "aggs": { 
    "City": { 
     "nested": { 
     "path": "locations" 
     }, 
     "aggs": { 
     "City": { 
      "terms": { 
      "field": "locations.city.keyword", 
      "size": 100, 
      "order": [ 
       { 
       "_count": "desc" 
       }, 
       { 
       "_term": "asc" 
       } 
      ] 
      }, 
      "aggs": { 
      "City": { 
       "reverse_nested": {} 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

你能告诉什么映射看起来像你的类型,即什么'GET {指数}/{类型}/_ mapping'回报? –

+0

已更新,包括映射和我的示例查询 –

回答

1

假设你的映射是正确的,因为每个查询您的使用情况

您可以使用下面提到的查询可以在你使用过滤器聚合。

{ 
    "query": { 
     "match_all": {} 
    }, 
    "aggs": { 
     "city_agg": { 
      "nested": { 
       "path": "locations" 
      }, 
      "aggs": { 
       "filter_locations_regions": { 
        "filter": { 
         "term": { 
          "locations.region.keyword": "region1" 
         } 
        }, 
        "aggs": { 
         "cities_in_region_agg": { 
          "terms": { 
           "field": "locations.city.keyword", 
           "size": 100, 
           "order": [{ 
             "_count": "desc" 
            }, 
            { 
             "_term": "asc" 
            }] 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
+0

已更新问题 –

+0

@commit您是否尝试过查询?它没有工作吗? – Rahul

+0

我即将试用它。我只是想先给出额外的信息,以防在我尝试之前改变它。 –