# Startswith analyzer not working

**URL:** https://discuss.elastic.co/t/startswith-analyzer-not-working/172394
**Category:** Elasticsearch
**Created:** [March 14, 2019, 5:02pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394 "2019-03-14T17:02:34Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Mohsin\_Farooq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsin_farooq/32/42025_2.png) [@Mohsin\_Farooq](https://discuss.elastic.co/u/Mohsin_Farooq)
#### Post date: [March 14, 2019, 5:02pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/1 "2019-03-14T17:02:35Z")

</div>

So i just created a new index to search for a string which starts with some specific characters like 'Kibana\*' (just like 'Kibana%' in SQL) which means it should return results with text "Starting with" Kibana and not "Contains" Kibana,, for that i specified an anaylzer for my field "business"

Here is my new index mapping:

```
   PUT stg_sdmr_three
{
     "settings": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
    },
    "mappings" : {
        "_doc" : {
          "properties" : {            
          
          "@version" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "business" : {
            "type" : "text",
            "analyzer" : "standard",
            "search_analyzer" : "analyzer_startswith",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 120
              }
            }
          }
          
        }
        }
    }
}

```

Sample Data for searching:

```
PUT stg_sdmr_three/_doc/1
{
    "@version" : "1",
    "business" : "Jeff Nomura "
  
}

PUT stg_sdmr_three/_doc/2
{
    "@version" : "1",
    "business" : "HORIKAWAYA NOMURA "
  
}

PUT stg_sdmr_three/_doc/3
{
    "@version" : "1",
    "business" : "NOMURA CONSULTING "
  
}

```

now i am getting all three results in response,, where i want only the third one  
"NOMURA CONSULTING" , as its starts with "nomura"

Here is my query :

```
{
  "query": {          
            "prefix": {
              "business": "nomura"
            }    
          }
    
}

```

i have tried prefix, match\_phrase\_prefix & wildcard also but nothing helps

---

<div class="post-metadata">

### Author: ![Mohsin\_Farooq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsin_farooq/32/42025_2.png) [@Mohsin\_Farooq](https://discuss.elastic.co/u/Mohsin_Farooq)
#### Post date: [March 14, 2019, 5:14pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/2 "2019-03-14T17:14:28Z")

</div>

just to be clear,, i am doing all these query stuff in kibana and my index is pretty large with 10+ GB ,,

---

<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: [March 20, 2019, 4:10pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/3 "2019-03-20T16:10:44Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

### Author: ![Mohsin\_Farooq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsin_farooq/32/42025_2.png) [@Mohsin\_Farooq](https://discuss.elastic.co/u/Mohsin_Farooq)
#### Post date: [March 20, 2019, 4:23pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/4 "2019-03-20T16:23:43Z")

</div>

Thanks for the quick reply,, i will post data for the query it soon

---

<div class="post-metadata">

### Author: ![Mohsin\_Farooq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsin_farooq/32/42025_2.png) [@Mohsin\_Farooq](https://discuss.elastic.co/u/Mohsin_Farooq)
#### Post date: [March 20, 2019, 5:03pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/5 "2019-03-20T17:03:11Z")

</div>

@dadoonet i just edit my first post with all the scripts,, pls see if its clear enough now

---

<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: [March 20, 2019, 5:26pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/6 "2019-03-20T17:26:51Z")

</div>

Here is an example that works:

```auto
DELETE stg_sdmr_three
PUT stg_sdmr_three
{
  "settings": {
    "analysis": {
      "analyzer": {
        "analyzer_startswith": {
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      }
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "business": {
          "type": "text",
          "analyzer": "analyzer_startswith",
          "search_analyzer": "simple"
        }
      }
    }
  }
}

PUT stg_sdmr_three/_doc/1
{
    "business" : "Jeff Nomura"
}

PUT stg_sdmr_three/_doc/2
{
    "business" : "HORIKAWAYA NOMURA"
}

PUT stg_sdmr_three/_doc/3
{
    "business" : "NOMURA CONSULTING"
}

GET stg_sdmr_three/_search
{
  "query": {
    "prefix": {
      "business": "nomura"
    }
  }
}

```

But may be you would prefer using the match phrase prefix query instead?  
See [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html)

---

<div class="post-metadata">

### Author: ![Mohsin\_Farooq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mohsin_farooq/32/42025_2.png) [@Mohsin\_Farooq](https://discuss.elastic.co/u/Mohsin_Farooq)
#### Post date: [March 21, 2019, 2:14pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/7 "2019-03-21T14:14:55Z")

</div>

Thanks alot David,, you are a gem

---

<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: [April 18, 2019, 2:14pm UTC](https://discuss.elastic.co/t/startswith-analyzer-not-working/172394/8 "2019-04-18T14:14:55Z")

</div>

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