Startswith analyzer not working

(Mohsin Farooq) #1

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

ES BUG? Filter excluding "-" still brings results with dash "-"
(Mohsin Farooq) #2

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

(David Pilato) #3

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.

(Mohsin Farooq) #4

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

(Mohsin Farooq) #5

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

(David Pilato) #6

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
(Mohsin Farooq) #7

Thanks alot David,, you are a gem

(system) closed #8

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