Terms Facet problem with nested document


(esrohj) #1

Hi

I am new in ElasticSearch and trying to make terms facet working.

Here is my document mapping.
"mapping": {
"properties": {
"doc": {
"properties": {
"id": {
},
"top": {
"properties": {
"categoryId": {
"type": "string"
}
}
},
"middle": {
"properties": {
"categoryId": {
"type": "string"
}
}
},
"base": {
"properties": {
"categoryId": {
"type": "string"
}
}
}
}
}
}
}

I have a list of categoryIds and I would like to get facet of those categoryIds from my mapping.
But, I don't know which level individual categoryId belongs to (top or middle or base).
Therefore, I need to search from 3 different inner document.
The below is my query use to get facet but it does not return facets for defined categoryIds.
Let's say that I have 2 documents.
Document1 has "1" as "top" categoryId and "3" as "base" categoryId.
Document2 has "2" as "middle" categoryId and "4" as "base" categoryId.
Document3 has "5" as "top" categoryid.

The facet result seems to return "1", "3", "2" and "4".
I just want to get "1" and "2" as categoryId facet result.

Does anyone know what did I do wrong?

In mapping, I add "include_in_parent=true" and nested type but it did not help.

Thanks.

My facet structured query is like following:
{
"from": 0,
"size": 0,
"query": {
"filtered": {
"query": {
"bool": {
"must": {
"match_all": {}
}
}
},
"filter": {
"or": {
"filters": [
{
"terms": {
"doc.top.categoryId": [
"1", "2"
]
}
},
{
"terms": {
"doc.middle.categoryId": [
"1", "2"
]
}
},
{
"terms": {
"doc.base.categoryId": [
"1", "2"
]
}
}
]
}
}
}
},
"facets": {
"categories_facet": {
"terms": {
"fields": [
"doc.top.categoryId",
"doc.middle.categoryId",
"doc.base.categoryId"
],
"size": 5
},
"facet_filter": {
"or": {
"filters": [
{
"terms": {
"doc.top.categoryId": [
"1","2"
]
}
},
{
"terms": {
"doc.middle.categoryId": [
"1", "2"
]
}
},
{
"terms": {
"doc.base.categoryId": [
"1","2"
]
}
}
]
}
}
}
}
}


(system) #2