elasticsearch 5.x java doc
程序员文章站
2022-07-05 09:04:17
...
//查询
String index = "客户表_"+公司ID;//es 索引
String type = "good"; //es type
//查询组装
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//简单查询
// 等于
boolQueryBuilder.must(QueryBuilders.termQuery(fieldName, value));
// 多个值
boolQueryBuilder.must(QueryBuilders.termsQuery(fieldName, value));
// 不等于
boolQueryBuilder.mustNot(QueryBuilders.termQuery(fieldName, value));
// 包含
boolQueryBuilder.must(QueryBuilders.wildcardQuery(fieldName, "*" + value + "*"));
// 不包含
boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(fieldName, "*" + value + "*"));
// 有值
boolQueryBuilder.must(QueryBuilders.existsQuery(fieldName));
// 没有值
boolQueryBuilder.mustNot(QueryBuilders.existsQuery(fieldName));
// 数字范围
boolQueryBuilder.must(QueryBuilders.rangeQuery(fieldName).gt(dateStart).lt(dateEnd));
// 日期
// 绝对时间
boolQueryBuilder.must(QueryBuilders.rangeQuery(fieldName).format("yyyy-MM-dd").timeZone("+08:00").gte(dateStart).lte(dateEnd));
//查询复杂条件
boolQueryBuilder.must(QueryBuilders.nestedQuery("even", QueryBuilders.termQuery("even.111", 222), ScoreMode.None));
//has_child:基于子文档的内容,查找父文档
boolQueryBuilder.must(JoinQueryBuilders.hasChildQuery("type",QueryBuilders.termQuery("openid","111"), ScoreMode.None));
boolQueryBuilder.must(QueryBuilders.termQuery("mobile","186666666"));
System.out.println("has_child:基于子文档的内容,查找父文档"+boolQueryBuilder);
//has_child:基于子文档的内容,查找父文档
boolQueryBuilder.must(JoinQueryBuilders.hasParentQuery(index,QueryBuilders.termsQuery("mobile","186666666"),true));
boolQueryBuilder.must(QueryBuilders.termQuery("openid","111"));
SearchResponse searchResponse = client.prepareSearch(index)
.setTypes(type)
.setQuery(boolQueryBuilder)
.addSort(SortBuilders.fieldSort("_doc"))
.setSize(10000)
// 这个游标维持多长时间
.setScroll(TimeValue.timeValueMinutes(15)).execute().actionGet();
// 发送量
Long count = searchResponse.getHits().getTotalHits();
for (SearchHit hit : searchResponse.getHits()) {
System.out.println(hit.getId()); //_id
System.out.println(hit.getSource());//数据
}
Map<String,Object> map = new HashMap();
map.put("name","张三");
//单条新增
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,"event_two");
indexRequestBuilder.setSource(map);
indexRequestBuilder.setId("222222222");
indexRequestBuilder.execute();
//单条修改
UpdateRequestBuilder requestBuilder = client.prepareUpdate(index,type,"AWReEq5GvJs95AmUKpif");
requestBuilder.setDoc(map);
requestBuilder.execute();
//父子级别新增
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,type);
indexRequestBuilder.setSource(map);
indexRequestBuilder.setParent("222222222");
indexRequestBuilder.setId("222222222");
indexRequestBuilder.execute();
//父子集修改
UpdateRequestBuilder requestBuilder = client.prepareUpdate(index,type,"AWReEq5GvJs95AmUKpif");
requestBuilder.setDoc(map);
requestBuilder.setParent("222222222");
requestBuilder.execute();
批量增加
String index = "客户表_"+公司ID;//es 索引
String type = "good"; //es type
//获取默认组
BulkRequestBuilder EsBulkRequest = client.prepareBulk();
//同步es
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index,type);
indexRequestBuilder.setSource(map).setId(uuid.toString());
EsBulkRequest.add(indexRequestBuilder);
UpdateRequestBuilder updateBuilder = client.prepareUpdate(index,type,"_id");
updateBuilder.setDoc(map);
EsBulkRequest.add(updateBuilder);
//保存
if (EsBulkRequest.numberOfActions()!= 0){
bulkWriteOperation.execute();
EsBulkRequest.execute();
}
上一篇: 操作系统实验之进程调度--先来先服务、优先级、时间片轮转
下一篇: 多线程互斥-读写者问题
推荐阅读
-
Java利用openoffice将doc、docx转为pdf实例代码
-
Java代码解决ElasticSearch的Result window is too large问题
-
Elasticsearch Java Rest Client API 整理总结 (一)
-
Java中使用elasticsearch搜索引擎实现简单、修改等操作
-
2020 java Elasticsearch面试题及答案(最全版本持续更新)
-
Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
-
Java程序中Doc文档注释示例教程
-
2020最新Java工程师面试题-Elasticsearch 面试篇(附答案)
-
elasticsearch FunctionScore Java API sample
-
elasticsearch doc和segment原理