[英]Elasticsearch High level Java Client version 6.0.1 to implement script based sorting
I'm using ES version 6.0.1 and have integrated the Java High level rest client having version 6.0.1 in my application.我正在使用 ES 版本 6.0.1 并在我的应用程序中集成了版本为 6.0.1 的 Java 高级其余客户端。
I am currently trying to build this script based sorting query using the Java High Level Rest client API of elastic search:我目前正在尝试使用弹性搜索的 Java 高级 Rest 客户端 API 构建这个基于脚本的排序查询:
{
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"params": {
"ids": [3, 2, 1570]
},
"source": """
int idsCount = params.ids.size();
int id = (int)doc['id'].value;
int foundIdx = params.ids.indexOf(id);
return foundIdx > -1 ? foundIdx: idsCount + 1;
"""
}
}
}
}
But I couldn't find any documentation regarding the script based sorting queries for the java client.但是我找不到任何关于 Java 客户端基于脚本的排序查询的文档。 I would appreciate if someone will help me out in implementing the above query using java API.
如果有人能帮助我使用 Java API 实现上述查询,我将不胜感激。
I am not sure about ES version 6.0.1.我不确定 ES 版本 6.0.1。 I am using ES version 7.9.0
我正在使用 ES 版本 7.9.0
This works for me.这对我有用。
Map<String, List<int>> params = new HashMap<String, List<int>>();
List<int> list = Arrays.asList(3, 2, 1570);
params.put("ids", list);
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
.size(size)
.from(from);
Script inlineScript = new Script(ScriptType.INLINE, "painless", "int idsCount = params.ids.size();
int id = (int)doc['id'].value;
int foundIdx = params.ids.indexOf(id);
return foundIdx > -1 ? foundIdx: idsCount + 1;", params);
ScriptSortBuilder ssb = new ScriptSortBuilder(inlineScript, ScriptSortType.NUMBER).order(SortOrder.ASC);
searchSourceBuilder.sort(ssb);
searchRequest.source(searchSourceBuilder);
searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.