h5四合一网站建设,php直播网站开发,教育网网站建设规范,网络平台企业ElasticSearch系列整体栏目 内容链接地址【一】ElasticSearch下载和安装https://zhenghuisheng.blog.csdn.net/article/details/129260827【二】ElasticSearch概念和基本操作https://blog.csdn.net/zhenghuishengq/article/details/134121631【三】ElasticSearch的高级查询Quer…ElasticSearch系列整体栏目 内容链接地址【一】ElasticSearch下载和安装https://zhenghuisheng.blog.csdn.net/article/details/129260827【二】ElasticSearch概念和基本操作https://blog.csdn.net/zhenghuishengq/article/details/134121631【三】ElasticSearch的高级查询Query DSLhttps://blog.csdn.net/zhenghuishengq/article/details/134159587【四】ElasticSearch的聚合查询操作https://blog.csdn.net/zhenghuishengq/article/details/134159587【五】SpringBoot整合elasticSearchhttps://blog.csdn.net/zhenghuishengq/article/details/134212200 SpringBoot整合elasticSearch 一SpringBoot整合ElasticSearch1需要的依赖以及版本2创建config配置类并测试连接3增删改查测试3.1索引插入数据3.2根据id查询数据3.3删除一条数据 4普通查询4.1match条件查询4.2term精确匹配4.3prefix前缀查询4.4通配符查询wildcard4.5范围查询4.6fuzzy模糊查询4.7highlight高亮查询 5聚合查询5.1aggs聚合查询5.2获取最终结果 一SpringBoot整合ElasticSearch
前面几篇讲解了es的安装dsl语法聚合查询等接下来这篇主要就是讲解通过java的方式来操作es这里选择通过springboot的方式整合ElasticSearchSearch
在学习这个整合之前可以查看对应的官网资料https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/7.17/connecting.html
1需要的依赖以及版本
首先创建springboot项目然后需要的依赖如下我前面用的是7.7.0的版本因此这里继续使用这个版本。其他的依赖根据个人需要选择
propertiesjava.version8/java.versionelasticsearch.version7.7.0/elasticsearch.version
/properties
dependenciesdependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.7.0/version/dependency
/dependencies2创建config配置类并测试连接
随后创建一个config的配置类用于连接上ElasticSearch我这边是单机版并没有集群
/*** 连接es的工具类*/
Configuration
public class ElasticSearchConfig { public static final RequestOptions COMMON_OPTIONS;static {RequestOptions.Builder builder RequestOptions.DEFAULT.toBuilder();COMMON_OPTIONS builder.build();}Beanpublic RestHighLevelClient esRestClient(){RestHighLevelClient client new RestHighLevelClient(RestClient.builder(new HttpHost(xx.xx.xx.xx, 9200, http)));return client;}
}在创建好了之后可以直接在test类中进行测试看能否连接成功
RunWith(SpringRunner.class)
SpringBootTest
public class StudyApplicationTests {Resourceprivate RestHighLevelClient client;Testpublic void contextLoads() {System.out.println(restHighLevelClient);}
}在运行之后如果打印出了以下这句话表示整合成功
org.elasticsearch.client.RestHighLevelClient7d151a3增删改查测试
3.1索引插入数据
首先先创建一个users的索引并向里面插入一条数据。插入和更新都可以用这个方法
//创建一个user索引并且插入一条数据
Test
public void addData() throws IOException {//创建一个索引IndexRequest userIndex new IndexRequest(users);User user new User();user.setId(1);user.setUsername(Tom);user.setPassword(123456);user.setAge(18);user.setSex(女);//添加数据userIndex.source(JSON.toJSONString(user), XContentType.JSON);IndexResponse response client.index(userIndex, ElasticSearchConfig.COMMON_OPTIONS);//响应数据System.out.println(response);
}随后再在kibana中查询这个索引可以看到这条数据是已经插入成功的并且索引页创建成功 3.2根据id查询数据
查询id为1的数据需要通过QueryBuild构造器查询
Test
public void getById() throws IOException {SearchRequest request new SearchRequest(users);SearchSourceBuilder builder new SearchSourceBuilder();builder.query(QueryBuilders.matchQuery(id, 1));request.source(builder);SearchResponse response client.search(request, RequestOptions.DEFAULT);System.out.println(response);
}3.3删除一条数据
删除刚刚创建的这条数据这里直接设置id为1即可
Test
public void deleteById() throws Exception{DeleteRequest request new DeleteRequest(users);request.id(1);DeleteResponse delete client.delete(request, ElasticSearchConfig.COMMON_OPTIONS);System.out.println(delete);
}4普通查询
这里主要是结合本人写的第三篇Query DSL的语法通过java的方式写出依旧是先创建一个员工的信息索引并且设置字段得我属性
PUT /employees
{mappings: {properties: {name:{type: keyword},job:{type: keyword},salary:{type: integer}}}
}随后批量的插入10条数据
PUT /employees/_bulk
{ index : { _id : 1 } }
{ name : huisheng1,job:python,salary:35000 }
{ index : { _id : 2 } }
{ name : huisheng2,job:java,salary: 50000}
{ index : { _id : 3 } }
{ name : huisheng3,job:python,salary:18000 }
{ index : { _id : 4 } }
{ name : huisheng4,job:java,salary: 22000}
{ index : { _id : 5 } }
{ name : huisheng5,job:javascript,salary:18000 }
{ index : { _id : 6 } }
{ name : huisheng6,job:javascript,salary: 25000}
{ index : { _id : 7 } }
{ name : huisheng7,job:c,salary:20000 }
{ index : { _id : 8 } }
{ name : huisheng8,job:c,salary: 20000}
{ index : { _id : 9 } }
{ name : huisheng9,job:java,salary:22000 }
{ index : { _id : 10 } }
{ name : huisheng10,job:java,salary: 9000}4.1match条件查询
首先是分页查询分页查询的queryDSL的语法如下
GET /employees/_search
{query: {match: {job: java}}
}java的语法如下
SearchRequest request new SearchRequest(employees);
SearchSourceBuilder builder new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery(job, java));
request.source(builder);
SearchResponse response client.search(request, RequestOptions.DEFAULT);
System.out.println(response);短语匹配的语法如下
builder.query(QueryBuilders.matchPhraseQuery(job,java));多字段查询的语法如下
String fields[] {job,name};
builder.query(QueryBuilders.multiMatchQuery(java,fields));queryString的语法如下
builder.query(QueryBuilders.queryStringQuery(java));4.2term精确匹配
GET /employees/_search
{query: {term: {job: java}}
}精确匹配通过java的方式如下
builder.query(QueryBuilders.termQuery(job,java));4.3prefix前缀查询
PUT /employees/_search
{query:{prefix:{name:{value:huisheng1}}}
}前缀查询的java方式如下
builder.query(QueryBuilders.prefixQuery(name,huisheng1));4.4通配符查询wildcard
GET /employees/_search
{query: {wildcard: {job: {value: *py*}}}
}通配符查询的java方式如下
builder.query(QueryBuilders.wildcardQuery(job,py));4.5范围查询
POST /employees/_search
{query: {range: {salary: {gte: 25000}}}
}范围查询的java方式如下
builder.query(QueryBuilders.rangeQuery(salary).gte(25000));4.6fuzzy模糊查询
GET /employees/_search
{query: {fuzzy: {job: {value: javb,fuzziness: 1 //表示允许错一个字}}}
}模糊查询的java方式如下
builder.query(QueryBuilders.fuzzyQuery(job,javb).fuzziness(Fuzziness.ONE));4.7highlight高亮查询
GET /employees/_search
{query: {term: {job: {value: java}}},highlight: {fields: {*:{}}}
}高亮查询的java方式如下
builder.query(QueryBuilders.termQuery(job,java));
HighlightBuilder highlightBuilder new HighlightBuilder();
highlightBuilder.field(job);
builder.highlighter(highlightBuilder);5聚合查询
5.1aggs聚合查询
先通过job进行分组查询再拿到结果后再进行stats查询求最大值最小值平均值等
POST /employees/_search
{size: 0,aggs: {name: {terms: {field: job},aggs: {stats_salary: {stats: {field: salary}}}}}
}其java代码如下需要注意的点就是如果存在二级聚合那么需要调用这个 subAggregation 方法如果只需要聚合的结果而不需要查询的结果可以直接在SearchSourceBuilder的实例设置为0即可。
Test
public void toAgg() throws Exception{//创建检索请求SearchRequest searchRequest new SearchRequest();//指定索引searchRequest.indices(employees);//构建检索条件SearchSourceBuilder builder new SearchSourceBuilder();//构建聚合条件TermsAggregationBuilder aggregationBuilder AggregationBuilders.terms(jobData).field(job);aggregationBuilder.subAggregation(AggregationBuilders.stats(salaryData).field(salary));//将聚合条件加入到检索条件中builder.aggregation(aggregationBuilder);//只要聚合的结果不需要查询的结果builder.size(0);searchRequest.source(builder);//执行检索SearchResponse searchResponse client.search(searchRequest, RequestOptions.DEFAULT);System.out.println(检索结果 searchResponse);
}打印的结果如下和预期要打印的结果是一致的
{took:4,timed_out:false,_shards:{total:1,successful:1,skipped:0,failed:0},hits:{total:{value:10,relation:eq},max_score:null,hits:[]},aggregations:{sterms#jobData:{doc_count_error_upper_bound:0,sum_other_doc_count:0,buckets:[{key:java,doc_count:4,stats#salaryData:{count:4,min:9000.0,max:50000.0,avg:25750.0,sum:103000.0}},{key:c,doc_count:2,stats#salaryData:{count:2,min:20000.0,max:20000.0,avg:20000.0,sum:40000.0}},{key:javascript,doc_count:2,stats#salaryData:{count:2,min:18000.0,max:25000.0,avg:21500.0,sum:43000.0}},{key:python,doc_count:2,stats#salaryData:{count:2,min:18000.0,max:35000.0,avg:26500.0,sum:53000.0}}]}}}除了上面的state求全部的最大值最小值等还可以分别的求最大值最小值平均值个数等求平均值的的示例如下需要使用到这个 AvgAggregationBuilder 构造器
AvgAggregationBuilder avgAggregationBuilder AggregationBuilders.avg(salaryData).field(salary);
//将聚合条件加入到检索条件中
builder.aggregation(avgAggregationBuilder);求最大值的示例如下需要使用到这个 MaxAggregationBuilder 构造器
MaxAggregationBuilder maxAggregationBuilder AggregationBuilders.max(maxData).field(salary);
//将聚合条件加入到检索条件中builder.aggregation(maxAggregationBuilder);求最小值的示例如下需要使用到这个 MinAggregationBuilder 构造器
MinAggregationBuilder minAggregationBuilder AggregationBuilders.min(minData).field(salary);
//将聚合条件加入到检索条件中
builder.aggregation(minAggregationBuilder);求总个数的示例如下需要使用到这个 ValueCountAggregationBuilder 构造器
ValueCountAggregationBuilder countBuilder AggregationBuilders.count(countData).field(salary);
//将聚合条件加入到检索条件中
builder.aggregation(countBuilder);5.2获取最终结果
上面在查询之后会获取 SearchResponse 的对象这里面就值执行查询后返回的结果
SearchResponse searchResponse随后可以直接过滤结果通过for循环去遍历这个 getHits
SearchHits hits searchResponse.getHits();
SearchHit[] searchHits hits.getHits();
for (SearchHit searchHit : searchHits) {String sourceAsString searchHit.getSourceAsString();Employees employees JSON.parseObject(sourceAsString, Employees.class);System.out.println(employees);}或者直接获取聚合操作结果的值
//获取jobData聚合。还有Avg、Max、Min等
Terms maxData aggregations.get(jobData);
for (Terms.Bucket bucket : maxData.getBuckets()) {String keyAsString bucket.getKeyAsString();System.out.println(job职业 keyAsString 数量 bucket.getDocCount());
}
文章转载自: http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.yznsx.cn.gov.cn.yznsx.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.cniedu.com.gov.cn.cniedu.com http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.cwqrj.cn.gov.cn.cwqrj.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.klpwl.cn.gov.cn.klpwl.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.wcjgg.cn.gov.cn.wcjgg.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.gxhqt.cn.gov.cn.gxhqt.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.qjldz.cn.gov.cn.qjldz.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.crkmm.cn.gov.cn.crkmm.cn