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

**URL:** <https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426>\
**Category:** Elasticsearch\
**Created:** [May 7, 2021, 1:43pm UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426 "2021-05-07T13:43:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [May 7, 2021, 1:43pm UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/1 "2021-05-07T13:43:43Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![jdmcalee](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@jdmcalee](https://discuss.elastic.co/u/jdmcalee)\
**Post date:** [May 7, 2021, 1:55pm UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/2 "2021-05-07T13:55:11Z")

</div>

I believe you want to use the [cross\_fields type](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields) for that query.

---

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [May 7, 2021, 1:58pm UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/3 "2021-05-07T13:58:40Z")

</div>

Yes, it works, thanks

---

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [May 10, 2021, 7:02am UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/4 "2021-05-10T07:02:04Z")

</div>

It doesn't work in nested fields

---

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [May 10, 2021, 7:03am UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/5 "2021-05-10T07:03:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jdmcalee](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@jdmcalee](https://discuss.elastic.co/u/jdmcalee)\
**Post date:** [May 20, 2021, 2:30pm UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/6 "2021-05-20T14:30:05Z")

</div>

Did you try wrapping the cross fields query in a [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html)?

---

<div class="post-metadata">

**Author:** ![My-project-repositor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/my-project-repositor/32/85949_2.png) [@My-project-repositor](https://discuss.elastic.co/u/My-project-repositor)\
**Post date:** [May 31, 2021, 5:43am UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/7 "2021-05-31T05:43:47Z")

</div>

Yes I did

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 28, 2021, 5:44am UTC](https://discuss.elastic.co/t/all-fields-should-at-least-contain-one-term-and-all-terms-should-be-present/272426/8 "2021-06-28T05:44:04Z")

</div>

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