# How to ensure all words are matched across multiple bool.should.clauses?

**URL:** <https://discuss.elastic.co/t/how-to-ensure-all-words-are-matched-across-multiple-bool-should-clauses/94233>\
**Category:** Elasticsearch\
**Created:** [July 23, 2017, 10:00am UTC](https://discuss.elastic.co/t/how-to-ensure-all-words-are-matched-across-multiple-bool-should-clauses/94233 "2017-07-23T10:00:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joonhocho](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joonhocho/32/20343_2.png) [@joonhocho](https://discuss.elastic.co/u/joonhocho)\
**Post date:** [July 23, 2017, 10:00am UTC](https://discuss.elastic.co/t/how-to-ensure-all-words-are-matched-across-multiple-bool-should-clauses/94233/1 "2017-07-23T10:00:25Z")

</div>

I want to make sure all words are matched by at least one clause under bool.should query.

Here's my data structure:

```
person = {
  username,
  names: [{first, last}], // nested
  addresses: [{city, state}], // nested
}

```

Lets say we have a person in db:

```
{
  names: [{first: "john", last: "doe"}],
  addresses: [{city: "san francisco", state: "ca"}],
}

```

User types "john doe" in search box and is matched since we have a matching data.  
User types "john doe san francisco" and is still matched, now with HIGHER score.  
User types "john doe san diego" and should not be matched because diego is not matched.

How can I achieve this?

Here's my current query which does not guarantee all words are matched:

```
query: {
  bool: {
    should: [
      {match: {username: text}},
      {
        nested: {
          path: 'names',
          query: {
            multi_match: {
              query: text,
              type: 'cross_fields',
              fields: [
                'names.first',
                'names.last',
              ],
            },
          },
        },
      },
      {
        nested: {
          path: 'addresses',
          query: {
            multi_match: {
              query: text,
              type: 'cross_fields',
              fields: [
                'addresses.city',
                'addresses.state',
              ],
            },
          },
        },
      },
    ],
  },
},
```

---

<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:** [August 20, 2017, 10:00am UTC](https://discuss.elastic.co/t/how-to-ensure-all-words-are-matched-across-multiple-bool-should-clauses/94233/2 "2017-08-20T10:00:29Z")

</div>

Closed on August 20, 2017, 10:00am UTC

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