Query with match by multiple fields

ElasticsearchNosql

Elasticsearch Problem Overview


I'm pretty new to elastic search and would like to write a query that is concerned about two fields. I mean the content of the fields contains the specified substring. I have a document containing fields, like this:

name: n
tag: t

I tried this:

/_search -d '
{
    "query": {
        "match": {
             "name": "n",
             "tag": "t"
        }
    }
}

But the query results in the following error:

> [match] query parsed in simplified form, with direct field name, but > included more options than just the field name, possibly use its > 'options' form, with 'query' element?

Is there a way to do this in elasticsearch?

Elasticsearch Solutions


Solution 1 - Elasticsearch

You need two match queries enclosed in a bool/must query, like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "n"
          }
        },
        {
          "match": {
            "tag": "t"
          }
        }
      ]
    }
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3663882View Question on Stackoverflow
Solution 1 - ElasticsearchValView Answer on Stackoverflow