当我按降序搜索ID时,ElasticSearch返回的ID不为最大 [英] ElasticSearch return not maximum ID when i'm searching by descending id
问题描述
我有索引URI
contracts / _doc /
我将文档推送到此处,ID由SQL-DB预生成
这是来自的代码示例JAVA客户端:
最终的BulkRequest请求= new BulkRequest();
contract.forEach(e-> request.add(new IndexRequest(CONTRACT_INDEX,
CONTRACT_INDEX_TYPE,
String.valueOf(e.getId()))
.source( contractNumber,e.getContractNumber())
)
);
有时我需要查看索引中ID的最大值,然后发送此查询
{
sort:[
{ _id:{ order: desc} }
],
大小:1
}
问题是,ElasticSearch总是返回不超过 9999
的值hits:[
{
_index:合同,
_type: _doc,
_id: 9999,
_score:null,
_source:{
contractNumber: 2000/290/990990
},
sort:[
9999
]
}
]
当我表演时搜索并查询:
{
size:10000
}
实际结果处于正确状态,我所有的ID r 9999存在并通过
contracts / _doc / 10005
$选择p $ p>
返回正确的结果。
请告知我如何解决这种情况
解决方案原因是您的ID是字符串,并且字符串按字典顺序(而不是数字顺序)排序,因此在字符串世界9999> 1000000中。
您应该做的是在文档中创建另一个数字字段,其中包含与ID字段等效的数字,并按该数字字段排序。
I have index URI
contracts/_doc/
I push my documents there, ID is pre-generated by SQL-DB
this is the code sample from JAVA client:
final BulkRequest request = new BulkRequest(); contracts.forEach(e -> request.add(new IndexRequest(CONTRACT_INDEX, CONTRACT_INDEX_TYPE, String.valueOf(e.getId())) .source("contractNumber", e.getContractNumber()) ) );
Sometimes i need to see the biggest value of ID in my index, and i send this query
{ "sort" : [ { "_id" : {"order" : "desc"}} ], "size": 1 }
The problem is that ElasticSearch always return value no more than
9999
"hits": [ { "_index": "contracts", "_type": "_doc", "_id": "9999", "_score": null, "_source": { "contractNumber": "2000/290/990990" }, "sort": [ "9999" ] } ]
When i'm performing search with query:
{ "size": 10000
}
the actual result is in proper state, all my ID's over 9999 is present and select by
contracts/_doc/10005
returns proper result.
Please advise how i can solve this situation
解决方案The reason is that your IDs are strings and strings are sorted lexicographically, not numerically, hence in the string world 9999 > 1000000.
What you should do is to have another numerical field in your document that contains the numeric equivalent of your ID field and sort by that numeric field.
这篇关于当我按降序搜索ID时,ElasticSearch返回的ID不为最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!