How to divide must query into separate queries

Mapping

PUT index
PUT /index/_mapping
{
  "properties": {
    "categories": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "deleted": {
      "type": "boolean"
    },
    "id": {
      "type": "long"
    },
    "manufacturers": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "integer"
        }
      }
    },
    "vendors": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "isDeletedVendor": {
          "type": "boolean"
        },
        "isPublishedVendor": {
          "type": "boolean"
        }
      }
    },
    "published": {
      "type": "boolean"
    }
  }
}

Fill with Data

PUT index/_doc/1
{
  "categories": [
    {
      "id": 9
    }
  ],
  "manufacturers": [
    {
      "id": 2452
    }
  ],
  "vendors": [
    {
      "id": 8,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    },
    {
      "id": 9,
      "isDeletedVendor": true,
      "isPublishedVendor": true
    }
  ],
  "published": true,
  "deleted": false
}

Query

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "vendors",
                  "query": {
                    "terms": {
                      "vendors.id": [
                        24
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "exists": {
                            "field": "vendors.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "manufacturers",
                  "query": {
                    "terms": {
                      "manufacturers.id": [
                        2452
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "exists": {
                            "field": "manufacturer.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10000,
  "_source": {
    "includes": [
      "deleted",
      "published",
      "vendors.id",
      "categories.id",
      "manufacturers.id",
      "id"
    ]
  }
}

so what I have in query logic is: GET hits where (
[
[manufacturer ids in 2452 or
manucaturerids not exist ] AND
[vendorids in 24 or
vendorids not exist]
]
)
Query works perfect but i want logic like this
GET hits where (
[
[manufacturer ids in 2452 or
manucaturerids not exist ] AND
[vendorids in 24 or
vendorids not exist]
]
OR
[
[manufacturer ids in 3 or
manucaturerids not exist ] AND
[vendorids in 9 or
vendorids not exist]
]
)
I was trying something like this but it didn't worked... pls help...

{
  "query": {
    "bool": {
      "must": [
        {
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "vendors",
                  "query": {
                    "terms": {
                      "vendors.id": [
                        24
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "exists": {
                            "field": "vendors.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "manufacturers",
                  "query": {
                    "terms": {
                      "manufacturers.id": [
                        2452
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "exists": {
                            "field": "manufacturer.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
        },
        {
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "vendors",
                  "query": {
                    "terms": {
                      "vendors.id": [
                        9
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "exists": {
                            "field": "vendors.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "manufacturers",
                  "query": {
                    "terms": {
                      "manufacturers.id": [
                        3
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "exists": {
                            "field": "manufacturer.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
        },
      ]
    }
  },
  "size": 10000,
  "_source": {
    "includes": [
      "deleted",
      "published",
      "vendors.id",
      "categories.id",
      "manufacturers.id",
      "id"
    ]
  }
}

I also tried this

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "vendors",
                  "query": {
                    "terms": {
                      "vendors.id": [
                        24
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "exists": {
                            "field": "vendors.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "manufacturers",
                  "query": {
                    "terms": {
                      "manufacturers.id": [
                        2452
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "exists": {
                            "field": "manufacturer.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ],
      "must": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "vendors",
                  "query": {
                    "terms": {
                      "vendors.id": [
                        9
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "exists": {
                            "field": "vendors.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "nested": {
                  "path": "manufacturers",
                  "query": {
                    "terms": {
                      "manufacturers.id": [
                        3
                      ]
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "exists": {
                            "field": "manufacturer.id"
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10000,
  "_source": {
    "includes": [
      "deleted",
      "published",
      "vendors.id",
      "categories.id",
      "manufacturers.id",
      "id"
    ]
  }
}

I also tried this...

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              24
                            ]
                          }
                        }
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "vendors",
                              "query": {
                                "exists": {
                                  "field": "vendors.id"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "terms": {
                            "manufacturers.id": [
                              2452
                            ]
                          }
                        }
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturer.id"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
                {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              87
                            ]
                          }
                        }
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "vendors",
                              "query": {
                                "exists": {
                                  "field": "vendors.id"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "terms": {
                            "manufacturers.id": [
                              2452
                            ]
                          }
                        }
                      }
                    },
                    {
                      "bool": {
                        "must_not": [
                          {
                            "nested": {
                              "path": "manufacturers",
                              "query": {
                                "exists": {
                                  "field": "manufacturer.id"
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10000,
  "_source": {
    "includes": [
      "deleted",
      "published",
      "vendors.id",
      "categories.id",
      "manufacturers.id",
      "id"
    ]
  }
}

Idon't know if it is working, but it is extremly slow

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              94
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "terms": {
                            "manufacturers.id": [
                              2452
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
                {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "vendors",
                        "query": {
                          "terms": {
                            "vendors.id": [
                              101
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "should": [
                    {
                      "nested": {
                        "path": "manufacturers",
                        "query": {
                          "terms": {
                            "manufacturers.id": [
                              278
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "size": 10000,
  "_source": {
    "includes": [
      "deleted",
      "published",
      "vendors.id",
      "categories.id",
      "manufacturers.id",
      "id"
    ]
  }
}

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