# Query DSL: How to use must in should condition?

**URL:** <https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989>\
**Category:** Elasticsearch\
**Created:** [November 24, 2017, 8:36am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989 "2017-11-24T08:36:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imqishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imqishi/32/23426_2.png) [@imqishi](https://discuss.elastic.co/u/imqishi)\
**Post date:** [November 24, 2017, 8:36am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989/1 "2017-11-24T08:36:33Z")

</div>

I need to do a time period-period match like this:

Data has 2 time field such as d\_from and d\_to.  
Now given param p\_from and param p\_to.

I need to get records which SHOULD satisfy following conditions:

1. p\_from gte d\_from and(MUST) p\_from lte d\_to
2. p\_to gte d\_from and(MUST) p\_to lte d\_to
3. p\_from \<= d\_from and(MUST) p\_to \>= d\_to

1 and 2 is easy to do it seems just use bool-should-range is ok, but condition 3 makes me confused, it need two params satisfy their own condition by MUST then SHOULD with 1 and 2...

Can somebody help me please? Thanks alot..

---

<div class="post-metadata">

**Author:** ![imqishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imqishi/32/23426_2.png) [@imqishi](https://discuss.elastic.co/u/imqishi)\
**Post date:** [November 24, 2017, 8:45am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989/2 "2017-11-24T08:45:37Z")

</div>

emm... Maybe I found solution...Well, I use php so here's an array...

```auto
-queryField: array:1 [
    "bool" => array:1 [
      "should" => array:3 [
        0 => array:1 [
          "range" => array:1 [
            "SESSION_START_TIME" => array:2 [
              "gte" => "2017-11-24T16:20:44+08:00"
              "lte" => "2017-11-24T16:25:44+08:00"
            ]
          ]
        ]
        1 => array:1 [
          "range" => array:1 [
            "SESSION_END_TIME" => array:2 [
              "gte" => "2017-11-24T16:20:44+08:00"
              "lte" => "2017-11-24T16:25:44+08:00"
            ]
          ]
        ]
        2 => array:1 [
          "bool" => array:1 [
            "must" => array:2 [
              0 => array:1 [
                "range" => array:1 [
                  "SESSION_START_TIME" => array:1 [
                    "lt" => "2017-11-24T16:20:44+08:00"
                  ]
                ]
              ]
              1 => array:1 [
                "range" => array:1 [
                  "SESSION_END_TIME" => array:1 [
                    "gt" => "2017-11-24T16:25:44+08:00"
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]

```

---

<div class="post-metadata">

**Author:** ![imqishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imqishi/32/23426_2.png) [@imqishi](https://discuss.elastic.co/u/imqishi)\
**Post date:** [November 27, 2017, 2:42am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989/3 "2017-11-27T02:42:51Z")

</div>

well I found this solution will contain more records.. it is not precise...anybody have idea?

---

<div class="post-metadata">

**Author:** ![imqishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/imqishi/32/23426_2.png) [@imqishi](https://discuss.elastic.co/u/imqishi)\
**Post date:** [November 28, 2017, 11:36am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989/4 "2017-11-28T11:36:05Z")

</div>

Finally I solve this by NLPchina/elasticsearch-sql...  
see

> **[NLPchina/elasticsearch-sql](https://github.com/NLPchina/elasticsearch-sql)**
>
> elasticsearch-sql - Use SQL to query Elasticsearch

Great project!

---

<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:** [December 26, 2017, 11:36am UTC](https://discuss.elastic.co/t/query-dsl-how-to-use-must-in-should-condition/108989/5 "2017-12-26T11:36:21Z")

</div>

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