Skip to content

Merging two queries with a common part #1647

Open
@oren0e

Description

@oren0e

Hi,
Currently if I do something like this:

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from elasticsearch_dsl.query import Q

client = Elasticsearch("http://<ADDRESS>:9200", size=10000, from_=0, index="test")
q1 = Q({"range": {"updated_at": {"gte": "2023-05-30"}}})
q2 = Q({"range": {"updated_at": {"gte": "2023-05-30", "lte": "now"}}})

s = Search(using=client, index="test").query(q1).query(q2)

print(s.to_dict())

I will get something like the following:

{'query': {'bool': {'must': [{'range': {'updated_at': {'gte': '2023-05-30'}}}, {'range': {'updated_at': {'gte': '2023-05-30', 'lte': 'now'}}}]}}}

I would expect that the resulting query will merge the two common gte parts to have something like this (I don't mind that this will continue to live inside a bool-must block):

{'query': {'bool': {'must': [{'range': {'updated_at': {'gte': '2023-05-30', 'lte': 'now'}}}]}}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions