简体   繁体   中英

How to write complex ElasticSearch query

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM