# Return documents that match a minimal number of words in the same sentence

**URL:** <https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848>\
**Category:** Elasticsearch\
**Created:** [August 5, 2020, 10:13am UTC](https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848 "2020-08-05T10:13:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flm/32/3169_2.png) [@flm](https://discuss.elastic.co/u/flm)\
**Post date:** [August 5, 2020, 10:13am UTC](https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848/1 "2020-08-05T10:13:18Z")

</div>

I need to return documents that match at least N words in the same sentence.

I split my documents per sentence and index each one as a separate value like so:

```auto
PUT /test_index/_doc/id1
    {
      "texts": [
"Your first step is the subject line.",
"You will have just seconds to gain the full attention of your reader."]
    }

```

and leave the position\_increment\_gap to the default 100.

Let say I need to match a minimum of 2 words.  
I need to return the document if I search for the terms ("bla", "attention", "reader") but not for ("bla", "subject", "reader"). "bla" is not in the document, "attention" and "reader" are on the same sentence, "subject" and "reader" are not.

The approach with a boolean should query and minimum\_should\_match does not work, as this query returns the document when it shouldn't:

```auto
"query" : {
          "bool": {
            "should": [
              {"term": {"texts": "subject"}},
              {"term": {"texts": "reader"}}
            ],
            "minimum_should_match": 2
          }
      }

```

So I need a way to mix proximity and minimum should match.  
Is there a way to achieve that?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 6, 2020, 11:42am UTC](https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848/2 "2020-08-06T11:42:27Z")

</div>

The [nested type](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) could be a solution here. By indexing every sentence as a nested object, you can query these sentences independently.

First, you set up a nested type in your index' mapping:

```auto
PUT test_index
{
  "mappings": {
    "properties": {
      "texts": {
        "type": "nested"
      }
    }
  }
}

```

Next, you index your document, using a slightly different structure:

```auto
PUT /test_index/_doc/id1
{
  "texts": [
    {
      "text": "Your first step is the subject line."
    },
    {
      "text": "You will have just seconds to gain the full attention of your reader."
    }
  ]
}

```

Now, you can use the [nested query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) to get to the desired results:

```auto
GET /test_index/_search
{
  "query": {
    "nested": {
      "path": "texts",
      "query": {
        "match": {
          "texts.text": {
            "query": "bla attention reader",
            "minimum_should_match": 2
          }
        }
      }
    }
  }
}

GET /test_index/_search
{
  "query": {
    "nested": {
      "path": "texts",
      "query": {
        "match": {
          "texts.text": {
            "query": "bla subject reader",
            "minimum_should_match": 2
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![flm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flm/32/3169_2.png) [@flm](https://discuss.elastic.co/u/flm)\
**Post date:** [August 10, 2020, 11:08am UTC](https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848/3 "2020-08-10T11:08:16Z")

</div>

Thanks Abdon, that's great. I overlooked this "nested type" feature.

---

<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:** [September 7, 2020, 11:08am UTC](https://discuss.elastic.co/t/return-documents-that-match-a-minimal-number-of-words-in-the-same-sentence/243848/4 "2020-09-07T11:08:25Z")

</div>

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