Terms query with the operator AND

Hello!

I have this register (is a fragment):

"_index": "worker",
"_type": "work",
"_id": "055-008765",
"_score": 1,
"_source": {
"sex": "Women",
"name": "Nat",
"vehicles": [
"Car",
"Bus "
]
}

And I have this query:

"bool": {
"must": [
{
"terms": {
"vehicles": [
"car",
"bus"
]
}
}
]

I want all the workers that they have car AND bus, not one of them. How can I do it?

Thanks for all.

1 Like

You can do this either with a single match query, or with a bool as you mentioned, here's an example of both:

POST /_search
{
  "query": {
    "match": {
      "vehicles": {
        "query": "car bus",
        "operator": "AND"
      }
    }
  }
}

POST /_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"vehicles": "car"}},
        {"match": {"vehicles": "bus"}}
      ]
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.