NativeSearchQuery 已被弃用,5.0 以后有何等效替代方式?

huangapple go评论154阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2023年4月10日 19:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定