Question

I have a facet count problem while using arrays. I have a gist which you guys can have look at in order to see my actual mapping and the documents I'm indexing: https://gist.github.com/3607876 .

Briefly, I'm submitting this query throught the search API:

curl -XPOST 'localhost:9200/org/_search?pretty=true' -d '
{
    "query" : {
        "term" : { "participating-org.role" : "leading" }
    },
    "filter" : {
        "term" : { "participating-org.role" : "leading" }
    },
    "facets" : {
        "organization_facets" : {
            "terms" : { "field" : "participating-org.name" }
        }
    }
}'

and I'm getting back the following facet:

facets: {
    participating-org.name: {
        _type: "terms"
        missing: 0
        total: 8
        other: 0
        terms: [
            {
                term: "def"
                count: 4
            }
            {
                term: "abc"
                count: 4
            }
        ]
    }
}

I wouldn't expect the "def" entry here since its participating-org object has always "leading" role and I'm trying to filter those entries out. I don't know why there is count for "abc" participating-org too which is not in "leading" role.

Do you guys have any suggestions? Is it my mappings or the facets query problems?

Was it helpful?

Solution

You can remove the filter

"filter" : {
    "term" : { "participating-org.role" : "leading" }
}

since it doesn't make any difference. Filters are not taken into account while computing the facet counts, they are only taken into account to filter the search results.

The query

"query" : {
    "term" : { "participating-org.role" : "leading" }
}

does make a difference for both your documents and your facet, but in fact you are asking elasticsearch to give you all the documents with participating-org.role leading, and compute the facets out of those search results. The filter that you're trying to apply is applied to the returned documents, and the returned documents are used to make the facet. Your documents have multiple participating-org inner objects, and of them has always role leading, that's why you're selecting all documents with that query. Yo're filtering the top level documents If you only want to exclude a specific facet entry you can do it, but I think you'd be more interested in the nested documents support. You can have a look at the Nested type, this great article about it and how to make facets baed on nested documents at the bottom of this page.

OTHER TIPS

The solution for this problem is in this gist:
https://gist.github.com/3616612

Result will be now:



    facets: {
        participating-org.role.leading: {
            _type: "terms"
            missing: 0
            total: 4
            other: 0
            terms: [
                {
                    term: "def"
                    count: 4
                }
            ]
        }
    }

Great Solution for me, it works but if you guys have any suggestions, please put down your thoughts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top