0

I wish to calculate value-count aggregations on some indexed product data, but I seem to be getting some parameters in the ValueCountAgg constructor wrong.

An example of such indexed data is as follows -:

{
  "_index": "test-index",
  "_type": "product_product",
  "_id": "1",
  "_score": 1,
  "_source": {
    "code": "SomeProductCode1",
    "list_price": 10,
    "description": null,
    "displayed_on_eshop": "true",
    "active": "true",
    "tree_nodes": [],
    "id": 1,
    "category": {},
    "name": "This is Product",
    "price_lists": [
      {
        "price": 10,
        "id": 1
      },
      {
        "price": 10,
        "id": 2
      }
    ],
    "attributes": {
      "color": "blue",
      "attrib": "something",
      "size": "L"
    },
    "type": "goods"
  }
}

I'm calculating aggregations as follows -:

for attribute in filterable_attributes:
    count = ValueCountAgg(
        name='count_'+attribute, field='attributes.'+attribute
    )
    query.agg.add(count)

where query is a ~pyes.query.Query object wrapped inside a ~pyes.query.Search object. filterable_attributes is a list of attribute names, such as color and size.

I have tried setting field=attribute as well, but it seems to make no difference. The resultset that I obtain on conducting the search has the following as its aggs attribute -:

{'count_size': {'value': 0}, 'count_color': {'value': 0}}

where size and color are indexed inside the attributes dictionary as shown above. These are evidently wrong results, and I think it is because I am not setting field properly.

Where am I going wrong?

PritishC
  • 508
  • 8
  • 18

1 Answers1

1

I've found where I was going wrong.

According to Scoping Aggregations, the scope of an aggregation is by default associated with its query. My query was returning zero results, and I had to modify the search phrase for the same.

I got the required results after that, and aggregations are coming out right.

{'count_size': {'value': 3}, 'count_color': {'value': 3}}
PritishC
  • 508
  • 8
  • 18