Combine ‘should’ with ‘filter’ result will exclude should in elasticsearch query

53 Views Asked by At

I have to search for a result with these conditions:

  1. filter:- make="FUSO" AND model="SUPERGRADE" AND is_vin_required="is_vin_required_1"
  2. should:- make="FUSO" AND is_vin_required="is_vin_required_0"

result should be union of both. I tried to add the same condition but the conditions in should are ignored. I also added

"minimum_should_match":1

but again same result.

I used this query:-

{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        },
        {
          "match": {
            "type": "product_abstract"
          }
        },
        {
          "match": {
            "store": "ZA"
          }
        },
        {
          "match": {
            "locale": "en_US"
          }
        },
        {
          "match": {
            "is-active": true
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "active-from": {
                    "lte": "now"
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "active-from"
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "range": {
                  "active-to": {
                    "gte": "now"
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "active-to"
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {}
        }
      ],
      "filter": [
        {
          "nested": {
            "query": {
              "term": {
                "integer-facet.facet-name": {
                  "value": "price-DEFAULT-ZAR-GROSS_MODE",
                  "boost": 1
                }
              }
            },
            "path": "integer-facet"
          }
        },
        {
          "bool": {
            "should": [
              {
                "nested": {
                  "path": "string-facet",
                  "query": {
                    "bool": {
                      "filter": [
                        {
                          "term": {
                            "string-facet.facet-name": {
                              "value": "make",
                              "boost": 1
                            }
                          }
                        },
                        {
                          "term": {
                            "string-facet.facet-value": {
                              "value": "FUSO",
                              "boost": 1
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "nested": {
                  "path": "string-facet",
                  "query": {
                    "bool": {
                      "filter": [
                        {
                          "term": {
                            "string-facet.facet-name": {
                              "value": "model",
                              "boost": 1
                            }
                          }
                        },
                        {
                          "term": {
                            "string-facet.facet-value": {
                              "value": "SUPERGRADE",
                              "boost": 1
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "should": [
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "string-facet",
                  "query": {
                    "bool": {
                      "filter": [
                        {
                          "term": {
                            "string-facet.facet-name": {
                              "value": "make",
                              "boost": 1
                            }
                          }
                        },
                        {
                          "term": {
                            "string-facet.facet-value": {
                              "value": "FUSO",
                              "boost": 1
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              },
              {
                "multi_match": {
                  "fields": [
                    "full-text",
                    "full-text-boosted^3"
                  ],
                  "query": "is_vin_required_0",
                  "type": "cross_fields"
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1,
      "boost": 1
    }
  }
}

No result was found with this query.

I tried with "minimum_should_match": 1 but still no result was found.

I also follow this article to achieve result:- Link

0

There are 0 best solutions below