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

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

{
  "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...

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