Searching boolean keys within nested object in Elasticsearch


(Sachin) #1

I have "user" object and it has nested object "status" which has few boolean flags. I am able to search with name and phone values. I need to search all the users which has that boolean flag. For example search all users which has flag1 status set. Preferable solution will be during indexing time. Just FYI I am using couchdb river to populate the data.

Data structure -
user:{ name: ABC, phone: 1234567890, status: { flag1: true, flag9: true } }


(Emptyemail) #2

you will need to use a nested filter, note the 'status.flag1'

{nested: {path: 'status',filter: {term: 'status.flag1': true}}}

You might want to consider using regular objects rather than nesting in this case, I've found that nesting only helps if you are planning to update the nested objects separately and much more frequently than the main object.


(Sachin) #3

Thanks for reply, in this case yes nested object will be updated more often. Just confirming during indexing in mapping I should set the filter on nested object and then if I search for flag1 I will get appropriate results. correct?


(Emptyemail) #4

Your mapping document should say that status property is a nested property, during indexing you are just indexing the document as is. You would use the nested filter as part of your query for the user objects. If that doesn't work it might be easier if you post relevant parts of your mapping document along with a sample query.


(Sachin) #5

Thank You so much, I got it working using nested mapping and nested filter.

However, is there a way if we enter flag1 as query term and get all the user objects which has flag1?


(Emptyemail) #6

If you just use the nested query then that's what you should get back


(system) #7