elasticsearch: two implementations of or operation should and its mixed use with must/filter

The project needs, and I started looking for abuse again! In the past two weeks, because of project requirements, elasticsearch has been used, and with the spirit of "face up to difficulties, learn if you don't understand", I have started another round of searching for abuse.

 
9166166-264de44b8c27b6e0.jpg
Elastic Search

After completing the development of related functions in the past two weeks, I feel that elasticsearch is actually relatively easy to get started, but it also encountered some problems: for example, the or operation or the no operation in sql, adding fields according to term or match, cannot get the expected results. , Text, keyword, index, etc. Now let’s talk about the realization of the or operation first.

In elasticssearch, the or operation can use should. For example, the implementation that satisfies a=1 or b=2 is as follows:

{"query": {

  "bool": {

  "should": [

      {"match": {

      "a": "1"

     },

    {"match": {

    "b": "2"

    }}

  ]

}}}

So if we change the above conditions to: satisfy a=1 or b=2, and c=3, maybe many friends will make the same mistake as me, that is, add a must: c=3 to the above code, that is:

{"query": {

  "bool": {

  "must": [

    {"match": {

      "c": "3"

  }}

  ],

  "should": [

      {"match": {

      "a": "1"

     },

    {"match": {

    "b": "2"

    }}

  ]

}}}

At first glance, there is no problem, that is, the condition in must is satisfied and one of the conditions in should is satisfied. However, after such a run, the result obtained seems to be only c=3, and the filtering of a and b is missing; what's going on? ?

In fact, when silhouette is at the same level as must or filter, it does not need to meet any conditions in should by default. At this time, we can add the minimum_should_match parameter to achieve our purpose, that is, the above code is changed to:

{"query": {

  "bool": {

  "must": [

    {"match": {

      "c": "3"

  }}

  ],

  "should": [

      {"match": {

      "a": "1"

     },

    {"match": {

    "b": "2"

    }}

  ],

    "minimum_should_match":1

}}}

The above code indicates that all the conditions in must must be met, and at least one of the conditions in should is met, so that the expected result is obtained.

Of course, we can also use another way to achieve, that is, put should into a bool condition in must, that is, use a layered way to make should and must not appear at the same time, the implementation is as follows:

{"query": {

    "bool": {

      "must": [

      {

            "match": { "c": "3" },

            "bool": {

                "should": [

              {

                  "match": { "a": "1"  },

                  "match": { "b": "2"  }

                }

        }

  ]

}}}

Both of the above methods can solve the problem of coexistence of should and must or filter. The specific choice depends on the specific situation.

 

Guess you like

Origin blog.csdn.net/qq_32783703/article/details/106948475