Dynamic Faceting


(Mymoona S M) #1

I was able to aggregate over dynamic facets that I have in my feed as mentioned in the comment:

My response would look something like:
"buckets": [ { "key": "color_family", "doc_count": 2, "value": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "white", "doc_count": 1 }, { "key": "black", "doc_count": 1 } ] } { "key": "paper color", "doc_count": 2, "value": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "white", "doc_count": 3 }, { "key": "green", "doc_count": 1 } ] } },

When I do something like below: I get all color_family and all white, where as it should be just white from color_family.

{
"filter": {
"bool": {
"must": [
{
"match": {
"attributes.key": "color_family"
}
},
{
"match": {
"attributes.value": "white"
}
}
]
}
}
}

I am looking for it to work more like a normal facet, please let me know if any has solved this before.


(Mark Harwood) #2

I think you'll need to provide more detailed materials namely the JSON for :

  1. The mapping
  2. An example doc
  3. The query

From the query snippet you provided it looks like there's no "nested" clause so your query will match root-level docs not the nested attributes. Once we have a cut-down reproducible example of the scenario we can iterate further


(Mymoona S M) #3

First query for getting facets
{
"query": {
"multi_match" : {
"query": "tops",
"fields": ["name^12.1", "brand^20.0"]
}
},
"aggs": {
"attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"key": {
"terms": {
"field": "attributes.key"
},
"aggs": {
"value": {
"terms": {
"field": "attributes.value"
}
}
}
}
}
}
}
}

response:

{
"took": 826,
"hits": {
"total": 29513,
"max_score": 102.30364,
"hits": [
....
]
},
"aggregations": {
"attributes": {
"doc_count": 82662,
"key": {
"doc_count_error_upper_bound": 766,
"sum_other_doc_count": 42982,
"buckets": [
{
"key": "color_family",
"doc_count": 10875,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 2520,
"buckets": [
{
"key": "white",
"doc_count": 1996
},
{
"key": "blue",
"doc_count": 1226
},
{
"key": "brown",
"doc_count": 900
},
{
"key": "green",
"doc_count": 862
},
{
"key": "multicolor",
"doc_count": 752
},
{
"key": "red",
"doc_count": 747
},
{
"key": "gray",
"doc_count": 726
},
{
"key": "silver",
"doc_count": 722
},
{
"key": "pink",
"doc_count": 653
},
{
"key": "yellow",
"doc_count": 527
}
]
}
},
{
"key": "paper_color",
"doc_count": 2158,
"value": {
"doc_count_error_upper_bound": 9,
"sum_other_doc_count": 642,
"buckets": [
{
"key": "white",
"doc_count": 802
},
{
"key": "blue",
"doc_count": 208
},
{
"key": "beige",
"doc_count": 116
},
{
"key": "pink",
"doc_count": 112
},
{
"key": "red",
"doc_count": 112
},
{
"key": "brown",
"doc_count": 107
},
{
"key": "gray",
"doc_count": 102
},
{
"key": "green",
"doc_count": 101
},
{
"key": "bright",
"doc_count": 82
},
{
"key": "silver",
"doc_count": 76
}
]
}
},
]
}
}
}
}

query to select a filter
{
"query": {
"bool": {
"must": {
"multi_match" : {
"query": "tops",
"fields": ["name^12.1", "brand^20.0"]
}
},
"filter": {
"bool":{
"must":
[
{"match":{"attributes.key": "color_family"}},
{"match":{"attributes.value": "white"}}
]
}
}
}
},
"aggs": {
"attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"key": {
"terms": {
"field": "attributes.key"
},
"aggs": {
"value": {
"terms": {
"field": "attributes.value"
}
}
}
}
}
}
}
}

second one gets all the color family and white ones and not the white in color_family

{
"took": 139,
"hits": {
"total": 2002,
"max_score": 102.30364,
"hits": [
......
]
},
"aggregations": {
"attributes": {
"doc_count": 9146,
"key": {
"doc_count_error_upper_bound": 72,
"sum_other_doc_count": 3495,
"buckets": [
{
"key": "color_family",
"doc_count": 2002,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "white",
"doc_count": 1998
},
{
"key": "black",
"doc_count": 1
},
{
"key": "blue",
"doc_count": 1
},
{
"key": "green",
"doc_count": 1
},
{
"key": "multicolor",
"doc_count": 1
},
{
"key": "red",
"doc_count": 1
}
]
}
}
]
}
}
}
}


(system) #4

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