How to count of nested inner_hits documents?


(Alexej Kloos) #1

Hi@all

i have many companies and each company has many products. But how i get the count of all products.

Query:

  GET /company/_search
    {
      "from": 0,
      "size": 5,
      "_source": false,
      "query": {
        "nested": {
          "path": "products",
          "inner_hits": {
            "_source": false
          },
          "query": {
            "match": {
              "products.category.id": 3
            }
          }
        }
      },
      "aggs": {
        "products": {
          "nested": {
            "path": "products"
          },
          "aggs": {
            "count": {
              "value_count": {
                "field": "products.id"
              }
            }
          }
        }
      }
    }

The result looks like:

  "aggregations": {
    "products": {
      "doc_count": 121,
      "count": {
        "value": 121
      }
    }
  }

This value is wrong. Me need Elasticsearch to tell, count please from inner_hits. But how?

Anybody a idea?


(Alexej Kloos) #2

Solution found...

 "aggs": {
"products": {
  "nested": {
    "path": "products"
  },
  "aggs": {
    "inner": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "products.category.id": 6
              }
            }
          ]
        }
      },
      "aggs": {
        "agg_terms_products.country": {
          "terms": {
            "field": "products.country",
            "order": {
              "_count": "desc"
            },
            "size": 100
          }
        }
      }
    }
  }
}

},


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.