All fields should at least contain one term and all terms should be present

Hello,

How can I query documents where all fields should at least contain one term and all terms should be present ?

Suppose I have this index

PUT /test_query
{
 "mappings": {
  "properties": {
    "firstname": {
      "type": "text"
     },
     "lastname": {
      "type": "text"
     }
   }
 }
}

And these documents :

POST /test_query/_doc
{
  "firstname": "Will Smith",
  "lastname": "Will Smith"
}

POST /test_query/_doc
{
  "firstname": "Will",
  "lastname": "Smith"
}

I want to query documents where all fields (firstname, lastname) should at least contain one of these terms :

Will, Smith

I ran this query :

GET /test_query/_search
{
  "query": {
    "multi_match" : {
      "query": "WILL SMITH",
      "fields": [ "firstname", "lastname" ]
    }
  }
}

And it returns all the documents which is normal but if I run this query :

GET /test_query/_search
{
  "query": {
    "multi_match" : {
      "query": "WILL SMITH TEST",
      "fields": [ "firstname", "lastname" ]
    }
  }
}

It should give me an empty result instead of all documents because there is no TEST in all fields (firstname, lastname)

I believe you want to use the cross_fields type for that query.

1 Like

Yes, it works, thanks

It doesn't work in nested fields

It works when the field not nested but what about nested fields?

Did you try wrapping the cross fields query in a nested query?

Yes I did

1 Like

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