Elasticsearch API查询

zz/2024/4/19 22:37:35
1. 查询索引中的全部数据
public class EsTest_Doc_Query {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));// 1.查询索引中的全部数据// 修改部分开始#######SearchRequest request = new SearchRequest();request.indices("rapper");request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));SearchResponse response = client.search(request, RequestOptions.DEFAULT);// 修改部分结束#######SearchHits hits = response.getHits();System.out.println(hits.getTotalHits());System.out.println(response.getTook());for(SearchHit hit:hits){System.out.println(hit.getSourceAsString());}client.close();}
}
2. 条件查询
        SearchRequest request = new SearchRequest();request.indices("rapper");request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("sex","男")));SearchResponse response = client.search(request, RequestOptions.DEFAULT);
3. 分页查询
        SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());// 页码 =(当前页数-1)* 每页数目 例如:(第二页-1)*2=2, from =2builder.from(2);builder.size(2);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);
4. 查询排序
        SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());builder.sort("age", SortOrder.DESC);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();
5. 过滤字段
		SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());String[] include={"name"};String[] exclude={"age"};builder.fetchSource(include,exclude); //此时只有namerequest.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);
6. 组合查询
        SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder();BoolQueryBuilder boolQueryBuiler = QueryBuilders.boolQuery();boolQueryBuiler.must(QueryBuilders.matchQuery("sex","女"));boolQueryBuiler.mustNot(QueryBuilders.matchQuery("name","陈折涵"));
//        boolQueryBuiler.should(QueryBuilders.matchQuery("sex","男")) ;builder.query(boolQueryBuiler);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();
7. 范围查询
 		SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());SearchSourceBuilder builder = new SearchSourceBuilder();RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");rangeQuery.gte(20);rangeQuery.lt(40);builder.query(rangeQuery);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);
8. 高亮查询
		SearchRequest request = new SearchRequest();request.indices("rapper");// 设置查询条件    termsQuery 和 termQuerySearchSourceBuilder builder = new SearchSourceBuilder();TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "刘甜1");// 查询不到// 设置高亮,就是把目标字段使用前端标签包起来HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("name");highlightBuilder.preTags("<font color='red'>");highlightBuilder.postTags("</font>");builder.highlighter(highlightBuilder);builder.query(termQueryBuilder);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);
9. 聚合查询-最大最小平均值
 		SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder();// 创建聚合builderAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");builder.aggregation(aggregationBuilder);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();
10. 聚合查询-分组
        SearchRequest request = new SearchRequest();request.indices("rapper");SearchSourceBuilder builder = new SearchSourceBuilder();// 创建聚合builder,terms为自定义名称TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("age_group").field("age");builder.aggregation(aggregationBuilder);request.source(builder);SearchResponse response = client.search(request, RequestOptions.DEFAULT);SearchHits hits = response.getHits();

查询的方式有很多中,但是思想都差不多,总结了一张图
Query
有时间可以对比postman上的HTTP检索方式,会发现相似度很大,API就会很快上手。


http://www.ngui.cc/zz/2700335.html

相关文章

cocos2d cut the rope

http://www.cocoachina.com/downloads/video/2010/1103/2291.html

需要将一个11GB的文件传输到另外一台服务器,如何断点续传?如何限制带宽?

1、 http://www.redicecn.com/html/Linux/20130703/460.html 需要将一个11GB的文件传输到另外一台服务器&#xff0c;如何断点续传&#xff1f;如何限制带宽&#xff1f; 使用rsync&#xff0c;完整命令如下&#xff1a; rsync -av --bwlimit1000 --progress --inplace --rshss…

amazon返利

<script type"text/javascript" src"http://ir-na.amazon-adsystem.com/s/impression-counter?tag51zuiyou05-20&o1"></script> <noscript><img src"http://ir-na.amazon-adsystem.com/s/noscript?tag51zuiyou05-20"…

sizeof() 和 strlen

sizeof 在编译的时候计算&#xff0c;只关心计算的类型 int main() {int a 10;int size sizeof(a);//4 //sizeof 实在编译过程执行&#xff0c;只关心计算的类型int size_ sizeof(a 0.9);//8 //a0.9类型变为double ,8个字节 }int main() {const char* s "zytzyt&…

for()循环参数调用顺序

for(表达式1&#xff1b;表达式2&#xff1b;表达式3) {循环体语句; } 表达式1&#xff1a;初始化语句 表达式2&#xff1a;判断条件语句 表达式3&#xff1a;控制条件语句 A.先调用表达式1&#xff0c;初始化变量&#xff1b; B.再调用表达式2&#xff0c;判断变量是否满…

gcc ,g++,gdb的安装

1.gcc的安装 sudo apt install gcc 2.g的安装 sudo apt install g 3.gdb的安装 sudo apt install gdb

Mysql学习之constraint/key/primary key/unique/foreign key/constraint的关系

1.constraint就是约束的意思&#xff0c;在MySQL中约束的表现形式有&#xff1a;not null/ primary key/unique隐含有约束功能。 2.如果为一个字段创建了约束&#xff0c;除非这个字段是not null &#xff0c;否则就是为了这个字段即创建了约束又创建了索引&#xff0c;也就是…

基于jdk8的集合源码学习(二):List集合家族研究(AbstractList)

List集合继承关系如图&#xff0c;我们将根据这层关系进行自上而下的分析&#xff0c;分析每个类出现的原因&#xff0c;拥有哪些方法&#xff0c;这些方法底层是如何实现的&#xff1a; 学习一&#xff1a;List接口与AbstractCollection的区别&#xff1a; 从这个继承关系图中…

Spring的BeanFactory重要属性之PropertyEditor,如何bean的属性转换

PropertyEditor是什么&#xff1f; PropertyEditor是JavaBean规范里&#xff0c;提供的一个高级接口&#xff0c;该接口提供了一些方法规范&#xff0c;这些方法可以将JavaBean的外部数据String类型的数据&#xff0c;转换成JavaBean的内部属性值。PropertyEditorSupport是什么…

传统企业如何搭上互联网+的大船

&#xfeff;&#xfeff; 2014年被称为互联网元年&#xff0c;自2000年互联网泡沫之后&#xff0c;互联网再次开始成为商界关注的热点话题&#xff0c;互联网也被上升到国家战略高度。唯恐被时代抛弃的传统企业也纷纷加入互联网大军&#xff0c;进行一系列业务调整和策略改革。…