Elastic search is displaying correct result only after a space

I have created an index using the following mapping.

PUT _template/template_1
{
    "index_patterns": [
      "ubq-*"
    ],
    "settings": {
      "index": {
        "analysis": {
          
          "analyzer": {
            "my_analyzer": {
              "filter": [
                "lowercase"
                
              
              ],
              "type": "custom",
              "tokenizer": "keyword",
              "remove_trailing": "false"
            }
          }
        },
        "number_of_shards": "1"
      }
    },
    "mappings": {
      "doc": {
        "properties": {
          "Index": {
            "type": "float",
            "index": "true"
          },
          "Category": {
            "type": "keyword",
            "index": "true"
          },
          "Scat": {
            "type": "keyword",
            "index": "true"
          },
          "Sscat": {
            "type": "keyword",
            "index": "true"
          },
          "Products": {
            "type": "text",
            "index": "true",
            "analyzer": "my_analyzer"
          },
          "Measure": {
            "type": "keyword",
            "index": "true"
            
          },
          "Price": {
            "type": "float",
            "index": "true"
          },
          "Description": {
            "type": "keyword",
            "index": "true"
            
          },
          "Gst": {
            "type": "float",
            "index": "true"
          },
          "Url": {
            "type": "keyword",
            "index": "true"
            
          }
        }
      }
    },
    "aliases": {}
  }

I am getting random search results until i give a space. Please look in the images below!


In above image you can see search result displaying random products.
But giving a space after sugar displays the correct products.

1)How can i fix this issue so when i type sugar i see sugar?
2) When i search for products that are not availabe in my index, elastic is still returning some results, how to make sure elastic doesn't return any result when product is not found?

I suspect it's something about how you are querying Elasticsearch. E.g. are you sending all the characters as soon as they press the key, rather than on the next key press? What does your Elasticsearch query look like?

Alternatively, perhaps the data has trailing spaces? You've analyzed a few fields so that they are stored as keywords, and do not remove trailing spaces. Meaning "sugar " (note space at the end) will be stored exactly like that, so you must search for "sugar " to find it.

I'd recommend using a regular text analyzer which tokenizes and removes special characters/whitespace for full text search, rather than keyword.

1 Like

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