Azima
Azima

Reputation: 4141

elasticsearch facet nested aggregation

Using elasticsearch 7.0.0.

I am following this link.

I have an index test_products with following mapping:

{
"settings": {
    "number_of_shards": 1
},
"mappings": {
    "dynamic_templates": [
        {
            "search_result_data": {
                "mapping": {
                    "type": "keyword"
                },
                "path_match": "search_result_data.*"
            }
        }
        ],
    "properties": {
        "search_data": {
            "type": "nested",
            "properties": {
                "full_text": {
                    "type": "text"
                },
                "string_facet": {
                    "type": "nested",
                    "properties": {
                        "facet-name": {
                            "type": "keyword"
                        },
                        "facet-value": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}
}

And a document inserted with following format:

    {
  "search_result_data": {
    "sku": "wheel-6075-90092",
    "gtin": null,
    "name": "Matte Black Wheel Fuel Ripper",
    "preview_image": "abc.jg",    
    "url": "9836817354546538796",
    "brand": "Fuel Off-Road"
  },
  "search_data": 
    {
      "full_text": "Matte Black Wheel Fuel Ripper",
      "string_facet": [
        {
          "facet-name": "category",
          "facet-value": "Motor Vehicle Rims & Wheels"
        },
        {
          "facet-name": "brand",
          "facet-value": "Fuel Off-Road"
        }
      ]
    }    
}

and one other document..

I am trying to aggregate on string_facet as mentioned in the link.

"aggregations": {


"agg_string_facet": {
    "nested": {
      "path": "string_facet"
    },
    "aggregations": {
      "facet_name": {
        "terms": {
          "field": "string_facet.facet-name"
        },
        "aggregations": {
          "facet_value": {
            "terms": {
              "field": "string_facet.facet-value"
            }
          }
        }
      }
    }
  }
}

But I get all (two) documents returned with :

"aggregations": {
    "agg_string_facet": {
      "doc_count": 0
   }
 }

What am I missing here?

Also why are the docs being returned as a response?

Upvotes: 0

Views: 823

Answers (1)

Kevin Quinzel
Kevin Quinzel

Reputation: 1428

Documents are returned as a response because they match with your query. If you'd like them to disappear, you can set the "size" field to 0. By default, it's set to 10.

query{
...
}, 
"size" = 0

I read the docs and Facet aggregation has been removed. The recommendation is to use the Terms aggregation.

Now, for your question, you can go with two options:

  1. If you'd like to get the unique values for each: facet-value and facet-name, you can do the following:

"aggs":{
    "unique facet-values":{
        "terms":{
            "field": "facet-value.keyword",
            "size": 30 #By default is 10, maximum recommended is 10,000
        }
    },
    "unique facet-names":{
        "terms":{
            "field": "facet-name.keyword"
            "size": 30 #By default is 10, maximum recommended is 10,000
        }
    }
}
  1. If you'd like to get the unique combinations between facet-name and facet-value, you can use the Composite aggregation. If you choose this way, your aggs should look like this:

{
    "aggs":{
       "unique-facetvalue-and-facetname-combination":{
            "composite":{
                "size": 30, #By default is 10, maximum recommended is 10,000. No matter what size you choose, you can paginate.
                "sources":[
                    { 
                         "value":
                             { 
                                 "terms":{ 
                                     "field": "facet-value.keyword" 
                                  } 
                             } 
                    },
                    { 
                         "name":
                             { 
                                 "terms":{ 
                                     "field": "facet-name.keyword" 
                                  } 
                             } 
                    }
                ]
            }
        }
    }
}

The advantage of using Composite over Terms is that Composite lets you paginate your results with the After key. So your cluster's performance does not get affected.

Hope this is helpful! :D

Upvotes: 2

Related Questions