白龙菜牛
2021-05-17 15:15
采纳率: 100%
浏览 190

SpringBoot 集成 elasticsearch 中下面两种查询方式的区别

用SearchRequest searchRequest = new SearchRequest("discusspost");创建,然后使用RestHighLevelClient中的.search()方法进行查询。和。在dao层继承ElasticsearchRepository,然后调用里面的 .search()方法进行查询。

这两种查询方式有啥区别

 

SearchRequest searchRequest = new SearchRequest("discusspost");

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()

.query(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))

.from(0).size(10);

searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

 

======== 第二种 =========

 

Dao 层

public interface BlogDao extends ElasticsearchRepository<BlogEntity,String> {}

Service 层

BoolQueryBuilder queryBuilder = new BoolQueryBuilder();

BoolQueryBuilder keyBuilder = new BoolQueryBuilder();

keyBuilder.should(QueryBuilders.wildcardQuery("title", "*aaa*"));

queryBuilder.must(keyBuilder);

Pageable pageable = PageRequest.of(article.getStart(), article.getLimit(), Sort.Direction.DESC,"releaseDateTime");
Page<Article> search = blogDao .search(queryBuilder,pageable);

 

就是

https://blog.csdn.net/wpw2000/article/details/115704320

https://blog.csdn.net/weixin_45566249/article/details/111297868

这两个博客上用的方法;我都试了下,都能查询出结果;想知道两种方法的区别,或者说那种方法好一点;

如果有更好的方式,也欢迎大佬推荐

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 小P聊技术 2021-05-17 16:10
    已采纳

    // 这一段是多字段匹配查询

    QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content")  

     

    // 这一段是模糊查询

    QueryBuilders.wildcardQuery("title", "*aaa*")

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题