-1

I have data in elasticsearch like this:

{
    a : "status",
    b : "01"
},
{
    a : "status b",
    b : "02"
}

I want to get all data with must and should

I have tried wit must and should query like this

{
  "query": {
    "bool": {
      "must":[
        {
          "match": {
            "a": "status b"
          }
        }],
      "should":[ {
        "match": {
          "b": "01"
        }
      }]
    }
  }
}

but query should is not working, anyone can help me?

DZS
  • 337
  • 1
  • 4
  • 13
  • Can you list the search result? And from my experience, you need to checkout the mappings of your field like "a" and "b". If they are text or string in lower version es, then the field will be analyzed by es so that unexpected results will be returned because current search is like full-text search. – Yifeng Apr 17 '20 at 02:15

2 Answers2

1

As you have not mentioned your mapping, I created my own mapping according to your data and indexed your sample docs and it works fine.

You can also check how your data is indexed using the _analyze API, which would help you debug the issue efficiently. Also, use explain API which would tell you why your should clause is not matching any doc.

Index def

{
    "mappings": {
        "properties": {
            "a": {
                "type": "text"
            },
            "b": {
                "type": "integer"
            }
        }
    }
}

Index sample docs

{
    a : "status",
    b : "01"
},
{
    a : "status b",
    b : "02"
}

Note search query is also same as yours

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "a": "status b"
                    }
                }
            ],
            "should": [
                {
                    "match": {
                        "b": "01"
                    }
                }
            ]
        }
    }
}

And it bring both the sample docs

"hits": [
            {
                "_index": "so_must_should",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.2111092,
                "_source": {
                    "a": "status",
                    "b": "01"
                }
            },
            {
                "_index": "so_must_should",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.77041256,
                "_source": {
                    "a": "status b",
                    "b": "02"
                }
            }
        ]

Please cross-check your mapping and compare it with this sample and let me know if you have further questions.

Amit
  • 30,756
  • 6
  • 57
  • 88
0

What is included in the must statement has to appear in the document for sure to be included in the result document list and you can include more than 1 condition acting as an AND logical operation.The condition included in the should statement is optional, if the document fulfills that condition will be included in the result and if it doesn't it will included as well but the scoring will be different appearing below in the list of results.

In addition, you have to take into account that the condition "status b" is tokenized depending on your analyzer. So, if you are using the default analyzer the tokens are "status" and "b", so any document containing one these values will fullfill the condition acting as an OR logical operator.

sgalinma
  • 202
  • 1
  • 5