Dmitry Vasechkin
Dmitry Vasechkin

Reputation: 45

Elasticsearch: results from multiple queries in one?

have 4 different results in such kind of queries:

GET _search
{
  "query": {
    "query_string": {
      "default_field": "desc",
      "query": "лес"
    }
  }
}
GET _search
{
  "query": {
    "query_string": {
      "default_field": "desc",
      "query": "лес*"
    }
  }
}
GET _search
{
  "query": {
    "query_string": {
      "default_field": "desc",
      "query": "*лес"
    }
  }
}
GET _search
{
  "query": {
    "query_string": {
      "default_field": "desc",
      "query": "*лес*"
    }
  }
}

The difference is the asterisk position. Is it possible to get results from all four queries in one ?

Upvotes: 0

Views: 250

Answers (1)

glenacota
glenacota

Reputation: 2547

if you don't need to differentiate what query has determined a successful hit, then you can just use a boolean should query. Something like

{
  "query": {
    "bool": {
      "should": [
      {
        "query_string": {
          "default_field": "desc",
          "query": "лес"
        }
      },
      {
        "query_string": {
          "default_field": "desc",
          "query": "лес*"
        }
      },
      ... 
      ]
    }
  }
}

Update with some recommendations on the queries:

  • I'm not sure what you need *nec, nec*, and *nec* for. If you are going to combine them, and you don't need to differentiate the hit match, isn't *nec* enough?
  • I would recommend you switching from match_query to match (first query), prefix and wildcard (on the other queries, if you have non-analysed sub-fields available)

Upvotes: 2

Related Questions