# Using Terms Stats Facet on "edgeNGram" key field

**URL:** <https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243>\
**Category:** Elasticsearch\
**Created:** [June 27, 2012, 10:18pm UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243 "2012-06-27T22:18:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 27, 2012, 10:18pm UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/1 "2012-06-27T22:18:27Z")

</div>

Heya,

Full gist is here : [https://gist.github.com/3007168](https://gist.github.com/3007168)

> <https://gist.github.com/dadoonet/3007168>

Let's say I have a document like this :

{  
"id":"212468504455168001",  
"location":{  
"lat":46.20715933,  
"lon":6.14494212  
},  
"lat":46.20715933,  
"lng":6.14494212,  
"path":"1022232003013110123223112",  
"created\_at":1339491413  
}

I defined first an analyzer and a mapping for field path:

```
  "analyzer":{
    "myanalyzer":{
      "type":"custom",
      "tokenizer":"mytokenizer"
    }
  },
  "tokenizer":{
    "mytokenizer":{
      "type":"edgeNGram",
      "min_gram":"1",
      "max_gram":"25",
      "side":"front"
    }
  }

```

Then I defined a mapping for field path:

```
    "path":{"type":"string", "analyzer":"myanalyzer"}

```

As you can see, I apply a EdgeNGram on path field. So my path is broken in  
25 tokens like : 1,10,102,1022,...,1022232003013110123223112

My need is now to compute facets on this field as a key, but only for tokens  
with a size of x (x depends to the user).

So if x is 4, I want to compute on the first 4 characters of my path.

My facet is :

```
"path_lat":{

  "terms_stats":{

    "key_field":"path",

    "value_field":"lat",

    "size":0

  },

  "facet_filter":{

    "geo_bounding_box":{

      "location":{

        "top_left":{ "lat":84.4740645845916, "lon":-179.999999 },

        "bottom_right":{ "lat":-75.67219739055291, "lon":179.999999 }

      }

    }

  }

}

```

My concern is that I get facets for the 25 tokens for each document (see  
result : [https://gist.github.com/3007168#file\_result.json](https://gist.github.com/3007168#file_result.json)  
[https://gist.github.com/3007168#file\_result.json](https://gist.github.com/3007168#file_result.json)). But, I only need to  
compute facet on the edgeNGram with a length of 4.

I imagine that I can create a multifield value with 25 fields (path\_1 to  
path\_25) and applying each time a edgeNGram with a min/max:1,min/max:2, .,  
min/max:25 and then apply my facet on field path\_4 for example, but is there  
a fancier way to do it ?

Thanks for your help

I hope that my description is clear enough :-/

Cheers

David.

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 28, 2012, 7:51am UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/2 "2012-06-28T07:51:20Z")

</div>

> My need is now to compute facets on this field as a key, but only for  
> tokens with a size of x (x depends to the user).

> So if x is 4, I want to compute on the first 4 characters of my path.
> 
> My concern is that I get facets for the 25 tokens for each document  
> (see result : [Using Terms Stats Facet on "edgeNGram" key field · GitHub](https://gist.github.com/3007168#file_result.json)). But,  
> I only need to compute facet on the edgeNGram with a length of 4.

The "terms" facet accepts a "regex" filter, which would allow you to  
restrict results only on terms of length 4.

Unfortunately, this doesn't work on the terms\_stats facet. I'm sure this  
would be easy to implement. Open an issue?

In the meantime, you'll have to do the filtering client side

clint

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 28, 2012, 8:13am UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/3 "2012-06-28T08:13:11Z")

</div>

Many thanks Clint.

Issue is here : [Add regex filter on terms\_stats facet · Issue #2063 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2063)  
[https://github.com/elasticsearch/elasticsearch/issues/2063](https://github.com/elasticsearch/elasticsearch/issues/2063)  
I would like to try to implement it myself. Could you tell me where to start ?

David.

Le 28 juin 2012 à 09:51, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) a écrit :

> > My need is now to compute facets on this field as a key, but only for  
> > tokens with a size of x (x depends to the user).
> 
> > So if x is 4, I want to compute on the first 4 characters of my path.
> > 
> > My concern is that I get facets for the 25 tokens for each document  
> > (see result : [Using Terms Stats Facet on "edgeNGram" key field · GitHub](https://gist.github.com/3007168#file_result.json)). But,  
> > I only need to compute facet on the edgeNGram with a length of 4.
> 
> The "terms" facet accepts a "regex" filter, which would allow you to  
> restrict results only on terms of length 4.
> 
> Unfortunately, this doesn't work on the terms\_stats facet. I'm sure this  
> would be easy to implement. Open an issue?
> 
> In the meantime, you'll have to do the filtering client side
> 
> clint

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 28, 2012, 8:37am UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/4 "2012-06-28T08:37:42Z")

</div>

> Issue is  
> here : [Add regex filter on terms\_stats facet · Issue #2063 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2063)
> 
> I would like to try to implement it myself. Could you tell me where to  
> start ?

Heh - you're asking me? Perl guy? 🙂

I'd start with: grep -r regex src/ | grep facet

clint

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 28, 2012, 10:39am UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/5 "2012-06-28T10:39:48Z")

</div>

> > I would like to try to implement it myself. Could you tell me where to start  
> > ?

> Heh - you're asking me? Perl guy? 🙂

Big LOL 😉

> > I'd start with: grep -r regex src/ | grep facet

Should I say "thanks" ? 😉

Ok. I will try to dig into the code and ask Shay for some advices on how to plug  
this new feature in...

Take care  
David.

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 20, 2012, 10:08pm UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/6 "2012-07-20T22:08:00Z")

</div>

Just an update on this Thread.

Thanks to Clint I found the way to implement it. So there is now a pull request for it here : [Add regex filter on terms\_stats facet by dadoonet · Pull Request #2109 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/2109)

Cheers

David.

De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de David Pilato  
Envoyé : jeudi 28 juin 2012 12:40  
À : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
Objet : Re: Using Terms Stats Facet on "edgeNGram" key field

> > I would like to try to implement it myself. Could you tell me where to start ?

> Heh - you're asking me? Perl guy? 🙂

Big LOL 😉

> > I'd start with: grep -r regex src/ | grep facet

Should I say "thanks" ? 😉

Ok. I will try to dig into the code and ask Shay for some advices on how to plug this new feature in...

Take care

David.

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/) [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<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, 3:19am UTC](https://discuss.elastic.co/t/using-terms-stats-facet-on-edgengram-key-field/8243/7 "2017-07-06T03:19:36Z")

</div>

Closed on July 6, 2017, 3:19am UTC


