# Sorting based on initial term, don't include 2nd, 3rd term etc of the field

**URL:** <https://discuss.elastic.co/t/sorting-based-on-initial-term-dont-include-2nd-3rd-term-etc-of-the-field/165045>\
**Category:** Elasticsearch\
**Created:** [January 21, 2019, 12:28pm UTC](https://discuss.elastic.co/t/sorting-based-on-initial-term-dont-include-2nd-3rd-term-etc-of-the-field/165045 "2019-01-21T12:28:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PUSHKAR\_SINGH](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@PUSHKAR\_SINGH](https://discuss.elastic.co/u/PUSHKAR_SINGH)\
**Post date:** [January 21, 2019, 12:28pm UTC](https://discuss.elastic.co/t/sorting-based-on-initial-term-dont-include-2nd-3rd-term-etc-of-the-field/165045/1 "2019-01-21T12:28:56Z")

</div>

Suppose, we have below Details

## Mapping:=\> { "demo\_index": { "mappings": { "my\_doc": { "properties": { "first\_field": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore\_above": 256 } } }, "second\_field": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore\_above": 256 } } } } } } } }

Data:=\>  
PUT demo\_index/my\_doc/1  
{  
"first\_field":"Alpha Bita Gama",  
"second\_field":"XYZ"  
}  
PUT demo\_index/my\_doc/2  
{  
"first\_field":"Bita1 Gama1 Alpha1",  
"second\_field":"PQR"  
}  
PUT demo\_index/my\_doc/3  
{  
"first\_field":"Gama2 Alpha2 Bita2",  
"second\_field":"KLM"  
}  
PUT demo\_index/my\_doc/4  
{  
"first\_field":"gama3 alpha3 bita3",  
"second\_field":"123"  
}  
PUT demo\_index/my\_doc/5  
{  
"first\_field":"all bita gama",  
"second\_field":"123"  
}

* * *

**Problem:=\> I can not change the mapping, Because It is running on production** , and  
**1. I need to get the records begins with**'Al' or 'al' **(It mean Camel Case, Upper Case, Lower Case record)**.

**2. Result should include initial term starting with 'Al' or 'al' only (It don't include 2nd, 3rd term etc of the field)**  
**3. Based on below query I am getting only records begins with 'Al' not with 'al'**  
Query:=\>  
GET demo\_index/\_search  
{  
"\_source": ["first\_field","second\_field"],  
"query": {  
"query\_string": {  
"fields": ["first\_field.keyword","second\_field"],  
"query": "Al\*"  
}  
}  
}

Please suggest me possible modification in query except change in mapping.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [January 28, 2019, 10:19pm UTC](https://discuss.elastic.co/t/sorting-based-on-initial-term-dont-include-2nd-3rd-term-etc-of-the-field/165045/2 "2019-01-28T22:19:43Z")

</div>

Not at a computer where I can test this, but I think the only way to achieve what you want _without_ a mapping change is to use a regex query and pin the regular expression to the beginning with `^`:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#\_standard\_operators](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#_standard_operators)

That way only the first token in the string matches. Do note that regex queries are relatively more expensive than other queries.

---

<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:** [February 25, 2019, 10:19pm UTC](https://discuss.elastic.co/t/sorting-based-on-initial-term-dont-include-2nd-3rd-term-etc-of-the-field/165045/3 "2019-02-25T22:19:47Z")

</div>

Closed on February 25, 2019, 10:19pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
