繁体   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