Abhi
Abhi

Reputation: 1574

Elasticsearch terms with space returns no results

I want to match exact text irrespective of case. What I want works with

    GET elastic_org/_search
{
  "query": {
    "match": {
      "registeredName": {
        "minimum_should_match": "100%",
        "query": "prop org"
      }
    }
  }
}

but the library I use https://github.com/olivere/elastic/, does not support minimum should match

I'm trying with term query

GET elastic_org/_search
{
  "query": {
   "term": {
     "registeredName": "Prop org" //also tried prop org
   }
  }
}

I don't get any results. I tried the above query for a word nospace and I get results.

How do I get exact matches without it being case sensitive and with space?

Index mapping:

{
  "elastic_org" : {
    "mappings" : {
      "properties" : {
       
        "id" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        
        "registeredName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
        
      }
    }
  }
}

Upvotes: 0

Views: 1002

Answers (1)

Bhavya
Bhavya

Reputation: 16192

You can use the case_insensitive param along with the terms query. This parameter was introduced in elasticsearch version 7.10.0

{
  "query": {
    "term": {
      "registeredName.keyword": {
        "value": "prop org",
        "case_insensitive": true
      }
    }
  }
}

Update 1:

Term query returns documents that match exactly with the search term. When using a term query, the text type fields should not be used.

Based on your index mapping, you need to add .keyword to the registeredName field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after registeredName field).

Update 2:

If you don't want to use the case_insensitive parameter, you can use the lowercase filter. This will ensure that all the terms are lowercase, before indexing and searching

Adding a working example with index data, mapping, search query and search result

Index Mapping:

{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "registeredName": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      }
    }
  }
}

Index Data:

{
  "registeredName": "Prop org"
}

Search Query:

{
  "query": {
    "term": {
      "registeredName": "prop org"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67716203",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "registeredName": "Prop org"
        }
      }
    ]

Upvotes: 1

Related Questions