# Facet to get all keys from an object in elasticsearch

**URL:** <https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032>\
**Category:** Elasticsearch\
**Created:** [October 20, 2013, 4:28pm UTC](https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032 "2013-10-20T16:28:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LM1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lm1/32/2015_2.png) [@LM1](https://discuss.elastic.co/u/LM1)\
**Post date:** [October 20, 2013, 4:28pm UTC](https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032/1 "2013-10-20T16:28:40Z")

</div>

Let's say I have the following docs:

{  
"title": "Some Title",  
options: {  
"key5": 1,  
"key3": 0,  
"key1": 1,  
}  
},  
{  
"title": "Some Title",  
options: {  
"key2": 0,  
"key3": 0,  
"key5": 1,  
}  
}

I want to get all the keys from options object using facet.

If options was a simple array of keys as strings, I would simple use a  
facet like this:

"facets" : {  
"options" : {  
"terms" : {  
"field" : "options"  
}  
}  
}

But it doesn't work in my case.

So if a query returns those two docs, I should get these keys:  
["key5","key3","key1","key2"]

What kind of facet do I actually need?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 20, 2013, 4:57pm UTC](https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032/2 "2013-10-20T16:57:55Z")

</div>

Hey,

the way documents are stored, are actually different than you might think.  
When your first example document is stored, the fields are flattened to the  
following structure

options.key5 = 1  
options.key3 = 0  
options.key1 = 1

So, actually three fields are stored, instead of the one your are expecting.

A possible solution to this problem is to store an options array, which  
includes "options": ["key5", "key3", "key1"]  
However this does not yield the 0/1 information bit, so maybe this is  
incomplete.

Maybe nested documents and the nested facet could help you as well, but  
this depends on what kind of output you are actually expecting.

Hope this helps.

--Alex

On Sun, Oct 20, 2013 at 6:28 PM, LM [levani9191@gmail.com](mailto:levani9191@gmail.com) wrote:

> Let's say I have the following docs:
> 
> {  
> "title": "Some Title",  
> options: {  
> "key5": 1,  
> "key3": 0,  
> "key1": 1,  
> }  
> },  
> {  
> "title": "Some Title",  
> options: {  
> "key2": 0,  
> "key3": 0,  
> "key5": 1,  
> }  
> }
> 
> I want to get all the keys from options object using facet.
> 
> If options was a simple array of keys as strings, I would simple use a  
> facet like this:
> 
> "facets" : {  
> "options" : {  
> "terms" : {  
> "field" : "options"  
> }  
> }  
> }
> 
> But it doesn't work in my case.
> 
> So if a query returns those two docs, I should get these keys:  
> ["key5","key3","key1","key2"]
> 
> What kind of facet do I actually need?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![LM1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lm1/32/2015_2.png) [@LM1](https://discuss.elastic.co/u/LM1)\
**Post date:** [October 20, 2013, 5:00pm UTC](https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032/3 "2013-10-20T17:00:51Z")

</div>

I guess the optimal way would be to add another key where I store just an array of these keys than…

Thanks for help!

On Oct 20, 2013, at 8:57 PM, Alexander Reelsen [alr@spinscale.de](mailto:alr@spinscale.de) wrote:

> Hey,
> 
> the way documents are stored, are actually different than you might think. When your first example document is stored, the fields are flattened to the following structure
> 
> options.key5 = 1  
> options.key3 = 0  
> options.key1 = 1
> 
> So, actually three fields are stored, instead of the one your are expecting.
> 
> A possible solution to this problem is to store an options array, which includes "options": ["key5", "key3", "key1"]  
> However this does not yield the 0/1 information bit, so maybe this is incomplete.
> 
> Maybe nested documents and the nested facet could help you as well, but this depends on what kind of output you are actually expecting.
> 
> Hope this helps.
> 
> --Alex
> 
> On Sun, Oct 20, 2013 at 6:28 PM, LM [levani9191@gmail.com](mailto:levani9191@gmail.com) wrote:  
> Let's say I have the following docs:
> 
> {  
> "title": "Some Title",  
> options: {  
> "key5": 1,  
> "key3": 0,  
> "key1": 1,  
> }  
> },  
> {  
> "title": "Some Title",  
> options: {  
> "key2": 0,  
> "key3": 0,  
> "key5": 1,  
> }  
> }  
> I want to get all the keys from options object using facet.
> 
> If options was a simple array of keys as strings, I would simple use a facet like this:
> 
> "facets" : {  
> "options" : {  
> "terms" : {  
> "field" : "options"  
> }  
> }  
> }  
> But it doesn't work in my case.
> 
> So if a query returns those two docs, I should get these keys: ["key5","key3","key1","key2"]
> 
> What kind of facet do I actually need?
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/pTTXnXdCDg8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/pTTXnXdCDg8/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:11am UTC](https://discuss.elastic.co/t/facet-to-get-all-keys-from-an-object-in-elasticsearch/14032/4 "2017-07-06T02:11:33Z")

</div>


