我不知道如何使用es7.12许多功能不再可用

gywdnpxw  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(282)

我的排序是无效的,我不知道为什么?同时,我的代码似乎很混乱,我想优化它,我不知道怎么做。这主要是一个排序问题。类型和分数不总是按createtime排序,这不是我在这里需要的相关代码:测试代码:@autowired discusspostrepository discusspostrepository;

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

@Autowired
private ElasticsearchOperations elasticsearchOperations;

@Autowired
DiscussPostMapper discussPostMapper;

@Autowired
RestHighLevelClient client;

@Test
public void testSelect2() throws IOException {
    NativeSearchQuery build = new NativeSearchQueryBuilder()
            .withQuery(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))
            //sort
            .withSort(SortBuilders.fieldSort("type").order(SortOrder.DESC))
            .withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
            .withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
            //paging
            .withPageable(PageRequest.of(0, 10))
            //Highlight, Adding it here doesn't seem to work
            .withHighlightFields(
                    new HighlightBuilder.Field("title").preTags("<em>").postTags("</em>"),
                    new HighlightBuilder.Field("content").preTags("<em>").postTags("</em>")
            ).build();

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("discusspost");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

    HighlightBuilder highlightBuilder = new HighlightBuilder();
    HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
    highlightTitle.highlighterType("unified");
    highlightBuilder.field(highlightTitle);
    HighlightBuilder.Field highlightContent = new HighlightBuilder.Field("content");
    highlightBuilder.field(highlightContent);
    searchSourceBuilder.highlighter(highlightBuilder);

    //Type and score cannot be sorted effectively
    searchSourceBuilder.sort(new FieldSortBuilder("type").order(SortOrder.DESC));
    searchSourceBuilder.sort(new FieldSortBuilder("score").order(SortOrder.DESC));
    //Only createtime works
    searchSourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
    searchSourceBuilder.query(build.getQuery());

    searchRequest.source(searchSourceBuilder);

    SearchResponse searchResponse = client.search(searchRequest, ElasticSearchConfig.COMMON_OPTIONS);
    System.out.println(searchResponse.toString()); }

实体:

@Document(indexName = "discusspost", shards = 6, replicas = 3)
public class DiscussPost {

@Id
private int id;

@Field(type = FieldType.Integer)
private int userId;

@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String title;

@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
private String content;

@Field(name = "type",type = FieldType.Integer)
private int type;
@Field(type = FieldType.Integer)
private int status;
@Field(name = "createTime",type = FieldType.Date)
private Date createTime;
@Field(type = FieldType.Integer)
private int commentCount;
@Field(name = "score",positiveScoreImpact = false,type = FieldType.Double)
private double score;

}
@Repository 
public interface DiscussPostRepository extends ElasticsearchRepository<DiscussPost,Integer> {
}
@Configurable
public class ElasticSearchConfig {
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }
    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")));

        return client;
    }
}

我查了一些资料,但还是不知道怎么做

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题