Search code examples
elasticsearch

How to query any field using wildcard in elasticsearch


I currently have a problem. I want to search exactly for field a and match the search for field b.

this is my code

GET /_search
{
  "query": {
    "bool": {
      "must": [
        { "multi_match": { "query": "biden","fields": [ "*.keyword"] } },
        { "wildcard": { "email.keyword": "123456*" } }
      ]
    }
  }
}

As you can see, I not only want to use wildcard to search the 'email' field but also phone, name and any fields (maybe wildcard cannot do this) How can I do this?

Now, I'm not sure if wildcard will achieve what I want


Solution

  • You can try to use term query to return the exact string provided on that field, if that’s the case.

    For example, we have multiple docs in index hello_world. But searching for word “Blippi and the whole gang” reveals more exact results than we are looking for:

    GET  /hello_world/_search
    {
        "query": {
            "bool": {
                "must": [{
                    "term": {
                        "doc.play_name.keyword": {
                        "value": "Blippi and the whole gang",
                        "boost": 1.0
                        }
                  }},{
                    "term": {
                        "doc.protagonist.keyword": {
                        "value": "Blippi and the whole gang",
                        "boost": 1.0
                    }
                }}]
            }
        }
    }
    

    Result shows:

    {
      "took": 5,
      "timed_out": false,
      "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 1,
          "relation": "eq"
        },
        "max_score": 3.0726933,
        "hits": [
          {
            "_index": "hello_world",
            "_id": "7",
            "_score": 3.0726933,
            "_source": {
              "doc": {
                "type": "diretory",
                "play_name": "Blippi and the whole gang",
                "movie_time_minutes": 0,
                "characters": 2,
                "protagonist": "Blippi and the whole gang",
                "director": "Blippi and the whole gang"
              }
            }
          }
        ]
      }
    }