Query help must_not except when

I wonder if someone would be able to help me with this query

The query works and returns results, but I want to change the must not bit, instead of

"must_not": [ { "match": { "notificationType": "88" }}

I want to say do not include records where notificationType = 88 except if brand="Fiction" and PublicationDate=now

I don't understand how to add in that exception to the main query.

Any help would be appreciated

Thanks

Grant

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "pub_date_calculated": {
              "lt": "now+180d"
            }
          }
        }

      ],
      "must_not": [
        {
          "match": {
            "notificationType": "88"
          }
        },
        {
          "match": {
            "productForm": "DH"
          }
        }
   ] }
  }
}

I think you need to add a new bool query inside your must_not clause.
This new bool query would then be a should array with 2 clauses:

  • One for notificationType = 88
  • Another one with a bool query containing a must_not array with 2 clauses for:
    • brand="Fiction"
    • PublicationDate=now

HTH

Thanks for the guidance, I still don't seem to be able to get this to work. I would be expecting the number of records to increase. Have I done this must_not query correctly?

Ant help would be appreciated

Grant

  "must_not": [
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "notificationType": "88"
              }
            },
            {
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "brand": "Fiction"
                      }
                    },
                    {
                      "range": {
                        "pub_date_calculated": {
                          "lt": "now+280d"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    },
    {
      "match": {
        "notificationType": "88"
      }
    },
    {
      "match": {
        "productForm": "DH"
      }
    }

]

I believe this because you let the following clauses:

{
      "match": {
        "notificationType": "88"
      }
    },
    {
      "match": {
        "productForm": "DH"
      }
    }

I'd probably remove that.

If you need further help, could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

OK, I have a simpler version below

Get records pub_date_calculated less than now + 1 day
Exclude records notificationType = 88
Include all records that have an Imprint of Avon (even if they have a notificationType = 88)

Thanks

Grant

productindex/_search

{
  "query": {
"bool": {
  "must": [
    {
      "range": {
        "pub_date_calculated": {
          "lt": "now+1d"
        }
      }
    }
  ],
  "must_not": [
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "notificationType": "88"
              }
            },
            {
              "query": {
                "bool": {
                  "must_not": [
                    {
                      "match": {
                        "imprint": "Avon"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  ]
}
  }
}

Does it work?

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