弹性滤波器面数

时间:2017-12-01 08:51:52

标签: elasticsearch elasticsearch-5

我有一个包含动态属性的文档,我可以使用此查询进行过滤:

{
    "query": {
        "nested": {
            "path": "properties",
            "query" : {
                "bool": {
                    "must" : [
                        {
                            "term" : {
                                "properties.name": "Merk"
                            }
                        },
                        {
                            "term" : {
                                "properties.value": "Elcee Holland"
                            }
                        }
                    ]
                }
            }
        }
    }
}

此查询的结果是:

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 10.14826,
        "hits": [
            {
                "_index": "demo-articles-v3",
                "_type": "article",
                "_id": "120400871755634330808993320",
                "_score": 10.14826,
                "_source": {
                    "id": "120400871755634330808993320",
                    "name": "Metaalschroef binnenzeskant, DIN 912 RVS A4-80",
                    "description": "m16x70 cilinderschroef bzk a4-80 din912 klasse 80",
                    "fullDescription": "Metaalschroef met een binnenzeskant cilinderkop",
                    "synonyms": [],
                    "properties": [
                        {
                            "name": "draad",
                            "value": "16",
                            "sort": 99
                        },
                        {
                            "name": "lengte",
                            "value": "70",
                            "sort": 99
                        },
                        {
                            "name": "materiaal",
                            "value": "roestvaststaal",
                            "sort": 99
                        },
                        {
                            "name": "kwaliteit (materiaal)",
                            "value": "A4",
                            "sort": 99
                        },
                        {
                            "name": "DIN",
                            "value": "912",
                            "sort": 99
                        },
                        {
                            "name": "AISI",
                            "value": "316",
                            "sort": 99
                        },
                        {
                            "name": "draadsoort",
                            "value": "metrisch",
                            "sort": 99
                        },
                        {
                            "name": "Merk",
                            "value": "Elcee Holland",
                            "sort": 1
                        }
                    ]
            },
            {
                "_index": "demo-articles-v3",
                "_type": "article",
                "_id": "120304871755635880708993326",
                "_score": 10.145194,
                "_source": {
                    "id": "120304871755635880708993326",
                    "name": "Zeskant moer, DIN 934 RVS A4-80",
                    "description": "m16 zeskantmoer rvs a4-80 din934",
                    "fullDescription": "Zeskanten moer in A4-80 kwaliteit",
                    "synonyms": [],
                    "properties": [
                        {
                            "name": "draad",
                            "value": "16",
                            "sort": 99
                        },
                        {
                            "name": "materiaal",
                            "value": "roestvaststaal",
                            "sort": 99
                        },
                        {
                            "name": "kwaliteit (materiaal)",
                            "value": "A4-80",
                            "sort": 99
                        },
                        {
                            "name": "DIN",
                            "value": "934",
                            "sort": 99
                        },
                        {
                            "name": "AISI",
                            "value": "316",
                            "sort": 99
                        },
                        {
                            "name": "draadsoort",
                            "value": "metrisch",
                            "sort": 99
                        },
                        {
                            "name": "Merk",
                            "value": "Elcee Holland",
                            "sort": 1
                        }
                    ]
                }
            },
            {
                "_index": "demo-articles-v3",
                "_type": "article",
                "_id": "120044871755635678006176201",
                "_score": 10.054587,
                "_source": {
                    "id": "120044871755635678006176201",
                    "name": "Zeskanttapbout, DIN 933 RVS A4-80",
                    "description": "m16x45 tapbout din933 a4-80",
                    "fullDescription": "Zeskanten tapbout",
                    "synonyms": [],
                    "properties": [
                        {
                            "name": "draad",
                            "value": "16",
                            "sort": 99
                        },
                        {
                            "name": "lengte",
                            "value": "45",
                            "sort": 99
                        },
                        {
                            "name": "aandrijving",
                            "value": "zeskant",
                            "sort": 99
                        },
                        {
                            "name": "materiaal",
                            "value": "roestvaststaal",
                            "sort": 99
                        },
                        {
                            "name": "kwaliteit (materiaal)",
                            "value": "A4-80",
                            "sort": 99
                        },
                        {
                            "name": "DIN",
                            "value": "933",
                            "sort": 99
                        },
                        {
                            "name": "AISI",
                            "value": "316",
                            "sort": 99
                        },
                        {
                            "name": "draadsoort",
                            "value": "metrisch",
                            "sort": 99
                        },
                        {
                            "name": "Merk",
                            "value": "Elcee Holland",
                            "sort": 1
                        }
                    ]
                }
            }
        ]
    }
}

现在我想了解过滤后的搜索结果的各个方面。所以我想要一个带有“draad”(3):16(3)的桶。等

我尝试了这个查询:

{
    "size" : 0,
    "aggs" : {
        "all" : {
            "global" : {},
            "aggs" : {
                "features" : {
                    "nested" : {
                        "path" : "properties"
                    },
                    "aggs" : {
                        "names" : {
                            "filter" : {
                                "nested" : {
                                    "path" : "properties",
                                    "query" : {
                                        "bool" : {
                                            "must" : [{
                                                    "term" : {
                                                        "properties.name" : "Merk"
                                                    }
                                                }, {
                                                    "term" : {
                                                        "properties.value" : "Elcee Holland"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            },
                            "aggs" : {
                                "filtered_names" : {
                                    "terms" : {
                                        "field" : "properties.name",
                                        "size" : 5
                                    },
                                    "aggs" : {
                                        "values" : {
                                            "terms" : {
                                                "field" : "properties.value",
                                                "size" : 5
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
    }
}

但是这会返回一个奇怪的结果。只是一个桶而不是几个水桶......我在这里做错了什么?

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 9288,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "all": {
            "doc_count": 9288,
            "features": {
                "doc_count": 41925,
                "names": {
                    "doc_count": 3,
                    "filtered_names": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "draadsoort",
                                "doc_count": 3,
                                "values": {
                                    "doc_count_error_upper_bound": 0,
                                    "sum_other_doc_count": 0,
                                    "buckets": [
                                        {
                                            "key": "metrisch",
                                            "doc_count": 3
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我从您的查询中删除了一些部分,我认为这些部分是不必要的。这就是我进行嵌套聚合的方式:

{
  "size": 0,
  "aggs": {
    "features": {
      "nested": {
        "path": "properties"
      },
      "aggs": {
        "names": {
          "filter": {
            "must": [
              {
                "term": {
                  "properties.name": "Merk"
                }
              },
              {
                "term": {
                  "properties.value": "Elcee Holland"
                }
              }
            ]
          },
          "aggs": {
            "filtered_names": {
              "terms": {
                "field": "properties.name",
                "size": 5
              },
              "aggs": {
                "values": {
                  "terms": {
                    "field": "properties.value",
                    "size": 5
                  }
                }
              }
            }
          }
        }
      }
    }
  }
} 

希望它能解决你的问题。