Skiminock
Skiminock

Reputation: 149

Elasticsearch insensitive search by term

I've got next query for search

    {  
       "query":{  
          "bool":{  
             "must":[  
                {  
                   "term":{  
                      "cardrecord.fields.name.raw":"HERE_IS_SOME_NAME"
                   }
                }
             ],
             "must_not":[  

             ],
             "should":[  

             ]
          }
       },
       "from":0,
       "size":50,
       "sort":[  

       ],
       "facets":{  

       }
    }

How can I modify the query for case insensetive search by term? I can add some more description if needed.

Upvotes: 1

Views: 135

Answers (4)

DecKno
DecKno

Reputation: 323

all the fields are analyzed using Standard Analyzer by default. If "index":"not_analyzed" is specified in mapping then the field will not be analyzed

Standard Analyzer converts the input string to lowercase and splits with whitespace and special characters. so in your case, HERE_IS_SOME_NAME will be split into tokens some, name. But the tokenshere and is will not be created as they are english adverbs.

Same thing happens when you search for "cardrecord.fields.name.raw" field. It splits into tokens and searches for all documents with that tokens in specific field (using Standard Analyzer). P.S: Separate or different analyzer can be configured for searching also.

so match query searches for all documents with some and name tokens. Hence you would have got additional documents.

term query specifically looks for exact case and full word match. But it will not match any document since tokens are already split and lowercase

Follow these steps for your requirement:

{
  "mappings": {
    "my_type": {
      "properties": {
        "cardrecord.fields.name.raw": {
          "type":  "string",
          "index": "not_analyzed" 
        }
      }
    }
  }
}

Update this mapping for your index named my_type as per code given above. You need to create new index with new mapping though. Since update might not reflect. Then try running your search query in your question.

Adding detailed sequence of query:

mapping:

  {
      "mappings": {
        "my_type": {
          "properties": {
            "cardrecord.fields.name.raw": {
              "type": "string",
              "index": "not_analyzed",
              "store": "true"
            }
          }
        }
      }
    }

Indexing document:

{
  "cardrecord.fields.name.raw": "HERE_IS_SOME_NAME"
}

search query:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "cardrecord.fields.name.raw": "HERE_IS_SOME_NAME"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 50,
  "sort": [],
  "facets": {}
}

Upvotes: 2

Val
Val

Reputation: 217474

You can use a match query, but you need to match the cardrecord.fields.name field, because the raw subfield is probably not_analyzed and thus won't work for case-insensitive matching.

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "cardrecord.fields.name": "HERE_IS_SOME_NAME"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 50,
  "sort": [],
  "facets": {}
}

Upvotes: 1

Mazhar Qayyum
Mazhar Qayyum

Reputation: 630

Try using a match query

{  
       "query":{  
          "bool":{  
             "must":[  
                {  
                   "match":{  
                      "cardrecord.fields.name.raw":"HERE_IS_SOME_NAME"
                   }
                }
             ],
             "must_not":[  

             ],
             "should":[  

             ]
          }
       },
       "from":0,
       "size":50,
       "sort":[  

       ],
       "facets":{  

       }
    }

Upvotes: 1

Ali Nikneshan
Ali Nikneshan

Reputation: 3502

use filter instead of query, this will reduce amount of processing a lot:

{  
   "filter":{  
      "bool":{  
         "must":[  
            {  
               "term":{  
                  "cardrecord.fields.name.raw":"HERE_IS_SOME_NAME"
               }
            }
         ],
         "must_not":[  

         ],
         "should":[  

         ]
      }
   },
   "from":0,
   "size":50,
   "sort":[  

   ],
   "facets":{  

   }
}

Upvotes: 1

Related Questions