当前位置: 首页 > elasticsearch, 搜索 > 正文

elasticsearch MatchAllQueryBuilder应用

关键字:
1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

需求:我们想查询搜索里面所有数据,按时间排序。就像查询数据库那样,分页获取文章。

解决:使用MatchAllQueryBuilder。代码如下:

 
      SearchResponse searchResponse = esClient.prepareSearch(searchParam.getIndexName())
                      .setTypes("news")
                      .setQuery(new MatchAllQueryBuilder())
                      .setScroll(new TimeValue(60000))
                      .addFields("id","channelid","catid","title","content","thumb","url","updatetime")
                      .setFrom(0)
                      .setSize(100)
                      .execute()
                      .actionGet();

获取频道1下的所有文章,按时间排序。

FilteredQueryBuilder queryBuilder=
QueryBuilders.filteredQuery(new MatchAllQueryBuilder(),
        		FilterBuilders.boolFilter().must(FilterBuilders.termFilter("channelid", 1l)));
      SearchResponse searchResponse = esClient.prepareSearch(searchParam.getIndexName())
                      .setTypes("news")
                      .setQuery(queryBuilder)
                      .setScroll(new TimeValue(60000))
                      .addFields("id","channelid","catid","title","content","thumb","url","updatetime")
                      .setFrom(0)
                      .setSize(100)
                      .execute()
                      .actionGet();

elasticsearch查询比数据库快很多,我们就可以用elasticsearch集群来做数据库的查询工作。

本文固定链接: http://www.chepoo.com/elasticsearch-match-all-query-builder-apply.html | IT技术精华网

elasticsearch MatchAllQueryBuilder应用:等您坐沙发呢!

发表评论