英文:
NativeSearchQuery is deprecated what is the equivalent beyond 5.0
问题
The equivalent of org.springframework.data.elasticsearch.core.query.NativeSearchQuery
for support beyond 5.0 is:
在5.0版本以后,org.springframework.data.elasticsearch.core.query.NativeSearchQuery
的等价部分是:
import org.springframework.data.elasticsearch.client.NativeSearchQuery;
import org.springframework.data.elasticsearch.client.NativeSearchQueryBuilder;
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes().build())
.withQuery(QueryBuilders.termQuery(Configuration.CONFIGURATION_TYPE, field))
.withSort(new FieldSortBuilder(Configuration.NAME).order(SortOrder.ASC))
.withPageable(pageable)
.build();
英文:
What is the equivalent of org.springframework.data.elasticsearch.core.query.NativeSearchQuery for support beyond 5.0.
Migrated the following snippet from
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes().build())
.withQuery(QueryBuilders.termQuery(Configuration.CONFIGURATION_TYPE, field))
.withSorts(new FieldSortBuilder(Configuration.NAME).order(SortOrder.ASC)).withPageable(pageable)
.build();
to the following query, but it fails
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
NativeQuery query = new NativeQueryBuilder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes().build())
.withQuery(QueryBuilders.termQuery(Configuration.CONFIGURATION_TYPE, field))
.withSorts(new FieldSortBuilder(Configuration.NAME).order(SortOrder.ASC)).withPageable(pageable)
.build();
答案1
得分: 1
使用本地查询,
package com.lazyaudio;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.QueryBuilders;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
public class Test {
public static void main(String[] args) {
Pageable pageable = null;
NativeQuery query = NativeQuery.builder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes().build())
.withQuery(QueryBuilders.termQueryAsQuery("fieldName", ""))
.withSort(Sort.by(Sort.Direction.DESC, "fieldName"))
.withPageable(pageable)
.build();
}
}
请注意,我已将代码中的HTML编码(")还原为正常的引号。
英文:
use Native Query,
package com.lazyaudio;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.QueryBuilders;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilterBuilder;
public class Test {
public static void main(String[] args) {
Pageable pageable = null;
NativeQuery query = NativeQuery.builder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes().build())
.withQuery(QueryBuilders.termQueryAsQuery("fieldName", ""))
.withSort(Sort.by(Sort.Direction.DESC, "fieldName"))
.withPageable(pageable)
.build();
}
}
答案2
得分: 1
以下是您要翻译的内容:
"I had the same issue, thank you. For my part, I also want to perform a groupBy on 'brand' by aggregation.
The JSON query I'm trying to formulate with NativeQuery looks like this:
{
"size": 0,
"aggs": {
"distinct_brand": {
"terms": {
"field": "brand",
"size": 10000,
"order": {
"_key": "asc"
}
}
}
}
}
And my code is currently like this:
Query query = NativeQuery.builder()
.withQuery(q -> q
.matchAll(my -> my))
.withSort(Sort.by(Sort.Direction.ASC, "brand")).build();
How can I add the aggregation?"
英文:
I had the same issue, thank you. For my part, I also want to perform a groupBy on 'brand' by aggregation.
The JSON query I'm trying to formulate with NativeQuery looks like this:
{
"size": 0,
"aggs": {
"distinct_brand": {
"terms": {
"field": "brand",
"size": 10000,
"order": {
"_key": "asc"
}
}
}
}
}
And my code is currently like this:
Query query = NativeQuery.builder()
.withQuery(q -> q
.matchAll(my -> my))
.withSort(Sort.by(Sort.Direction.ASC, "brand")).build();
How can I add the aggregation?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论