我有这段代码需要转换为spring-data-elastic-search。
{
"query": {
"match_all": {}
},
"size": 10,
"from": 0,
"sort": [
{
"program.title.descriptions.value.keyword": {
"order": "asc",
"nested": {
"path": "program.title.descriptions",
"filter": {
"match": {
"program.title.descriptions.length": "LONG"
}
}
}
}
}
]
}
我已经尝试过了,
SortBuilder sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath))
但是我不知道如何将“过滤器”部分添加到SortBuilder中。如果我尝试以下方法,
QueryBuilder matchFirst = QueryBuilders.boolQuery().filter(QueryBuilders.matchQuery("program.title.descriptions.length", "LONG"));
sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath)).setNestedFilter(matchFirst);
会说
java.lang.IllegalArgumentException: Setting both nested_path/nested_filter and nested not allowed
at org.elasticsearch.search.sort.FieldSortBuilder.setNestedFilter(FieldSortBuilder.java:213)
任何想法都会受到赞赏。
答案 0 :(得分:0)
这应该工作:
QueryBuilder matchFirst = QueryBuilders.boolQuery().filter(QueryBuilders.matchQuery("program.title.descriptions.length", "LONG"));
sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath).setFilter(matchFirst));
使用setNestedSort
时应使用setNestedFilter
或setNestedPath
,但不建议使用最后两个。
答案 1 :(得分:0)
您需要在嵌套查询中设置过滤器。你可以尝试
SortBuilder sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath).setFilter(matchFirst));