Query help must_not except when


(Grant Donovan) #1

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"
          }
        }
   ] }
  }
}

(David Pilato) #2

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


(Grant Donovan) #3

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"
      }
    }

]


(David Pilato) #4

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.


(Grant Donovan) #5

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"
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  ]
}
  }
}

(David Pilato) #6

Does it work?


(system) closed #7

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