簡體   English   中英

如何編寫復雜的 ElasticSearch 查詢

[英]How to write complex ElasticSearch query

我想使用范圍查詢從 ElasticSearch 返回數據。 我的情況是這樣的。

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

我面臨的問題是有些文件同時包含價格和折扣,但有些只包含價格。 我需要一個查詢來根據指定范圍獲取數據。 因此,它返回折扣字段而不是我想要的指定范圍。

現在我正在使用這個查詢。

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

請幫我解決這個問題,過去幾天我一直堅持下去。

根據您提供的條件,將創建以下 DSL 查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM