# Facet counting on non unique tag collection

**URL:** <https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517>\
**Category:** Elasticsearch\
**Created:** [January 27, 2013, 5:43am UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517 "2013-01-27T05:43:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rookie7799](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@rookie7799](https://discuss.elastic.co/u/rookie7799)\
**Post date:** [January 27, 2013, 5:43am UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/1 "2013-01-27T05:43:38Z")

</div>

Hi,  
I have a pretty simple question (I hope):

I have the following "structure" -  
curl -XPOST '[http://localhost:9200/test/records/](http://localhost:9200/test/records/)' -d '{  
"session\_id" : "abc",  
"user\_id" : 1,  
"timestamp" : 1359245610,  
"coords" : ["1x1", "1x1", "1x1", "4x4", "5x5"]  
}'  
curl -XPOST '[http://localhost:9200/test/records/](http://localhost:9200/test/records/)' -d '{  
"session\_id" : "abc",  
"user\_id" : 1,  
"timestamp" : 1359245610,  
"coords" : ["2x2", "3x3", "1x1", "4x4", "5x5"]  
}'

When I do the following:  
curl -XGET '[http://localhost:9200/test/\_search?pretty=true](http://localhost:9200/test/_search?pretty=true)' -d '{  
"query" : {  
"matchAll" : {}  
},  
"facets" : {  
"somename" : { "terms" : {"field" : "coords"} }  
}  
,"size":0}'

I get:  
{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 2,  
"max\_score" : 1.0,  
"hits" : []  
},  
"facets" : {  
"somename" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 8,  
"other" : 0,  
"terms" : [ {  
"term" : "5x5",  
"count" : 2  
}, {  
"term" : "4x4",  
"count" : 2  
}, {  
"term" : "1x1",  
"count" : 2  
}, {  
"term" : "3x3",  
"count" : 1  
}, {  
"term" : "2x2",  
"count" : 1  
} ]  
}  
}  
}

The question is - how do I make it say that there are actually FOUR "1x1" ?

Thank you!

--

---

<div class="post-metadata">

**Author:** ![rookie7799](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@rookie7799](https://discuss.elastic.co/u/rookie7799)\
**Post date:** [January 28, 2013, 2:53pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/2 "2013-01-28T14:53:34Z")

</div>

I thought this would be an easy thing to solve 🙂

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 28, 2013, 3:46pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/3 "2013-01-28T15:46:54Z")

</div>

Hi,

It the term facets return the number of document that have matched a  
particular term, so 2 is correct in this case.  
I guess in your case (and correct me if I'm wrong) you want to treat  
each coord as a separate hit. If that is the case you  
can use the `nested` field type [1](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html) and use the `nested` option in  
your `somename` facet.

Martijn

On 28 January 2013 15:53, rookie7799 [pavelbaranov@gmail.com](mailto:pavelbaranov@gmail.com) wrote:

> I thought this would be an easy thing to solve 🙂
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

---

<div class="post-metadata">

**Author:** ![rookie7799](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@rookie7799](https://discuss.elastic.co/u/rookie7799)\
**Post date:** [January 28, 2013, 3:59pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/4 "2013-01-28T15:59:19Z")

</div>

Thank you for reply Martijn!

Yes you're correct I want every "string" inside coords to be counted as 1.  
I've tried nested mapping and search but it didn't work for me, the example  
that they have on elasticsearch page refers to the following structure:

"obj1" : [  
{  
"name" : "blue",  
"count" : 4  
},  
{  
"name" : "green",  
"count" : 6  
}  
]

however , in my case I don't even have counts. I tried changing my layout a  
bit as well but it didn't help:  
curl -XPOST '[http://localhost:9200/test/mice/](http://localhost:9200/test/mice/)' -d '{  
"session\_id" : "12345abcde",  
"user\_id" : 1,  
"timestamp" : 1359245610,  
"coords" : [ {"v":"1x1"}, {"v":"1x1"}, {"v":"1x1"}, {"v":"4x4"},  
{"v":"5x5"} ]  
}'

The reason I want to be able to do it in the first place is to save space  
really. I could save ever coordinate in its own recrord but that means  
saving session\_id, user\_id, timestamp as well...

On Monday, January 28, 2013 10:46:54 AM UTC-5, Martijn v Groningen wrote:

> Hi,
> 
> It the term facets return the number of document that have matched a  
> particular term, so 2 is correct in this case.  
> I guess in your case (and correct me if I'm wrong) you want to treat  
> each coord as a separate hit. If that is the case you  
> can use the `nested` field type [1](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html) and use the `nested` option in  
> your `somename` facet.
> 
> Martijn
> 
> On 28 January 2013 15:53, rookie7799 \<[pavelb...@gmail.com](mailto:pavelb...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > I thought this would be an easy thing to solve 🙂
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 28, 2013, 4:36pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/5 "2013-01-28T16:36:55Z")

</div>

> however , in my case I don't even have counts. I tried changing my layout a  
> bit as well but it didn't help:  
> curl -XPOST '[http://localhost:9200/test/mice/](http://localhost:9200/test/mice/)' -d '{  
> "session\_id" : "12345abcde",  
> "user\_id" : 1,  
> "timestamp" : 1359245610,  
> "coords" : [ {"v":"1x1"}, {"v":"1x1"}, {"v":"1x1"}, {"v":"4x4"},  
> {"v":"5x5"} ]  
> }'  
> It should work did you specify the nested type in your mapping (it is  
> not applied automatically)?  
> See my example here: [Nested type and nested facets · GitHub](https://gist.github.com/4657029)

> The reason I want to be able to do it in the first place is to save space  
> really. I could save ever coordinate in its own recrord but that means  
> saving session\_id, user\_id, timestamp as well...  
> I understand, however with using nested you keep the overhead to a  
> minimum (compared to storing each coord as a different document).

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![rookie7799](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@rookie7799](https://discuss.elastic.co/u/rookie7799)\
**Post date:** [January 28, 2013, 4:41pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/6 "2013-01-28T16:41:09Z")

</div>

Yep:  
{

- test:  
{
  - mice:  
{
    - properties:  
{
      - coords:  
{
        - type: "nested",
        - properties:  
{
          - v:  
{
            - type: "string"  
}  
}  
},

      - session\_id:  
{
        - type: "string"  
},

      - timestamp:  
{
        - type: "long"  
},

      - user\_id:  
{
        - type: "long"  
}  
}  
}  
}

}

--

---

<div class="post-metadata">

**Author:** ![rookie7799](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@rookie7799](https://discuss.elastic.co/u/rookie7799)\
**Post date:** [January 28, 2013, 4:52pm UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/7 "2013-01-28T16:52:48Z")

</div>

Oh man, I was using terms\_stats instead 🙂 DUH

Thank you so much!

On Monday, January 28, 2013 11:36:55 AM UTC-5, Martijn v Groningen wrote:

> > however , in my case I don't even have counts. I tried changing my  
> > layout a  
> > bit as well but it didn't help:  
> > curl -XPOST '[http://localhost:9200/test/mice/](http://localhost:9200/test/mice/)' -d '{  
> > "session\_id" : "12345abcde",  
> > "user\_id" : 1,  
> > "timestamp" : 1359245610,  
> > "coords" : [ {"v":"1x1"}, {"v":"1x1"}, {"v":"1x1"}, {"v":"4x4"},  
> > {"v":"5x5"} ]  
> > }'  
> > It should work did you specify the nested type in your mapping (it is  
> > not applied automatically)?  
> > See my example here: [Nested type and nested facets · GitHub](https://gist.github.com/4657029)
> 
> > The reason I want to be able to do it in the first place is to save  
> > space  
> > really. I could save ever coordinate in its own recrord but that means  
> > saving session\_id, user\_id, timestamp as well...  
> > I understand, however with using nested you keep the overhead to a  
> > minimum (compared to storing each coord as a different document).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

---

<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:54am UTC](https://discuss.elastic.co/t/facet-counting-on-non-unique-tag-collection/10517/8 "2017-07-06T02:54:22Z")

</div>


