# Help me modify this query so that it can find out the matches which contain N terms in a specific order as opposed to all matches possible which contain the N terms

**URL:** <https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507>\
**Category:** Elasticsearch\
**Created:** [October 14, 2019, 6:20pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507 "2019-10-14T18:20:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [October 14, 2019, 6:20pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/1 "2019-10-14T18:20:16Z")

</div>

This is what I had, this can query for phrases but they will always be in any order

`{'query': {'bool': {'should': [{'bool': {'must': [{'match_phrase': {'data': 'word1'}}, {'match_phrase': {'data': 'word2'}}]}}]}}, 'from': 0, 'size': 9000}`

The results returned by the above are:

> bla bla word1 word2 bla  
> X Y word2 word1 bla bla  
> MNO bla bla word1 word2  
> ABC word1 word2 bla bla

The results I want for a given query of (word1,word2) are:

> bla bla word1 word2 bla  
> ABC word1 word2 bla bla  
> MNO bla bla word1 word2

Similarly, for a query of (word2,word1), I want the results:

> X Y word2 word1 bla bla

Can someone please tell me how to fix this query to include order of sequences

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [October 14, 2019, 7:39pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/2 "2019-10-14T19:39:38Z")

</div>

Hi,

You may want to look at [Intervals Queries](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html). One possible way to rewrite the query you provided is as follows:

```auto
GET my_index/_search
{
  "query": {
    "intervals": {
      "data": {
        "match": {
          "ordered": "true",
          "query": "word1 word2"
        }
      }
    }
  }
}

```

There are probably a few other ways to approach using word order in queries, so let me know if this doesn't solve your problem.

-William

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [October 14, 2019, 7:46pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/3 "2019-10-14T19:46:20Z")

</div>

Hi William,

Thank you for your prompt response.

I'm using the python API. If I do a search of this form:

```
body = {
  "query": {
    "intervals": {
      "data": {
        "match": {
          "ordered": "true",
          "query": "word1 word2"
        }
      }
    }
  }
}
result = es.search(index=index, body=body)

```

I get an error:

RequestError(400, 'parsing\_exception', 'no [query] registered for [intervals]')

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [October 14, 2019, 8:08pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/4 "2019-10-14T20:08:12Z")

</div>

Apparently, match\_phrase is a solution but according to this article this is not the case:

> [@Querystring search: Tokens are out of order](https://discuss.elastic.co/t/querystring-search-tokens-are-out-of-order/23238/5):
>
> You understanding is correct. The former will be translated into a Lucene phrase query, which uses the term doc positions to find matches. Both query terms are analyzed, but the latter will simply be a bag-of-words query, which ignores positions. Cheers, Ivan On Apr 14, 2015 10:38 PM, "Dave Reed" [infinity88@gmail.com](mailto:infinity88@gmail.com) wrote: To perhaps answer my own question, I think I understand the difference. details:"foo bar" Would search for the tokens in the same order (implied by the docs I ref…

```
body = {
    "query": {
        "multi_match" : {
            "query": "word1 word2",
            "fields": ["data"],
            "type": "phrase",
            "slop": 9999
        }
    }
}

```

Just tried this, can confirm, that it gives same results regardless of order of words considered.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 14, 2019, 8:11pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/5 "2019-10-14T20:11:24Z")

</div>

Intervals query was introduced in Elasticsearch 7.0 I believe. Which version are you using?

---

<div class="post-metadata">

**Author:** ![hjiop\_oop](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hjiop\_oop](https://discuss.elastic.co/u/hjiop_oop)\
**Post date:** [October 14, 2019, 8:23pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/6 "2019-10-14T20:23:38Z")

</div>

If it's a version thing, I think I could be using an older one, any way I could check?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 14, 2019, 8:26pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/7 "2019-10-14T20:26:51Z")

</div>

Running `curl localhost:9200` should give you the version number.

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [October 14, 2019, 8:28pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/8 "2019-10-14T20:28:05Z")

</div>

If you have the URL of your elasticsearch instance, you can `curl` it or enter it in a browser to see version information. You should see something like this:

```auto
{
  "name" : "...",
  "cluster_name" : "...",
  "cluster_uuid" : "...",
  "version" : {
    "number" : "7.2.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "508c38a",
    "build_date" : "2019-06-20T15:54:18.811730Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

```

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [October 14, 2019, 8:28pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/9 "2019-10-14T20:28:57Z")

</div>

And I apologize for not mentioning that Intervals queries are a somewhat new 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:** [November 11, 2019, 8:29pm UTC](https://discuss.elastic.co/t/help-me-modify-this-query-so-that-it-can-find-out-the-matches-which-contain-n-terms-in-a-specific-order-as-opposed-to-all-matches-possible-which-contain-the-n-terms/203507/10 "2019-11-11T20:29:06Z")

</div>

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