Question

I have the following JSON structures which I want to run an aggregation query in ElasticSearch upon:

{
    "user_id": 1,
    "gender_id": 1,
    "locale_id": 6,
    "age": 38,
    "hometown_city_id": 1002,
    "current_city_id": 672,
    "books": [
        {
            "b_id": 2065,
            "b_name": "aut qui assumenda ut",
            "b_cat": 56
        },
        {
            "b_id": 2527,
            "b_name": "libero et laudantium",
            "b_cat": 132
        },
        ...
    ]
}

What I basically want to do is for example to show the top 5 books the users with "gender_id": "1" (male) have read which also read the book with the "b_id": 2065.

Being a beginner with ElasticSearch I didn't come across a solution yet. I know there's the Aggegation Module (https://github.com/elasticsearch/elasticsearch/issues/3300) coming with v1.0, but I couldn't get it running.

If somebody already has implemented something similiar, please help! Thanks a lot in advance!

Was it helpful?

Solution 2

Thanks @mconlin for the hint. The query I got it working is the following:

{
    "query": {
        "filtered": {
            "filter": {
                "and" : [
                    { "term": { "gender_id": 1 } },
                    { "term": { "books.b_id": 2065} }
                ]
            }
        }
    },
    "facets": {
        "book_cnt": {
            "terms": {
                "field": "books.b_id",
                "size": 5
            }
        }
    }
}

Turns out it's better to use the filter in the query itself instead of as a separate facet.

OTHER TIPS

maybe something like this (not sure about the nesting books.field):

{
    "query" : {
        "match_all" : {  }
    },
    "facets" : {
        "filter_one" : {
            "filter" : {
                "and" : [
                        {"filter" : {"term" : { "gender_id" : 1 }},
                        {"filter" : {"term" : { "books.b_id" : 2065 }},
                ]
            }
        },
        "book_cnt" : {
          "terms" : {
                "field" : "books.b_name",
                "size" : 5
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top