Startswith analyzer not working

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

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

Could you provide a full recreation script as described in About the Elasticsearch category. 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.

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

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

Here is an example that works:

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

1 Like

Thanks alot David,, you are a gem

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