azer-p
azer-p

Reputation: 282

Elasticsearch wildcard search on multiple fields

Im building a filter function and what I want is a wildcard filter. If value is "roj", all records in any field containing "roj" should be displayed.

How to implement this?

Here's my query,

{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "Project_error"
          }
        },
        {
          "wildcard": {
            "api_name": {
              "wildcard": "*roj*"
            }
          }
        },
        {
          "wildcard": {
            "error_Code": {
              "wildcard": "*roj*"
            }
          }
        }
      ]
    }
  }
}

Java code

BoolQueryBuilder bqb = new BoolQueryBuilder();
bqb.must(QueryBuilders.existsQuery("Project_error"))

if(!filter.isEmpty()) {
   bqb.filter(QueryBuilders.wildcardQuery(fields[0],"*"+filter+"*"));
   bqb.must(QueryBuilders.wildcardQuery(fields[1],"*"+filter+"*"));
   ...
}

searchSourceBuilder.query(bqb);

This script displays data only if both field contains "roj", which not correct.

Upvotes: 0

Views: 2232

Answers (1)

dhamo
dhamo

Reputation: 555

Using the Below query you can achieve the result.

GET <index_name>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "*roj*",
            "fields":["field_1", "field_2"]
          }
        }
      ]
    }
  }
}

If you want apply your query term in all fields remove the fields attributes in the above query.!!!

Upvotes: 1

Related Questions