in elasticsearch/dsl/query.py [0:0]
def __and__(self, other: Query) -> Query:
q = self._clone()
if isinstance(other, Bool):
q.must += other.must
q.must_not += other.must_not
q.filter += other.filter
q.should = []
# reset minimum_should_match as it will get calculated below
if "minimum_should_match" in q._params:
del q._params["minimum_should_match"]
for qx in (self, other):
min_should_match = qx._min_should_match
# TODO: percentages or negative numbers will fail here
# for now we report an error
if not isinstance(min_should_match, int) or min_should_match < 0:
raise ValueError(
"Can only combine queries with positive integer values for minimum_should_match"
)
# all subqueries are required
if len(qx.should) <= min_should_match:
q.must.extend(qx.should)
# not all of them are required, use it and remember min_should_match
elif not q.should:
q.minimum_should_match = min_should_match
q.should = qx.should
# all queries are optional, just extend should
elif q._min_should_match == 0 and min_should_match == 0:
q.should.extend(qx.should)
# not all are required, add a should list to the must with proper min_should_match
else:
q.must.append(
Bool(should=qx.should, minimum_should_match=min_should_match)
)
else:
if not (q.must or q.filter) and q.should:
q._params.setdefault("minimum_should_match", 1)
q.must.append(other)
return q