# How to get Alphabetic Facet (A-Z) on name field in elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346
**Category:** Elasticsearch
**Created:** [March 28, 2013, 8:27am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346 "2013-03-28T08:27:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Maaz\_Bin\_Tariq](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Maaz\_Bin\_Tariq](https://discuss.elastic.co/u/Maaz_Bin_Tariq)
#### Post date: [March 28, 2013, 8:27am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/1 "2013-03-28T08:27:23Z")

</div>

Hi,

I have name field in my documents and I want to get facets on the base of  
alphabets (first letter) instead of full name.

For example:

If I have docs with following name

1. Maaz
2. John
3. Sarah
4. Symonds

The facets response should be something like  
{  
"facets": {  
"name": {  
"\_type": "terms",  
"missing": 0,  
"total": 4,  
"other": 0,  
"terms": [  
{  
"term": "S",  
"count": 2  
},  
{  
"term": "M",  
"count": 1  
},  
{  
"term": "J",  
"count": 1  
}  
}

Is it possible to doing using elastic search term facets or I have to do  
it pragmatically?

--  
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: [March 28, 2013, 8:49am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/2 "2013-03-28T08:49:01Z")

</div>

Hey there,

you can use a term facet with scripting, sample:

curl -X PUT localhost:9200/test/test/1 -d '{ "name":"Alex" }'  
curl -X PUT localhost:9200/test/test/2 -d '{ "name":"Berta" }'  
curl -X PUT localhost:9200/test/test/3 -d '{ "name":"Caesar" }'  
curl -X PUT localhost:9200/test/test/4 -d '{ "name":"Andre" }'

curl -X POST 'localhost:9200/test/test/\_search?pretty' -d '{"query": {  
"match\_all": {}} , "facets" : { "myName" : { "terms" : { "field":"name",  
"script" : "term[0]" } } } }'

Result of the facet:

"facets" : {  
"myName" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 4,  
"other" : 0,  
"terms" : [ {  
"term" : "a",  
"count" : 2  
}, {  
"term" : "c",  
"count" : 1  
}, {  
"term" : "b",  
"count" : 1  
} ]  
}  
}

Hope it helps... Note, that scripting is of course not that fast, as if you  
had stored a field, which only contains the first letter...  
For more information, check out the 'Term scripts' part at

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--Alex

On Thu, Mar 28, 2013 at 9:27 AM, Maaz Bin Tariq [maaz786@gmail.com](mailto:maaz786@gmail.com) wrote:

> Hi,
> 
> I have name field in my documents and I want to get facets on the base of  
> alphabets (first letter) instead of full name.
> 
> For example:
> 
> If I have docs with following name
> 
> 1. Maaz
> 2. John
> 3. Sarah
> 4. Symonds
> 
> The facets response should be something like  
> {  
> "facets": {  
> "name": {  
> "\_type": "terms",  
> "missing": 0,  
> "total": 4,  
> "other": 0,  
> "terms": [  
> {  
> "term": "S",  
> "count": 2  
> },  
> {  
> "term": "M",  
> "count": 1  
> },  
> {  
> "term": "J",  
> "count": 1  
> }  
> }
> 
> Is it possible to doing using Elasticsearch term facets or I have to do  
> it pragmatically?
> 
> --  
> 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: ![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: [March 28, 2013, 8:56am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/3 "2013-03-28T08:56:31Z")

</div>

Hey,

just a small, but important note on my sample. I ran this on an analyzed  
field, which you should not do. Make sure you run this on a not\_analyzed  
field, otherwise your facet will return values for each term.

--Alex

On Thu, Mar 28, 2013 at 9:49 AM, Alexander Reelsen [alr@spinscale.de](mailto:alr@spinscale.de) wrote:

> Hey there,
> 
> you can use a term facet with scripting, sample:
> 
> curl -X PUT localhost:9200/test/test/1 -d '{ "name":"Alex" }'  
> curl -X PUT localhost:9200/test/test/2 -d '{ "name":"Berta" }'  
> curl -X PUT localhost:9200/test/test/3 -d '{ "name":"Caesar" }'  
> curl -X PUT localhost:9200/test/test/4 -d '{ "name":"Andre" }'
> 
> curl -X POST 'localhost:9200/test/test/\_search?pretty' -d '{"query": {  
> "match\_all": {}} , "facets" : { "myName" : { "terms" : { "field":"name",  
> "script" : "term[0]" } } } }'
> 
> Result of the facet:
> 
> "facets" : {  
> "myName" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 4,  
> "other" : 0,  
> "terms" : [ {  
> "term" : "a",  
> "count" : 2  
> }, {  
> "term" : "c",  
> "count" : 1  
> }, {  
> "term" : "b",  
> "count" : 1  
> } ]  
> }  
> }
> 
> Hope it helps... Note, that scripting is of course not that fast, as if  
> you had stored a field, which only contains the first letter...  
> For more information, check out the 'Term scripts' part at  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-facet/)
> 
> --Alex
> 
> On Thu, Mar 28, 2013 at 9:27 AM, Maaz Bin Tariq [maaz786@gmail.com](mailto:maaz786@gmail.com) wrote:
> 
> > Hi,
> > 
> > I have name field in my documents and I want to get facets on the base of  
> > alphabets (first letter) instead of full name.
> > 
> > For example:
> > 
> > If I have docs with following name
> > 
> > 1. Maaz
> > 2. John
> > 3. Sarah
> > 4. Symonds
> > 
> > The facets response should be something like  
> > {  
> > "facets": {  
> > "name": {  
> > "\_type": "terms",  
> > "missing": 0,  
> > "total": 4,  
> > "other": 0,  
> > "terms": [  
> > {  
> > "term": "S",  
> > "count": 2  
> > },  
> > {  
> > "term": "M",  
> > "count": 1  
> > },  
> > {  
> > "term": "J",  
> > "count": 1  
> > }  
> > }
> > 
> > Is it possible to doing using Elasticsearch term facets or I have to do  
> > it pragmatically?
> > 
> > --  
> > 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: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [March 28, 2013, 10:27am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/4 "2013-03-28T10:27:43Z")

</div>

On Thu, 2013-03-28 at 09:49 +0100, Alexander Reelsen wrote:

> Hey there,

> curl -X POST 'localhost:9200/test/test/\_search?pretty' -d '{"query":  
> { "match\_all": {}} , "facets" : { "myName" : { "terms" :  
> { "field":"name", "script" : "term[0]" } } } }'

While this might work, it's always more efficient to prepare your data  
according to your needs, rather than trying to bolt things on  
afterwards. It'll perform better and use less memory.

So if you want to sort on the first letter of a field, then index the  
first letter into a different field.

You could even use multi-fields for this, eg the "name" field is  
analyzed per usual, and the "name.first\_letter" just indexes the first  
letter of the name.

# Create an index using the pattern tokenizer to capture just

# the first letter of each name string

# the main "name" field uses the default analyzer, and the

# "name.first\_letter" field uses our custom analyzer

curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
{  
"settings" : {  
"analysis" : {  
"analyzer" : {  
"first\_letter" : {  
"filter" : [  
"lowercase"  
],  
"tokenizer" : "first\_letter"  
}  
},  
"tokenizer" : {  
"first\_letter" : {  
"pattern" : "^(.).\*$",  
"group" : 1,  
"type" : "pattern"  
}  
}  
}  
},  
"mappings" : {  
"test" : {  
"properties" : {  
"name" : {  
"fields" : {  
"name" : {  
"type" : "string"  
},  
"first\_letter" : {  
"type" : "string",  
"analyzer" : "first\_letter"  
}  
},  
"type" : "multi\_field"  
}  
}  
}  
}  
}  
'

# Index some data

curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{ "name" : "Alex" }  
'  
curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{ "name" : "Berta" }  
'  
curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{ "name" : "Caesar" }  
'  
curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
{ "name" : "Andre" }  
'

# Facet on the "name.first\_letter" fields

curl -XGET  
'[http://127.0.0.1:9200/test/test/\_search?pretty=1&search\_type=count](http://127.0.0.1:9200/test/test/_search?pretty=1&search_type=count)' -d  
'  
{  
"facets" : {  
"first\_letter" : {  
"terms" : {  
"field" : "name.first\_letter"  
}  
}  
}  
}  
'

# {

# "hits" : {

# "hits" : ,

# "max\_score" : 0,

# "total" : 4

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "facets" : {

# "first\_letter" : {

# "other" : 0,

# "terms" : [

# {

# "count" : 2,

# "term" : "a"

# },

# {

# "count" : 1,

# "term" : "c"

# },

# {

# "count" : 1,

# "term" : "b"

# }

# ],

# "missing" : 0,

# "\_type" : "terms",

# "total" : 4

# }

# },

# "took" : 1

# }

# And search on the main "name" field:

curl -XGET '[http://127.0.0.1:9200/test/test/\_search?pretty=1](http://127.0.0.1:9200/test/test/_search?pretty=1)' -d '  
{  
"query" : {  
"match" : {  
"name" : "alex"  
}  
}  
}  
'

# {

# "hits" : {

# "hits" : [

# {

# "\_source" : {

# "name" : "Alex"

# },

# "\_score" : 0.30685282,

# "\_index" : "test",

# "\_id" : "CyJn4ixDRwy17Yl-UxSJwQ",

# "\_type" : "test"

# }

# ],

# "max\_score" : 0.30685282,

# "total" : 1

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "took" : 4

# }

clint

--  
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: ![Maaz\_Bin\_Tariq](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Maaz\_Bin\_Tariq](https://discuss.elastic.co/u/Maaz_Bin_Tariq)
#### Post date: [March 28, 2013, 11:59am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/5 "2013-03-28T11:59:22Z")

</div>

Thanks alot for the help.

On Thu, Mar 28, 2013 at 3:27 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> On Thu, 2013-03-28 at 09:49 +0100, Alexander Reelsen wrote:
> 
> > Hey there,
> 
> > curl -X POST 'localhost:9200/test/test/\_search?pretty' -d '{"query":  
> > { "match\_all": {}} , "facets" : { "myName" : { "terms" :  
> > { "field":"name", "script" : "term[0]" } } } }'
> 
> While this might work, it's always more efficient to prepare your data  
> according to your needs, rather than trying to bolt things on  
> afterwards. It'll perform better and use less memory.
> 
> So if you want to sort on the first letter of a field, then index the  
> first letter into a different field.
> 
> You could even use multi-fields for this, eg the "name" field is  
> analyzed per usual, and the "name.first\_letter" just indexes the first  
> letter of the name.
> 
> # Create an index using the pattern tokenizer to capture just
> 
> # the first letter of each name string
> 
> # the main "name" field uses the default analyzer, and the
> 
> # "name.first\_letter" field uses our custom analyzer
> 
> curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
> {  
> "settings" : {  
> "analysis" : {  
> "analyzer" : {  
> "first\_letter" : {  
> "filter" : [  
> "lowercase"  
> ],  
> "tokenizer" : "first\_letter"  
> }  
> },  
> "tokenizer" : {  
> "first\_letter" : {  
> "pattern" : "^(.).\*$",  
> "group" : 1,  
> "type" : "pattern"  
> }  
> }  
> }  
> },  
> "mappings" : {  
> "test" : {  
> "properties" : {  
> "name" : {  
> "fields" : {  
> "name" : {  
> "type" : "string"  
> },  
> "first\_letter" : {  
> "type" : "string",  
> "analyzer" : "first\_letter"  
> }  
> },  
> "type" : "multi\_field"  
> }  
> }  
> }  
> }  
> }  
> '
> 
> # Index some data
> 
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> { "name" : "Alex" }  
> '  
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> { "name" : "Berta" }  
> '  
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> { "name" : "Caesar" }  
> '  
> curl -XPOST '[http://127.0.0.1:9200/test/test?pretty=1](http://127.0.0.1:9200/test/test?pretty=1)' -d '  
> { "name" : "Andre" }  
> '
> 
> # Facet on the "name.first\_letter" fields
> 
> curl -XGET  
> '[http://127.0.0.1:9200/test/test/\_search?pretty=1&search\_type=count](http://127.0.0.1:9200/test/test/_search?pretty=1&search_type=count)' -d  
> '  
> {  
> "facets" : {  
> "first\_letter" : {  
> "terms" : {  
> "field" : "name.first\_letter"  
> }  
> }  
> }  
> }  
> '
> 
> # {
> 
> # "hits" : {
> 
> # "hits" : ,
> 
> # "max\_score" : 0,
> 
> # "total" : 4
> 
> # },
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "failed" : 0,
> 
> # "successful" : 5,
> 
> # "total" : 5
> 
> # },
> 
> # "facets" : {
> 
> # "first\_letter" : {
> 
> # "other" : 0,
> 
> # "terms" : [
> 
> # {
> 
> # "count" : 2,
> 
> # "term" : "a"
> 
> # },
> 
> # {
> 
> # "count" : 1,
> 
> # "term" : "c"
> 
> # },
> 
> # {
> 
> # "count" : 1,
> 
> # "term" : "b"
> 
> # }
> 
> # ],
> 
> # "missing" : 0,
> 
> # "\_type" : "terms",
> 
> # "total" : 4
> 
> # }
> 
> # },
> 
> # "took" : 1
> 
> # }
> 
> # And search on the main "name" field:
> 
> curl -XGET '[http://127.0.0.1:9200/test/test/\_search?pretty=1](http://127.0.0.1:9200/test/test/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "match" : {  
> "name" : "alex"  
> }  
> }  
> }  
> '
> 
> # {
> 
> # "hits" : {
> 
> # "hits" : [
> 
> # {
> 
> # "\_source" : {
> 
> # "name" : "Alex"
> 
> # },
> 
> # "\_score" : 0.30685282,
> 
> # "\_index" : "test",
> 
> # "\_id" : "CyJn4ixDRwy17Yl-UxSJwQ",
> 
> # "\_type" : "test"
> 
> # }
> 
> # ],
> 
> # "max\_score" : 0.30685282,
> 
> # "total" : 1
> 
> # },
> 
> # "timed\_out" : false,
> 
> # "\_shards" : {
> 
> # "failed" : 0,
> 
> # "successful" : 5,
> 
> # "total" : 5
> 
> # },
> 
> # "took" : 4
> 
> # }
> 
> clint
> 
> --  
> 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/a\_KmS6rns2Y/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/a_KmS6rns2Y/unsubscribe?hl=en-US)  
> .  
> 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:43am UTC](https://discuss.elastic.co/t/how-to-get-alphabetic-facet-a-z-on-name-field-in-elasticsearch/11346/6 "2017-07-06T02:43:55Z")

</div>


