Returning documents that match multiple wildcard string queries

Viewed 88

I'm new to Elasticsearch and would greatly appreciate help on this

In the query below I only want the first document to be returned, but instead both documents are returned. How can I write a query to search for two wildcard strings on two separate fields, but only return documents that match?

I think what's being returned currently is score dependent, but I don't need the score.

POST /pr/_doc/1
{
  "type": "Type ONE",
  "currency":"USD"
}

POST /pr/_doc/2
{
  "type": "Type TWO",
  "currency":"USD"
}

GET /pr/_search
{
  "query": {
      "bool": {
        "must": [
          {
            "simple_query_string": {
              "query": "Type ON*",
              "fields": ["type"],
              "analyze_wildcard": true
            }
          },
          {
            "simple_query_string": {
              "query": "US*",
              "fields": ["currency"],
              "analyze_wildcard":true
            }
          }
        ]
    }
  }
}
1 Answers

Use below query which uses the default_operator: AND and query string for in depth information and further reading.

Search query

{
  "query": {
    "query_string": {
      "query": "(Type ON*) AND (US*)",
      "fields" : ["type", "currency"],
      "default_operator" : "AND"
    }
  }
}

Index your sample docs and it returns your expected doc only:

"hits": [
      {
        "_index": "multiplequery",
        "_type": "_doc",
        "_id": "1",
        "_score": 2.1823215,
        "_source": {
          "type": "Type ONE",
          "currency": "USD"
        }
      }
    ]
Related