gpt4 book ai didi

elasticsearch - Elasticsearch范围查询字符串格式的数字

转载 作者:行者123 更新时间:2023-12-02 22:35:34 27 4
gpt4 key购买 nike

我有一个Elasticsearch索引,如下所示

{
"Price": "50.99"
},
{
"Price": "30.99"
},
{
"Price": "40.99"
},
{
"Price": "10.99"
}

我正在尝试根据 price的范围来获取文档。我想返回价格范围为 30-100的文档。但是它没有返回任何文档。
 GET index_name/_search
{
"query": {
"bool": {
"must": [
{ "range": {"Price": {"gte": 40, "lte": 100}}}
]
}
}
}

由于 Price是String格式,所以我无法提取文档。我没有更改 index的权限。有没有一种方法可以在不更改索引的情况下对其进行查询?

最佳答案

最佳方法:将字段类型更改为float

但是无论如何,如果要保持字段类型为字符串,可以使用无痛脚本来实现。即

GET index_name/_search
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "Float.parseFloat(doc['Price.keyword'].value) >= 40 && Float.parseFloat(doc['Price.keyword'].value) <= 100",
"lang": "painless"
}
}
}
}
}
}

关于elasticsearch - Elasticsearch范围查询字符串格式的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55374426/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com