103

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?

Community
  • 1
  • 1
user3663882
  • 6,957
  • 10
  • 51
  • 92

1 Answers1

246

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

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "n"
          }
        },
        {
          "match": {
            "tag": "t"
          }
        }
      ]
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • 2
    How can I get minimum number of records (say atleast 5) from each of my fields. – Harshit Bhatt Jan 08 '20 at 16:47
  • This query seems to only match if the value is in all fields vs. what one would typically want, which is to match if the value is found in ONE of the fields. – Matthew Dean Jun 18 '21 at 15:30
  • 7
    @MatthewDean that wasn't the need of the OP. But if you have a different need, feel free to create a new question, or if it's just OR semantics that you want then just replace `must` by `should` and also add `minimum_should_match: 1` and you're covered – Val Jun 18 '21 at 15:40