elasticsearchelasticsearch-queryterm-query

how to user query should and must_not in elasticseacrh


i have data in elastic like this

{
  "xId" : "25b35718-1804-428e-87fc-e215422d21a0",
  "date" : "2019-10-13T07:07:07.558Z",
  "type" : "promo",
  "accountId" : 50,
  "readBy" : [
    "b",
    "a"
  ]
}

I want to retrieve data with a number of conditions , so i use query with should and must_not, when I use that query, the array returns empty, but now the data still appears, is there something missing?

this is my sample query

{"query": {
          "constant_score": {
            "filter": {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "accountId": [50]
                    }
                  },{
                    "terms": {
                      "type": ["promo"]
                    }
                  },
                  {
                    "match": {
                      "userId": "a"
                    }
                  }
                ],
                "must_not": [
                  {
                    "terms": {
                      "type": []
                    }
                  }, {
                    "terms": {
                      "readBy": ["a"]
                    }
                  }

                ]
              }
            }
          }
        }
      }

Solution

  • solved i use query like this

    {"query": {
          "constant_score": {
            "filter": {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "accountId": [50]
                    }
                  },{
                    "terms": {
                      "type": ["promo"]
                    }
                  },
                  {
                    "match": {
                      "userId": "a"
                    }
                  }
                ],
                "must_not": [
                  {
                    "terms": {
                      "type": []
                    }
                  }, {
                    "term": {
                      "readBy.keyword": "a"
                    }
                  }
    
                ]
              }
            }
          }
        }
      }