Question

I have the following mapping:

curl -XPUT 'http://localhost:9200/bookstore/user/_mapping' -d '
{
  "user": {
    "properties": {
      "user_id": { "type": "integer" },
      "gender": { "type": "string", "index" : "not_analyzed" },
      "age": { "type": "integer" },
      "age_bracket": { "type": "string", "index" : "not_analyzed" },
      "current_city": { "type": "string", "index" : "not_analyzed" },
      "relationship_status": { "type": "string", "index" : "not_analyzed" },
      "books" : {
        "type": "nested",
        "properties" : {
          "b_oid": { "type": "string", "index" : "not_analyzed" },
          "b_name": { "type": "string", "index" : "not_analyzed" },
          "bc_id": { "type": "integer" },
          "bc_name": { "type": "string", "index" : "not_analyzed" },
          "bcl_name": { "type": "string", "index" : "not_analyzed" },
          "b_id": { "type": "integer" }
        }
      }
    }
  }
}'

Now, I try to query for example for Users which have "gender": "Male", have bought book in a certain category "bcl_name": "Trivia" and show the "b_name" book titles. I somehow cannot get it to run.

I have the query

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
    "size": 0,
    "from": 0,
    "query": {
     "filtered": {
         "query": {
             "terms": {
                 "gender": [
                     "Male"
                 ]
             }
         }
     }
    },
    "facets": {
        "CategoryFacet": {
             "terms": {
                 "field": "books.b_name",
                 "size": 5,
                 "shard_size": 1000,
                 "order": "count"
             },
             "nested": "books",
             "facet_filter": {
                 "terms": {
                     "books.bcl_name": [
                         "Trivia"
                     ]
                 }
             }
        }
    }
}'

which returns a result, but I'm not sure whether this is correct. I looked for some examples, and found this (http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/) for example. I'm able to rewrite my query like this:

curl -XGET 'http://localhost:9200/bookstore/user/_search?pretty=1' -d '{
    "size": 0,
    "from": 0,
    "query": {
     "filtered": {
         "query": {
             "terms": {
                 "gender": [
                     "Male"
                 ]
             }
         },
         "filter": {
             "nested": {
                 "path": "books",
                 "query": {
                     "filtered": {
                         "query": {
                             "match_all": {}
                         },
                         "filter": {
                             "and": [
                                 {
                                     "term": {
                                         "books.bcl_name": "Trivia"
                                     }
                                 }
                             ]
                         }
                     }
                 }
             }
         }
     }
    },
    "facets": {
     "CategoryFacet": {
         "terms": {
             "field": "books.b_name",
             "size": 5,
             "shard_size": 1000,
             "order": "count"
         },
         "nested": "books"
     }
    }
}'

which shows different results.

I, as a beginner, am a litte lost right now. Can someone please give me hint on how to solve this`? Thanks a lot in advance!

Was it helpful?

Solution

First query means:

  • Search for users whose gender : "Male"
  • But "CategoryFacet" includes the count of gender : "Male" AND books.bcl_name : "Trivia"

So in result set you get all "Male" users, but your CategoryFacet gives you the count of "Male users AND whose books.bcl_name is Trivia".

In second query your "CategoryFacet" does not include extra filtering. It just returns the facets from the exact result set.

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