# How to divide must query into separate queries

**URL:** <https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115>\
**Category:** Elasticsearch\
**Created:** [July 14, 2020, 10:57am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115 "2020-07-14T10:57:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 10:57am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115/1 "2020-07-14T10:57:04Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 10:58am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115/2 "2020-07-14T10:58:49Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 11:13am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115/3 "2020-07-14T11:13:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![buka\_bidzina](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buka_bidzina/32/50141_2.png) [@buka\_bidzina](https://discuss.elastic.co/u/buka_bidzina)\
**Post date:** [July 14, 2020, 11:27am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115/4 "2020-07-14T11:27:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 11, 2020, 11:27am UTC](https://discuss.elastic.co/t/how-to-divide-must-query-into-separate-queries/241115/5 "2020-08-11T11:27:20Z")

</div>

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