Home > Back-end >  How to write complex ElasticSearch query
How to write complex ElasticSearch query

Time:04-06

I want to return the data from ElasticSearch using the range query. My Condition is something like this.

((Range(Price and Discount) OR Range(Price) AND Filter(Must1) AND Filter(Must2)) 

The issue I am facing is that some document contains both price and discount but some only contains Price. I need a query to get data according to the specified range. So, it returns the discount field but not the specified range which I want.

right now I am using this query.

    "query": {
    "bool": {
        "must": [
            {
                "bool": {
                    "should": [
                        {
                            "bool": {
                                "must": [
                                    {
                                        "range": {
                                            "discount": {
                                                "gte": 10,
                                                "lte": 12
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "price": {
                                                "gte": 10,
                                                "lte": 12
                                            }
                                        }
                                    }
                                ]
                            }
                        },
                        {
                            "bool": {
                                "should": [
                                    {
                                        "range": {
                                            "discount": {
                                                "gte": 10,
                                                "lte": 12
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "price": {
                                                "gte": 10,
                                                "lte": 12
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {
                "terms": {
                    "Category": [
                        "123"
                    ]
                }
            },
            {
                "nested": {
                    "path": "the_path",
                    "query": {
                        "bool": {
                            "must": {
                                "match": {
                                }
                            },
                            "filter": [    
                            ]
                        }
                    }
                }
            }
        ]
    }
}

Please help me with this I am stuck with it from past few days.

CodePudding user response:

Based on the condition you have given, following DSL Query will be created

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "range": {
                                                "price": {
                                                    "gte": 10,
                                                    "lte": 20
                                                }
                                            }
                                        },
                                        {
                                            "range": {
                                                "deiscount": {
                                                    "gte": 10,
                                                    "lte": 20
                                                }
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "range": {
                                    "price": {
                                        "gte": 10,
                                        "lte": 20
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "filter": {
                            "term": {
                                "user.id": "kimchy"
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "filter": {
                            "term": {
                                "user.id": "kimchy"
                            }
                        }
                    }
                }
            ]
        }
    }
}
  • Related