# Elasticsearch “ends with” word in phrases

**URL:** <https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473>\
**Category:** Elasticsearch\
**Created:** [September 14, 2016, 9:15am UTC](https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473 "2016-09-14T09:15:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)\
**Post date:** [September 14, 2016, 9:15am UTC](https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473/1 "2016-09-14T09:15:34Z")

</div>

Hi All,

I try to implement a query from my content with Elasticsearch 2.4  
What I need, is displaying all results which ends with e.g. 154, 024... etc.

I've tried:

GET rd\_product\_lpv1/\_search  
{  
"query": {  
"match\_phrase\_prefix": {  
"STD\_EXTRN\_CODE.STD\_EXTRN\_CODE\_ENDS": {  
"query": "154",  
"max\_expansions": 5  
}  
}  
}  
}

The query mentioned above display results, where within the value begins with "154"  
**Example:**

"STD\_EXTRN\_CODE": "15400007118021",

I want to display results where the value ends with 154  
**sample code:-**  
"STD\_EXTRN\_CODE": "02100007118154",  
"STD\_EXTRN\_CODE": "02101231232024",

Here the settings & mappings I use:

```
PUT /rd_product_lpv1
{
   "settings": {
      "analysis": {
         "analyzer": {
            "analyzer_endswith":{
                "tokenizer": "keyword",
                "type": "standard",
                "filter" : [
                    "lowercase"
                ]
            }               
         }
     }
   },
   "mappings": {
    "properties": {
        "STD_EXTRN_CODE": {
          "type": "string",
           "fields": {
             "STD_EXTRN_CODE_RAW": {
                "type": "string",
                "index": "not_analyzed",
                "norms":{
                    "enabled": false
                },
                "fielddata":{
                    "format": "disabled"
                }
            },
            "STD_EXTRN_CODE_CONTAINS": {
                "type": "string",
                "analyzer": "str_index_analyzer",
                "fielddata":{
                    "format": "disabled"
                }
              },
            "STD_EXTRN_CODE_ENDS": {
                "type": "string",
                "analyzer": "analyzer_endswith",
                "fielddata":{
                    "format": "disabled"
                }
            }
          }
        }
    }
  }
}

```

Here the sample data,

```
POST rd_product_lpv1/product_lpv/1
{
  "STD_EXTRN_CODE": "15400007118021"
}

POST rd_product_lpv1/product_lpv/1
{
  "STD_EXTRN_CODE": "02100007118154"
}

```

Please let me know your thoughts

Thanks,  
Ganeshbabu R

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [September 14, 2016, 9:28am UTC](https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473/2 "2016-09-14T09:28:19Z")

</div>

Hi,

what I've seen people do in slightly similar cases is reverse the token that they want to search the suffix for using a [reverse token filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-reverse-tokenfilter.html). Then you can use the prefix search on those reversed fields and it will behave like suffix search. Be aware that you need to use the same analyzer at search time, but by default queries will use the analyzer defined in the field mapping, so you should be okay if you don't specify an explicit `search_analyzer`.

---

<div class="post-metadata">

**Author:** ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)\
**Post date:** [September 14, 2016, 12:23pm UTC](https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473/3 "2016-09-14T12:23:10Z")

</div>

Hi @cbuescher

Thanks for the clarification..

I modified the analyzer with the **reverse** token filter,

```
     "analyzer": {
        "analyzer_endswith":{
            "tokenizer": "keyword",
            "type": "standard",
            "filter" : [
                "lowercase",
                "reverse"
            ]
        } 

```

Its worked as expected !!

Thanks,  
Ganeshbabu R

---

<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 5, 2017, 10:20pm UTC](https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473/4 "2017-07-05T22:20:18Z")

</div>


