Home > Software design >  How to use Wildcards in Elastic search query to skip some prefix values
How to use Wildcards in Elastic search query to skip some prefix values

Time:10-22

"I am searching in a elasticsearch cluster GET request on the basis of sourceID tag with value :- "/A/B/C/UniqueValue.xml" and search query looks like this:-"

{
  "query": {
    "bool": {
      "must": [
               {
                "term": {
                "source_id": {
                "value": "/A/B/C/UniqueValue.xml"
               }
              }
             }
            ]
           }
          }
       }

"How can i replace "/A/B/C" from any wildcard or any other way as i just have "UniqueValue.xml" as an input for this query. Can some please provide the modified search Query for this requirement? Thanks."

CodePudding user response:

The following search returns documents where the source_id field contains a term that ends with UniqueValue.xml.

{
    "query": {
        "wildcard": {
            "source_id": {
                "value": "*UniqueValue.xml"
            }
        }
    }
}

Note that wildcard queries are expensive. If you need fast suffix search, you could add a multi-field to your mapping which includes a reverse token filter. Then you can use prefix queries on that reversed field.

  • Related