# Query with must and should with ANd and OR Logic in Elasticsearch

**URL:** <https://discuss.elastic.co/t/query-with-must-and-should-with-and-and-or-logic-in-elasticsearch/351068>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 15, 2024, 12:35pm UTC](https://discuss.elastic.co/t/query-with-must-and-should-with-and-and-or-logic-in-elasticsearch/351068 "2024-01-15T12:35:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakhshunda\_Noorein\_J](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rakhshunda_noorein_j/32/99407_2.png) [@Rakhshunda\_Noorein\_J](https://discuss.elastic.co/u/Rakhshunda_Noorein_J)\
**Post date:** [January 15, 2024, 12:35pm UTC](https://discuss.elastic.co/t/query-with-must-and-should-with-and-and-or-logic-in-elasticsearch/351068/1 "2024-01-15T12:35:38Z")

</div>

Hi,  
I want my search work like all the search terms searched with AND results first and then with OR results.

example,  
Search term - _ **borosil infrastructure** _  
the result should come like -

> borosil infrastructure  
> borosil infrastructure ltd  
> private sector borosil infrastructure  
> borosil ltd  
> borosil carp  
> infrastructure ltd  
> infrastructure info

first i am giving preference to AND results then OR...

My query is

```auto
{
  "from": 0,
  "size": 100,
  "query": {
"function_score": {
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "example",
            "fields": [
            "field1^6","field1._2gram","field1._index_prefix^12",
            "field2^2","field2._2gram^4","field2._index_prefix"
            ],
            "type": "bool_prefix"
          }
        }
      ],
      "should": [
        {
          "multi_match": {
            "query": "example",
            "fields": [
            "field1^6","field1._2gram","field1._index_prefix^12",
            "field2^2","field2._2gram^4","field2._index_prefix"
            ],
            "type": "bool_prefix",
            "operator": "and",
            "boost": 1000
          }
        }
      ],
      "filter": {
          "bool":{
              "must":[
                {
                    "term": {
                        "Field3": 0
                    }
                },
                {
                    "term": {
                        "Field4": 1
                    }
                }

              ]
          }
      }
    }
  },
      "functions": [
        {
          "filter": {
            "match_all": {
              "boost": 1
            }
          },
          "script_score": {
            "script": {
              "source": "doc['P_score'].value != null && doc['P_score'].value !=0 ? (doc['P_score'].value * 100000) + (doc['W_score'].value * 100000) : (doc['W_score'].value * 100000)",
              "lang": "painless"
            }
          }
        }
      ],
      "boost": 1,
      "boost_mode": "avg",
      "score_mode": "avg"
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

```

the results are coming as per desired in some cases but in some cases its not behaving as needed.

then I changed the query with AND in must and Or in should

> "query": {  
> "bool": {  
> "must": [  
> {  
> "multi\_match": {  
> "query": "example",  
> "fields": [  
> "field1^6","field1.\_2gram","field1.\_index\_prefix^12",  
> "field2^2","field2.\_2gram^4","field2.\_index\_prefix"  
> ],  
> "type": "bool\_prefix",  
> **"operator": "AND"**  
> }  
> }  
> ],  
> "should": [  
> {  
> "multi\_match": {  
> "query": "example",  
> "fields": [  
> "field1^6","field1.\_2gram","field1.\_index\_prefix^12",  
> "field2^2","field2.\_2gram^4","field2.\_index\_prefix"  
> ],  
> "type": "bool\_prefix",  
> **"operator": "OR"**  
> }  
> }  
> ]

but It end up in showing only AND results.. and no OR results are coming...

---

<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:** [February 12, 2024, 12:36pm UTC](https://discuss.elastic.co/t/query-with-must-and-should-with-and-and-or-logic-in-elasticsearch/351068/2 "2024-02-12T12:36:12Z")

</div>

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