# Facets resulting in objects instead of primitive types

**URL:** <https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227>\
**Category:** Elasticsearch\
**Created:** [May 5, 2015, 2:47pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227 "2015-05-05T14:47:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [May 5, 2015, 2:47pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/1 "2015-05-05T14:47:29Z")

</div>

Hi fellows

I checked this forum and stackoverflow but couldn't find a matching discussion while thinking this must arise to someone before already. So I'd be happy for any help. Let's come to the point:

I want to perform a search with facet calculation returning objects as facets insteads of single values.

My mapping looks as follows (excerpt):

```
"myConfiguration": {"properties": {"paint": {"properties": {"code": {"type": "string"},"text": {"type": "string"}}}}}

```

My query currently looks like this:

```
{
  "facets": {
    "tag": {
      "terms": { "field": "myConfiguration.paint.code" }
} } }

```

This works for the case of retrieving all paint codes used in my data set.

```
{
  "facets": {
    "tag": {
      "_type": "terms", "missing": 1, "total": 525, "other": 0,
      "terms": [
        {
          "term": "4",
          "count": 20
        },
        {
          "term": "2",
          "count": 10
        },
        {
          "term": "5",
          "count": 3
        }
      ] } } }

```

However, the text is what I am also interested in. What I rather want to have is something like the following:

```
{
  "facets": {
    "tag": {
      "_type": "terms", "missing": 1, "total": 525, "other": 0,
      "terms": [
        {
          "term": {"code":"4", "text":"yellow"},
          "count": 20
        },
        {
          "term": {"code":"2", "text":"black"},
          "count": 10
        },
        {
          "term": {"code":"5", "text":"white"},
          "count": 3
        }
      ] } } }

```

Is this possible at all and if yes how can I achieve that? Thanks in advance!!!  
Stefan

(keyword: facets, aggregation, term, query, search)

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 5, 2015, 3:20pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/2 "2015-05-05T15:20:34Z")

</div>

Hi Stefan, i dont know the version of your elasticsearch but dont use anymore the facets (they are deprecated) use the aggregations.

and for your example you can use the sub\_aggregations, your query will be more complex and the answer also, but you can get all the information in one query.

i let you check [http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html](http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html) for the nested,

and [http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html](http://www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html) this for get some examples of sub\_aggregations

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 5, 2015, 3:28pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/3 "2015-05-05T15:28:23Z")

</div>

if you read this link and you dont arrive to made your query, you can send some examples with the \*put request for your mapping and data, and i can try to help you to made this query.

Example how send the examples with your mapping and data :

```
PUT index/type
{
    "type" : {
        "properties" : {
            "foo" : {"type" : "string", "store" : true }
        }
    }
}
PUT index/type/1
    {
      "foo": "bar"
    }
    GET index/type/_search
    {
      "query": {
        "match": {
          "foo": "bar"
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [May 5, 2015, 3:36pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/4 "2015-05-05T15:36:31Z")

</div>

Hi Camilo\_Sierra,

thanks for you immediate response! I will definitely take a look at your suggestions.

Unfortunately at the moment I stuck with "facets" since my 'problem query' is so-to-say an extension of an existing query with facets. Recreating everything in terms of aggregation is quite an effort.

Anyhow, thanks a lot for your response and offer. If I have further questions I am happy that I can ask you again.

Cheers, Stefan

---

<div class="post-metadata">

**Author:** ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)\
**Post date:** [May 5, 2015, 3:51pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/5 "2015-05-05T15:51:57Z")

</div>

i never made one request like that with facets, sorry @sjtuner ! anyway tried to migrate, the aggregations are really amazing, faster...etc

good luck

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [May 5, 2015, 5:21pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/6 "2015-05-05T17:21:02Z")

</div>

You really should use aggregations as @Camilo_Sierra recommended. It is very little effort to convert from facets to aggregations and will be well worth the investment. If you can't do that your only option would be using script fields [1] to join the values together then use that field [2] during faceting so you would get values like:

"yellow\_4", count = 20  
"black\_2", count = 10  
"white\_5", count = 3

Then at runtime you can re-split the values back up if needed.

[1] [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html)  
[2] [http://www.elastic.co/guide/en/elasticsearch/reference/current/search-facets-terms-facet.html#\_script\_field](http://www.elastic.co/guide/en/elasticsearch/reference/current/search-facets-terms-facet.html#_script_field)

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [May 5, 2015, 10:04pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/7 "2015-05-05T22:04:02Z")

</div>

The script fields are actually something I have seen too and the approach you mentioned, mattweber, came up to mind mind as well. In the end I just failed to make that work. Maybe you can give me some practical hints?

Anyhow, I will definetly also investigate into migrating to aggregations.

Thanks again for you help. You have an awesome response time.

Stefan

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [May 6, 2015, 12:43am UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/8 "2015-05-06T00:43:11Z")

</div>

This should work @sjtuner

```
{"query":{"match_all": {}}, 
 "facets":{
     "paints": {
         "terms": {
             "script":"_source.paint.text + \"_\" + _source.paint.code"}}}}

```

Make sure you have scripting enabled.

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [May 12, 2015, 9:52am UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/9 "2015-05-12T09:52:23Z")

</div>

That actually worked for me, thanks a lot. Anyway, I will also check out aggregations. 😄

Best regards, Stefan

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [May 18, 2015, 11:35am UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/10 "2015-05-18T11:35:51Z")

</div>

Hi @Camilo_Sierra,

in the meantime I migrated to aggregations. It surprisingly easy and even more diffcult-appearing aggs were implemented pretty fast 🙂

Thanks again for your help.

Stefan

PS: I have another question towards aggregations but I will ask this in a different thread 😉

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [June 4, 2015, 3:03pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/11 "2015-06-04T15:03:53Z")

</div>

Hi @Camilo_Sierra and friends 😄

unfortunately it turned out that my first solution did actually not exactly what I had in mind. Now i'd kindly take your offer for more help. Lets consider the following example (requests are minified to keep this post's size limited)

```
PUT index/a/_mapping
{"a":{"properties":{"b":{"properties":{"c":{"properties":{"code":{"type":"string"},"text":{"type":"string"}}}}}}}}

PUT index/a/1
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"456","text":"def"}]}}}

PUT index/a/2
{"a":{"b":{"c":[{"code":"123","text":"abc"},{"code":"789","text":"ghi"}]}}}

```

What I want to have is all available c-elements, i.e., code as well as related text. My aggregation looks as follows:

```
POST index/a/_search
{"size":0,"aggs":{"allCCodes":{"terms":{"field":"a.b.c.code"},"aggs":{"allCTexts":{"terms":{"field":"a.b.c.text"}}}}}}

```

Unfortunately it returned the following:  
123 ---\> abc, def, ghi  
456 ---\> abc, def  
789 ---\> abc, ghi

What I want  
123 ---\> abc  
456 ---\> def  
789 ---\> ghi

Any ideas?

Best regards and wishes,  
Stefan

---

<div class="post-metadata">

**Author:** ![sjtuner](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@sjtuner](https://discuss.elastic.co/u/sjtuner)\
**Post date:** [June 4, 2015, 6:21pm UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/12 "2015-06-04T18:21:44Z")

</div>

Well, since this is at least partially new stuff, a new thread makes more sense here. I close this ticket therefore and create a new one. Thanks for your help again!!

---

<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, 12:09am UTC](https://discuss.elastic.co/t/facets-resulting-in-objects-instead-of-primitive-types/227/13 "2017-07-06T00:09:34Z")

</div>


