gpt4 book ai didi

elasticsearch - ElasticSearch 'range'查询返回不合适的结果

转载 作者:行者123 更新时间:2023-12-02 22:46:55 25 4
gpt4 key购买 nike

让我们进行以下查询:

{
"timeout": 10000,
"from": 0,
"size": 21,
"sort": [
{
"view_avg": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"from": 10,
"to": 20
}
}
},
{
"terms": {
"category_ids": [
16405
]
}
}
]
}
}

}

对我正在运行的数据集的此查询应不返回任何结果(因为所有价格都在100s-1000s范围内)。但是,此查询返回结果,价格匹配为:
"price": "1399.00"
"price": "1299.00"
"price": "1089.00"

依此类推,依此类推。等等,我有什么想法可以修改查询,以便返回正确的结果?

最佳答案

我99%确信您的映射错误,并且price声明为string。 Elasticsearch根据字段类型使用了不同的Lucene范围查询,正如您在documentation中看到的那样。 TermRangeQuery类型的string就像您的输出一样,它使用lexicographical ordering(即1100在10到20之间)。

要测试它,您可以尝试以下映射/搜索:

PUT tests/

PUT tests/test/_mapping
{
"test": {
"_source" : {"enabled" : false},
"_all" : {"enabled" : false},
"properties" : {
"num" : {
"type" : "float", // <-- HERE IT'S A FLOAT
"store" : "no",
"index" : "not_analyzed"
}
}
}
}

PUT tests/test/1
{
"test" : {
"num" : 100
}
}

POST tests/test/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"num": {
"from": 10,
"to": 20
}
}
}
]
}
}
}

Result:

{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

如果删除索引并尝试重新创建它,请将 num类型更改为字符串:
PUT tests/test/_mapping
{
"test": {
"_source" : {"enabled" : false},
"_all" : {"enabled" : false},
"properties" : {
"num" : {
"type" : "string", // <-- HERE IT'S A STRING
"store" : "no",
"index" : "not_analyzed"
}
}
}
}

您会看到不同的结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "tests",
"_type": "test",
"_id": "1",
"_score": 1
}
]
}
}

关于elasticsearch - ElasticSearch 'range'查询返回不合适的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31808199/

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