我使用的是ElasticSearch7.3.0但是java中是 ElasticSearch7.5.1这个不影响,只要在一个大的版本下就行7X
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<elasticsearch>7.5.1</elasticsearch>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch}</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
</dependencies>
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client=new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.81.141", 9200, "http")));
return client;
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
;import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/** * es 的工具类 * * @author czchen * @version 1.0 * @date 2020/8/25 14:37 */
@Slf4j
@Component
public class EsUtil {
@Autowired
private RestHighLevelClient restHighLevelClient;
public String numberoFshards="5";//分片数
public String numberoFreplicas="1";//副本数1
/** * 关键字 */
public static final String KEYWORD = ".keyword";
/** * 创建索引 * * @param index 索引 * @return */
public boolean createIndex(String index) throws IOException {
if(isIndexExist(index)){
log.error("Index is exits!");
return false;
}
//1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.put("index.number_of_shards",numberoFshards)
.put("index.number_of_replicas", numberoFreplicas));//副本数
// request.alias(new Alias(index+"alias"));//设置别名
//2.执行客户端请求
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
log.info("创建索引{}成功",index);
return response.isAcknowledged();
}
/** * 创建索引的同时映射Mapping * * @param index 索引 * @return */
public boolean createIndexAndMapping(String index ,XContentBuilder xContentBuilder) throws IOException {
//创建索引
boolean index1 = createIndex(index);
// 创建索引的映射
boolean mapping = createMapping(index, xContentBuilder);
log.info("创建索引{}成功",index);
log.info("创建索引的映射{}成功",mapping);
return index1;
}
/** * 给指定的索引添加映射 * @param index 索引 * @param xContentBuilder 映射信息 * @return * @throws IOException */
public boolean createMapping(String index ,XContentBuilder xContentBuilder) throws IOException {
if(!isIndexExist(index)){ //如果索引不存在那么报错
log.error("Index is exits!");
return false;
}
//1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest(index);
//2.执行客户端请求
// CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
PutMappingRequest mapping = Requests.putMappingRequest(index).type("_doc").source(xContentBuilder);
System.out.println(mapping);
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().putMapping(mapping, RequestOptions.DEFAULT);
log.info("创建索引{}成功",index);
log.info("创建映射{}成功",acknowledgedResponse.isAcknowledged());
return acknowledgedResponse.isAcknowledged();
}
/** * 删除索引 * * @param index * @return */
public boolean deleteIndex(String index) throws IOException {
if(!isIndexExist(index)) {
log.error("Index is not exits!");
return false;
}
//删除索引请求
DeleteIndexRequest request = new DeleteIndexRequest(index);
//执行客户端请求
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
log.info("删除索引{}成功",index);
return delete.isAcknowledged();
}
/** * 判断索引是否存在 * * @param index * @return */
public boolean isIndexExist(String index) throws IOException {
GetIndexRequest request = new GetIndexRequest(index);
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
return exists;
}
/** * 数据添加,正定ID * * @param json 要增加的数据 * @param index 索引,类似数据库 * @param id 数据ID, 为null时es随机生成 * @return */
public String addData(String json, String index, String id) throws IOException {
//创建请求
IndexRequest request = new IndexRequest(index);
//规则 put /test_index/_doc/1
request.id(id);
request.timeout(TimeValue.timeValueSeconds(1));
//将数据放入请求 json
IndexRequest source = request.source(JSON.parseObject(json), XContentType.JSON);
//客户端发送请求
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
log.info("添加数据成功 索引为: {}, response 状态: {}, id为: {}",index,response.status().getStatus(), response.getId());
return response.getId();
}
/** * 数据添加 随机id * * @param json 要增加的数据 * @param index 索引,类似数据库 * @return */
public String addData(String json, String index) throws IOException {
return addData(json, index, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
}
/** * 通过ID删除数据 * * @param index 索引,类似数据库 * @param id 数据ID */
public void deleteDataById(String index, String id) throws IOException {
//删除请求
DeleteRequest request = new DeleteRequest(index, id);
//执行客户端请求
DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
log.info("索引为: {}, id为: {}删除数据成功",index, id);
}
/** * 通过ID 更新数据 * * @param json 要增加的数据 * @param index 索引,类似数据库 * @param id 数据ID * @return */
public void updateDataById(String json, String index, String id) throws IOException {
//更新请求
UpdateRequest update = new UpdateRequest(index, id);
update.timeout("1s");
update.doc(json, XContentType.JSON);
//执行更新请求
UpdateResponse update1 = restHighLevelClient.update(update, RequestOptions.DEFAULT);
log.info("索引为: {}, id为: {}, 更新数据成功",index, id);
}
/** * 通过ID 更新数据,保证实时性 * * @param json 要增加的数据 * @param index 索引,类似数据库 * @param id 数据ID * @return */
public void updateDataByIdNoRealTime(String json, String index, String id) throws IOException {
//更新请求
UpdateRequest update = new UpdateRequest(index, id);
//保证数据实时更新
update.setRefreshPolicy("wait_for");
update.timeout("1s");
update.doc(json, XContentType.JSON);
//执行更新请求
UpdateResponse update1 = restHighLevelClient.update(update, RequestOptions.DEFAULT);
log.info("索引为: {}, id为: {}, 更新数据成功",index, id);
}
/** * 通过ID获取数据 * * @param index 索引,类似数据库 * @param id 数据ID * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) * @return */
public Map<String,Object> searchDataById(String index, String id, String fields) throws IOException {
GetRequest request = new GetRequest(index, id);
if (StringUtils.isNotEmpty(fields)){
//只查询特定字段。如果需要查询所有字段则不设置该项。
request.fetchSourceContext(new FetchSourceContext(true,fields.split(","), Strings.EMPTY_ARRAY));
}
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
Map<String, Object> map = response.getSource();
//为返回的数据添加id
map.put("id",response.getId());
return map;
}
/** * 通过ID判断文档是否存在 * @param index 索引,类似数据库 * @param id 数据ID * @return */
public boolean existsById(String index,String id) throws IOException {
GetRequest request = new GetRequest(index, id);
//不获取返回的_source的上下文
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
return restHighLevelClient.exists(request, RequestOptions.DEFAULT);
}
/** * 获取低水平客户端 * @return */
public RestClient getLowLevelClient() {
return restHighLevelClient.getLowLevelClient();
}
/** * 高亮结果集 特殊处理 * map转对象 JSONObject.parseObject(JSONObject.toJSONString(map), Content.class) * @param searchResponse * @param highlightField */
private List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
//解析结果
ArrayList<Map<String,Object>> list = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits().getHits()) {
Map<String, HighlightField> high = hit.getHighlightFields();
HighlightField title = high.get(highlightField);
hit.getSourceAsMap().put("id", hit.getId());
Map<String, Object> sourceAsMap = hit.getSourceAsMap();//原来的结果
//解析高亮字段,将原来的字段换为高亮字段
if (title!=null){
Text[] texts = title.fragments();
String nTitle="";
for (Text text : texts) {
nTitle+=text;
}
//替换
sourceAsMap.put(highlightField,nTitle);
}
list.add(sourceAsMap);
}
return list;
}
/** * 查询并分页 * @param index 索引名称 * @param query 查询条件 * @param size 文档大小限制 * @param from 从第几页开始 * @param fields 需要显示的字段,逗号分隔(缺省为全部字段) * @param sortField 排序字段 (缺省不排序) * @param highlightField 高亮字段 * @return */
public List<Map<String, Object>> searchListData(String index,
SearchSourceBuilder query,
Integer size,
Integer from,
String fields,
String sortField,
String highlightField) throws IOException {
SearchRequest request = new SearchRequest(index);
SearchSourceBuilder builder = query;
if (StringUtils.isNotEmpty(fields)){
//只查询特定字段。如果需要查询所有字段则不设置该项。
builder.fetchSource(new FetchSourceContext(true,fields.split(","), Strings.EMPTY_ARRAY));
}
from = from <= 0 ? 0 : from*size;
//设置确定结果要从哪个索引开始搜索的from选项,默认为0
builder.from(from);
builder.size(size);
if (StringUtils.isNotEmpty(sortField)){
//排序字段,注意如果proposal_no是text类型会默认带有keyword性质,需要拼接.keyword
builder.sort(sortField+".keyword", SortOrder.ASC);
}
//高亮
HighlightBuilder highlight = new HighlightBuilder();
highlight.field(highlightField);
//关闭多个高亮
highlight.requireFieldMatch(false);
highlight.preTags("<span style='color:red'>");
highlight.postTags("</span>");
builder.highlighter(highlight);
//不返回源数据。只有条数之类的数据。
//builder.fetchSource(false);
request.source(builder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
log.info("=="+response.getHits().getTotalHits()+"数量");
if (response.status().getStatus() == 200) {
// 解析对象
return setSearchResponse(response, highlightField);
}
return null;
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsApplication {
public static void main(String[] args) {
SpringApplication.run(EsApplication.class,args);
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.es.EsApplication;
import com.es.utils.EsUtil;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EsAppTest {
@Autowired
EsUtil esUtil;
@Test
public void test0() { //获取客户端的ip和端口
System.out.println( esUtil.getLowLevelClient().getNodes());
}
// 判断索引是否存在
@Test
public void test1() throws IOException {
System.out.println( esUtil.isIndexExist("index1"));
}
// 添加索引,分词器自己手动使用Postman进行映射
@Test
public void test4() throws IOException {
System.out.println( esUtil.createIndex("index12"));
}
// 添加索引同时映射字段, 比如:name添加ik分词器
@Test
public void test21() throws IOException {
System.out.println( esUtil.createIndexAndMapping("index15", getMapping()));
}
//给指定的索引添加映射
@Test
public void test11() throws IOException {
System.out.println( esUtil.createMapping("index13",getMapping()));
}
private XContentBuilder getMapping(){
XContentBuilder mapping = null;
try {
mapping = jsonBuilder().startObject()
//--- 固定
.startObject("properties")
// ----自行补充
.startObject("id").field("type","long").endObject()
.startObject("title").field("type","text").field("analyzer", "ik_max_word").endObject()
.startObject("description").field("type","text").endObject()
.startObject("url").field("type","text").endObject()
.startObject("type").field("type","integer").endObject()
//--- 固定
.endObject()
.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return mapping;
}
// 删除索引
@Test
public void test5() throws IOException {
System.out.println( esUtil.deleteIndex("index12"));
}
// 数据添加不指定文档ID 系统自动生成 UUID形式的文档ID,然后返回
@Test
public void test6() throws IOException {
String json="{\n" +
"\"name\": \"胡1\",\n" +
"\"age\": \"221\",\n" +
"\"like\": \"你好世界1\",\n" +
"\"desc\": \"ES初学者1\",\n" +
"\"time2\": \"2021-02-11 22:46:11\"\n" +
"}";
System.out.println( esUtil.addData(json,"index1"));
}
// 数据添加指定文档ID
@Test
public void test7() throws IOException {
String json="{\n" +
"\"name\": \"胡111\",\n" +
"\"age\": \"22111\",\n" +
"\"like\": \"你好世界1111\",\n" +
"\"desc\": \"ES初学者1111\",\n" +
"\"time2\": \"2021-02-11 22:46:11\"\n" +
"}";
System.out.println( esUtil.addData(json,"index1","1001"));
}
// 通过数据文档ID删除数据
@Test
public void test8() throws IOException {
esUtil.deleteDataById("index1","22");
}
//通过文档ID 更新数据
@Test
public void test9() throws IOException {
String json="{\n" +
"\"name\": \"胡2222\",\n" +
"\"age\": \"222222211\",\n" +
"\"like\": \"你好世界1222111\",\n" +
"\"desc\": \"ES初学者112222211\",\n" +
"\"time2\": \"2021-02-11 22:46:11\"\n" +
"}";
esUtil.updateDataById(json,"index1","100");
}
//通过文档ID 更新数据 (实时)
@Test
public void test10() throws IOException {
String json="{\n" +
"\"name\": \"胡13511\",\n" +
"\"age\": \"3333333\",\n" +
"\"like\": \"你333\",\n" +
"\"desc\": \"ES33333331\",\n" +
"\"time2\": \"2021-02-11 22:46:11\"\n" +
"}";
esUtil.updateDataByIdNoRealTime(json,"index1","100");
}
//查询指定索引下id
@Test
public void test2() throws IOException {
System.out.println( esUtil.searchDataById("index1","22",""));
}
//自定义查询 -> 分页->排序 -> 高亮
@Test
public void test3() throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery("你好").field("like");
sourceBuilder.query(queryBuilder);
System.out.println( esUtil.searchListData("index1",sourceBuilder,20,0,"","","like"));
}
}
单个匹配
//不分词查询 参数1: 字段名,参数2:字段查询值,因为不分词,所以汉字只能查询一个字,英语是一个单词.
QueryBuilder queryBuilder=QueryBuilders.termQuery("fieldName", "fieldlValue");
//分词查询
QueryBuilder queryBuilder2 = QueryBuilders.matchQuery("fieldName", "fieldlValue");
多个匹配
//不分词查询,参数1: 字段名,参数2:多个字段查询值,因为不分词,所以汉字只能查询一个字,英语是一个单词.
QueryBuilder queryBuilder=QueryBuilders.termsQuery("fieldName", "fieldlValue1","fieldlValue2...");
//分词查询,采用默认的分词器
QueryBuilder queryBuilder= QueryBuilders.multiMatchQuery("fieldlValue", "fieldName1", "fieldName2", "fieldName3");
//匹配所有文件,相当于就没有设置查询条件
QueryBuilder queryBuilder=QueryBuilders.matchAllQuery();
模糊查询常见的5个方法如下
//1.常用的字符串查询
QueryBuilders.queryStringQuery("fieldValue").field("fieldName");//左右模糊
//2.常用的用于推荐相似内容的查询 , 如果不指定filedName,则默认全部,常用在相似内容的推荐上
QueryBuilders.moreLikeThisQuery(new String[] {"fieldName"}).addLikeText("pipeidhua");
//3.前缀查询 如果字段没分词,就匹配整个字段前缀
QueryBuilders.prefixQuery("fieldName","fieldValue");
//4.fuzzy query:分词模糊查询,通过增加fuzziness模糊属性来查询,如能够匹配hotelName为tel前或后加一个字母的文档,fuzziness 的含义是检索的term 前后增加或减少n个单词的匹配查询
QueryBuilders.fuzzyQuery("hotelName", "tel").fuzziness(Fuzziness.ONE);
//5.wildcard query:通配符查询,支持* 任意字符串;?任意一个字符
QueryBuilders.wildcardQuery("fieldName","ctr*");//前面是fieldname,后面是带匹配字符的字符串
QueryBuilders.wildcardQuery("fieldName","c?r?");
//闭区间查询 2≤x≤5 此为闭区间
QueryBuilder queryBuilder0 = QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2");
//开区间查询 2<x<5 此为开区间
QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("fieldName").from("fieldValue1").to("fieldValue2").includeUpper(false).includeLower(false);//默认是true,也就是包含
//大于
QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("fieldName").gt("fieldValue");
//大于等于
QueryBuilder queryBuilder3 = QueryBuilders.rangeQuery("fieldName").gte("fieldValue");
//小于
QueryBuilder queryBuilder4 = QueryBuilders.rangeQuery("fieldName").lt("fieldValue");
//小于等于
QueryBuilder queryBuilder5 = QueryBuilders.rangeQuery("fieldName").lte("fieldValue");
QueryBuilders.boolQuery()
QueryBuilders.boolQuery().must();//文档必须完全匹配条件,相当于and
QueryBuilders.boolQuery().mustNot();//文档必须不匹配条件,相当于not
QueryBuilders.boolQuery().should();//至少满足一个条件,这个文档就符合should,相当于or
https://zhuanlan.zhihu.com/p/183816335 ElasticSearch常用查询及聚合分析 (注意把类型去掉7x默认类型是_doc)
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/index.html 官方文档
https://www.cnblogs.com/leeSmall/p/9218779.html (注意把类型去掉7x默认类型是_doc)
server:
port: 10121
es:
ip: 192.168.81.143
port: 9200
scheme: http
package com.es;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsApplication {
public static void main(String[] args) {
SpringApplication.run(EsApplication.class,args);
}
}
package com.es.builder;
public enum BootEnum{
must,should,must_not,filter
}
(索引和映射)
package com.es.builder;
import com.alibaba.fastjson.JSONObject;
public class EsPropertiesBuilder{
private JSONObject jsonObject=new JSONObject(100);
private String field;
private EsPropertiesBuilder(){
jsonObject.put("properties",new JSONObject());
}
public static EsPropertiesBuilder jsonBuilder(){
return new EsPropertiesBuilder();
}
public EsPropertiesBuilder fieldName(String field){
this.field=field;
JSONObject properties = (JSONObject)jsonObject.get("properties");
properties.put(field,new JSONObject());
return this;
}
public EsPropertiesBuilder fieldType(String type,String value){
JSONObject properties = (JSONObject)jsonObject.get("properties");
JSONObject f = (JSONObject) properties.get(field);
f.put(type,value);
return this;
}
public JSONObject end(){
return jsonObject;
}
}
(索引分片)
package com.es.builder;
import com.alibaba.fastjson.JSONObject;
public class EsSettingsBuilder{
private JSONObject jsonObject=new JSONObject(100);
private int numberOfShards=5; //分片
private int numberOfReplicas=1; //副本
public EsSettingsBuilder(int numberOfShards, int numberOfReplicas) {
jsonObject.put("settings",new JSONObject());
this.numberOfShards = numberOfShards;
this.numberOfReplicas = numberOfReplicas;
numberOfShards();
numberOfReplicas();
}
private EsSettingsBuilder(){
jsonObject.put("settings",new JSONObject());
numberOfShards();
numberOfReplicas();
}
public static EsSettingsBuilder jsonBuilder(){
return new EsSettingsBuilder();
}
public static EsSettingsBuilder jsonBuilder(int numberOfShards, int numberOfReplicas){
return new EsSettingsBuilder(numberOfShards,numberOfReplicas);
}
//分片个数,在创建索引不指定时 默认为 5;
public EsSettingsBuilder numberOfShards(){
JSONObject properties = (JSONObject)jsonObject.get("settings");
properties.put("number_of_shards",numberOfShards);
return this;
}
//#数据副本,一般设置为1;
public EsSettingsBuilder numberOfReplicas(){
JSONObject properties = (JSONObject)jsonObject.get("settings");
properties.put("number_of_replicas",numberOfReplicas);
return this;
}
public JSONObject end(){
return jsonObject;
}
}
测试实体类
package com.es.entity;
import lombok.Data;
@Data
public class User {
private String name ;
private int age ;
private String doc ;
}
package com.es.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.es.builder.EsPropertiesBuilder;
import com.es.builder.EsSelectBuilder;
import com.es.builder.EsSettingsBuilder;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;
@Service
@Data
@Slf4j
public class EsUtils {
private String ip;
private String port;
private String scheme;
private String es;
@SneakyThrows
public EsUtils(@Value("${es.ip}") String ip, @Value("${es.port}") String port, @Value("${es.scheme}") String scheme) {
this.ip = ip;
this.port = port;
this.scheme = scheme;
es = scheme + "://" + ip + ":" + port + "/";
log.info("ES地址:" + es);
//获取ES的状态
final String status = HttpUtils.doGet(es + "_cat/health?v", null);
if (status != null && !"".equals(status)) {
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(status.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
br.readLine(); //跳过标题
String s = br.readLine().split("\\s+")[3]; //获取当前ES的状态
if ("red".equals(s)) {
throw new Exception("ES 异常 请检查ES 是否状态良好");
}
}
}
// 查询索引是否存在 并且是否可用
@SneakyThrows
public boolean isIndex(String esIndex) {
final String index = HttpUtils.doGet(es + "_cat/indices?v", null);
if (index != null && !"".equals(index)) {
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(index.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
String line;
List<String[]> list = new ArrayList<>(100);
br.readLine();//跳过标题
while ((line = br.readLine()) != null) {
list.add(line.trim().split("\\s+"));
}
for (String[] strings : list) {
if (!"red".equals(strings[0]) && "open".equals(strings[1]) && esIndex.equals(strings[2])) {
return true;
}
}
}
return false;
}
//创建索引
@SneakyThrows
public boolean createdIndex(String index) {
if (!isIndex(index)) { //只有索引不存在,那麽我們就可以創建索引
//创建索引的同时添加默认分片和副本
String s = HttpUtils.doPut(es + index, JSON.toJavaObject(EsSettingsBuilder.jsonBuilder().end(), Map.class));
return (boolean) JSON.parseObject(s).get("acknowledged");
}
return false;
}
//创建索引的别名 ,一个索引可以创建多个别名
@SneakyThrows
public boolean createdIndexAliases(String index, String... alias) {
if (isIndex(index)) { //只有索引存在,那麽我們就可以創建别名
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("index", index);
jsonObject2.put("aliases", alias);
jsonObject1.put("add", jsonObject2);
jsonArray.add(jsonObject1);
jsonObject.put("actions", jsonArray);
String s = HttpUtils.doPost(es + "_aliases", JSON.parseObject(jsonObject.toString(), Map.class));
return (boolean) JSON.parseObject(s).get("acknowledged");
}
return false;
}
// 查询所有的索引中,是否有索引存在指定别名
@SneakyThrows
public boolean isAliase(String aliase) {
String s = HttpUtils.doGet(es + "_aliases", null);
Map map = JSON.parseObject(s, Map.class);
for (Object o : map.keySet()) {
JSONObject jsonObject = (JSONObject) map.get(o);
JSONObject jsonObject1 = (JSONObject) jsonObject.get("aliases");
Map map1 = JSON.parseObject(jsonObject1.toString(), Map.class);
for (Object o1 : map1.keySet()) {
if (aliase.equals(o1)) {
return true;
}
}
}
return false;
}
// 查询指定索引中,是否有存在指定别名
@SneakyThrows
public boolean isIndexAliase(String index, String aliase) {
if (isIndex(index)) {
String s = HttpUtils.doGet(es + "_aliases", null);
Map map = JSON.parseObject(s, Map.class);
for (Object o : map.keySet()) {
if (!o.equals(index)) {
continue;
}
JSONObject jsonObject = (JSONObject) map.get(o);
JSONObject jsonObject1 = (JSONObject) jsonObject.get("aliases");
Map map1 = JSON.parseObject(jsonObject1.toString(), Map.class);
for (Object o1 : map1.keySet()) {
if (aliase.equals(o1)) {
return true;
}
}
}
}
return false;
}
//创建索引的别名 ,一个索引可以创建多个别名
@SneakyThrows
public boolean deIndexAliases(String index, String... alias) {
if (isIndex(index)) { //只有索引存在,那麽我們就可以創建别名
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (String s : alias) {
if (isAliase(s)) { //如果別名存在那麽跳过
continue;
}
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("index", index);
jsonObject2.put("alias", s);
jsonObject1.put("remove", jsonObject2);
jsonArray.add(jsonObject1);
}
jsonObject.put("actions", jsonArray);
String s = HttpUtils.doPost(es + "_aliases", JSON.parseObject(jsonObject.toString(), Map.class));
return (boolean) JSON.parseObject(s).get("acknowledged");
}
return false;
}
// 别名迁移 , a索引的指定别名迁移到b索引中
@SneakyThrows
public boolean upIndexAliases(String aIndex, String bIndex, String alias) {
if (isIndex(aIndex) && isIndexAliase(aIndex, alias) && isIndex(bIndex)) {
deIndexAliases(aIndex, alias);
createdIndexAliases(bIndex, alias);
}
return true;
}
//索引分组(多个索引指向同一别名)
@SneakyThrows
public boolean createdIndexAliasesGroup(String alias, String... index) {
for (String s : index) {
if (!isIndex(s)) {
return false;
}
}
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("indices", index);
jsonObject2.put("alias", alias);
jsonObject1.put("add", jsonObject2);
jsonArray.add(jsonObject1);
jsonObject.put("actions", jsonArray);
String s = HttpUtils.doPost(es + "_aliases", JSON.parseObject(jsonObject.toString(), Map.class));
return (boolean) JSON.parseObject(s).get("acknowledged");
}
//创建索引的时候指定分片和副本
@SneakyThrows
public boolean createdIndex(String index, int shards, int replicas) {
if (!isIndex(index)) { //只有索引不存在,那麽我們就可以創建索引
//创建索引的同时添加默认分片和副本
String s = HttpUtils.doPut(es + index, JSON.toJavaObject(EsSettingsBuilder.jsonBuilder(shards, replicas).end(), Map.class));
return (boolean) JSON.parseObject(s).get("acknowledged");
}
return false;
}
//查询索引映射情况
@SneakyThrows
public Map getIndexMapping(String index) {
String doGet = HttpUtils.doGet(es + index, null);
Map map = JSON.parseObject(doGet, Map.class);
return JSON.parseObject(map.get(index).toString(), Map.class);
}
// 在ES 7X以后一个索引只能有一个type 默认是_doc,所以原理上就是改_doc的类型
@SneakyThrows
public boolean createdMapping(String index, EsPropertiesBuilder mapping) {
if (isIndex(index)) { //只有索引存在的时候才能映射
Map map = mapping.end().toJavaObject(Map.class);
String s = HttpUtils.doPut(es + index + "/" + "_mappings", map);
return (boolean) JSON.parseObject(s).get("acknowledged");
}
return false;
}
//创建索引的同时映射Mapping
@SneakyThrows
public boolean createdIndexAndMapping(String index, EsPropertiesBuilder mapping) {
if (createdIndex(index)) {
return createdMapping(index, mapping);
}
return false;
}
//创建索引的同时映射Mapping和指定分片和副本
@SneakyThrows
public boolean createdIndexAndMapping(String index, EsPropertiesBuilder mapping, int shards, int replicas) {
if (createdIndex(index, shards, replicas)) {
return createdMapping(index, mapping);
}
return false;
}
// 删除索引
@SneakyThrows
public boolean deIndex(String index) {
if (isIndex(index)) { //只有索引存在的时候才能删除
String doDelete = HttpUtils.doDelete(es + index, null);
return (boolean) JSON.parseObject(doDelete).get("acknowledged");
}
return false;
}
// 批量删除索引
@SneakyThrows
public boolean deIndexs(String[] index) {
for (String s : index) {
if (isIndex(s)) { //只有索引存在的时候才能删除
String doDelete = HttpUtils.doDelete(es + index, null);
return (boolean) JSON.parseObject(doDelete).get("acknowledged");
}
}
return false;
}
// 清空ES全部索引
@SneakyThrows
public boolean deIndexsAll() {
String doDelete = HttpUtils.doDelete(es + "_all", null);
return (boolean) JSON.parseObject(doDelete).get("acknowledged");
}
//添加一条数据 (指定行的id)
@SneakyThrows
public String addDocOne(String index, Object docId, Object object) {
if (isIndex(index) || isAliase(index)) {//只有索引或者別名存在的时候才能操作
JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
String s = HttpUtils.doPost(es + index + "/_doc/" + docId, JSON.toJavaObject(jsonObject, Map.class));
return (String) JSON.parseObject(s).get("_id");
}
return "0";
}
//添加一条数据 (随机行的id)
@SneakyThrows
public String addDocOne(String index, Object object) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
String s = HttpUtils.doPost(es + index + "/_doc/" + UUID.randomUUID(), JSON.toJavaObject(jsonObject, Map.class));
return (String) JSON.parseObject(s).get("_id");
}
return "0";
}
//批量添加条数据 (docid 随机生成)
/** * @param index 索引 * @param object 数据 * @param count 每次添加多少条 (主要是防止内存泄漏) 默认5条 一次写入(不够5条也会写入) 比如100条数据,那么底层代码就会写入20次 * @param capacity StringBuilder(容器大小) 主要防止不断开辟容器空间导致速度变慢 默认1000字符 new char[1000] * @return 执行结果 */
@SneakyThrows
public boolean addDocMuch(String index, List<Object> object, int count, int capacity) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
count = count <= 1 ? 5 : count;
capacity = capacity <= 1000 ? 2000 : capacity;
StringBuilder stringBuilder = new StringBuilder(capacity);
int count1 = count;
try {
for (Object o : object) {
stringBuilder.append("{\"index\":{\"_index\":\"" + index + "\",\"_type\":\"_doc\",\"_id\":\"" + UUID.randomUUID() + "\"}}" + "\n");
stringBuilder.append(JSON.toJSON(o) + "\n");
count--;
if (count <= 0) {
String s = HttpUtils.doPost(es + index + "/_doc/_bulk", stringBuilder.toString());
stringBuilder = null; //清空
count = count1;//计数从新开始
}
}
if (stringBuilder != null) {
String s = HttpUtils.doPost(es + index + "/_doc/_bulk", stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
//批量删除条数据 (依据文档id删除)
/** * @param index 索引 * @param docId 要删除的数据_id * @param count 每次添加多少条 (主要是防止内存泄漏) 默认5条 一次写入(不够5条也会写入) 比如100条数据,那么底层代码就会写入20次 * @param capacity StringBuilder(容器大小) 主要防止不断开辟容器空间导致速度变慢 默认1000字符 new char[1000] * @return 执行结果 */
@SneakyThrows
public boolean deDocMuch(String index, List<Object> docId, int count, int capacity) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
count = count <= 1 ? 5 : count;
capacity = capacity <= 1000 ? 2000 : capacity;
StringBuilder stringBuilder = new StringBuilder(capacity);
int count1 = count;
try {
for (Object o : docId) {
stringBuilder.append("{\"delete\":{\"_index\":\"" + index + "\",\"_type\":\"_doc\",\"_id\":\"" + o + "\"}}" + "\n");
count--;
if (count <= 0) {
String s = HttpUtils.doPost(es + index + "/_doc/_bulk", stringBuilder.toString());
stringBuilder = null; //清空
count = count1;//计数从新开始
}
}
if (stringBuilder != null) {
HttpUtils.doPost(es + index + "/_doc/_bulk", stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return true;
}
//删除-条数据
@SneakyThrows
public String deDocOne(String index, Object docId) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
String s = HttpUtils.doDelete(es + index + "/_doc/" + docId, null);
return (String) JSON.parseObject(s).get("_id");
}
return "0";
}
//查询-条数据 数据在_source里 get("_source")
@SneakyThrows
public Map getDocOne(String index, Object docId) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
String s = HttpUtils.doGet(es + index + "/_doc/" + docId, null);
return JSON.parseObject(s, Map.class);
}
return null;
}
//查询-批_id数据 数据在_source里 get("_source")
@SneakyThrows
public List<Map> getDocMuch(String index, List<Object> docIds) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
Map map = new HashMap();
map.put("ids", docIds);
String s = HttpUtils.doPost(es + index + "/_doc/_mget", map);
return (List<Map>) JSON.parseObject(s, Map.class).get("docs");
}
return null;
}
//更新_id数据
@SneakyThrows
public String upDocOne(String index, Object docIds, Object obj) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
Map map = new HashMap();
map.put("doc", JSON.parseObject(JSON.toJSON(obj).toString(), Map.class));
String s = HttpUtils.doPost(es + index + "/_doc/" + docIds + "/_update", map);
return (String) JSON.parseObject(s).get("_id");
}
return null;
}
//DSL高级检索(Query) 进行处理只显示内容
@SneakyThrows
public <T> List<T> getQueryDsl(String index, EsSelectBuilder esSelectBuilder, Class<T> c) {
if (isIndex(index) || isAliase(index)) {//只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_search", esSelectBuilder.end().toJavaObject(Map.class));
return getSource((List<Map>) JSON.parseObject(JSON.parseObject(s, Map.class).get("hits").toString(), Map.class).get("hits"), c);
}
return null;
}
//DSL高级检索(Query) 不做任何处理的信息返回,让前端自己弄
@SneakyThrows
public String getQueryDsl(String index, EsSelectBuilder esSelectBuilder) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_search", esSelectBuilder.end().toJavaObject(Map.class));
return s;
}
return null;
}
//DSL高级检索(count) 查询结果的数量
@SneakyThrows
public int getQueryDslCount(String index, EsSelectBuilder esSelectBuilder) {
if (isIndex(index) || isAliase(index)) { //只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_count", esSelectBuilder.end().toJavaObject(Map.class));
return Integer.parseInt(JSON.parseObject(s,Map.class).get("count").toString());
}
return 0;
}
//_reindex 数据迁移 ,将a索引的数据放入进b索引里
@SneakyThrows
public Long reindex(String old_index, String new_index) {
if (isIndex(old_index) && isIndex(new_index)) { //只有索引存在的时候才能迁移
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("index", old_index);
jsonObject1.put("size", 5000);
jsonObject.put("source", jsonObject1);
JSONObject jsonObject2 = new JSONObject();
jsonObject1.put("index", new_index);
jsonObject1.put("version_type", "internal");
jsonObject.put("dest", jsonObject2);
String s = HttpUtils.doPost(es + "_reindex?slices=auto&refresh", jsonObject);
return (Long) JSON.parseObject(s, Map.class).get("total");
}
return 0L;
}
// List<Map> 转 List<Object>
public static <T> List<T> getSource(List<Map> list, Class<T> c) {
List<T> list1 = new ArrayList<>();
for (Map map : list) {
list1.add(JSON.parseObject(JSON.toJSON(map.get("_source")).toString(), c));
}
return list1;
}
// 将高亮数据替换进_source里
public static <T> List<T> getSourceHighlight(String s, Class<T> c) {
List<T> list1 = new ArrayList<>();
List<Map> maps = (List<Map>) JSON.parseObject(JSON.parseObject(s, Map.class).get("hits").toString(), Map.class).get("hits");
for (Map map : maps) {
Map source = JSON.parseObject(JSON.toJSON(map.get("_source")).toString(), Map.class);
Map highlight = JSON.parseObject(JSON.toJSON(map.get("highlight")).toString(), Map.class);
for (Object o : highlight.keySet()) {
source.put(o,highlight.get(o));
}
list1.add(JSON.parseObject(JSON.toJSON(source).toString(),c));
}
return list1;
}
//聚合查询 结果赛选
@SneakyThrows
public Object getQueryMetric(String index, EsSelectBuilder esSelectBuilder) {
if (isIndex(index) || isAliase(index)) {//只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_search", esSelectBuilder.end().toJavaObject(Map.class));
Map map = JSON.parseObject(JSON.parseObject(s, Map.class).get("aggregations").toString(), Map.class);
Map map1= JSON.parseObject(map.get("value").toString(),Map.class);
return map1.get("value");
}
return null;
}
@SneakyThrows
public String getQueryMetricPercentiles(String index, EsSelectBuilder esSelectBuilder) {
if (isIndex(index) || isAliase(index)) {//只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_search", esSelectBuilder.end().toJavaObject(Map.class));
Map map = JSON.parseObject(JSON.parseObject(s, Map.class).get("aggregations").toString(), Map.class);
return JSON.parseObject(map.get("value").toString(),Map.class).get("values").toString();
}
return null;
}
//聚合查询 结果赛选
@SneakyThrows
public List<JSONObject> getQueryMetricDistinct(String index, EsSelectBuilder esSelectBuilder) {
if (isIndex(index) || isAliase(index)) {//只有索引或者別名存在的时候才能操作
String s = HttpUtils.doPost(es + index + "/_doc/_search", esSelectBuilder.end().toJavaObject(Map.class));
Map map = JSON.parseObject(JSON.parseObject(s, Map.class).get("aggregations").toString(), Map.class);
Map map1= JSON.parseObject(map.get("value").toString(),Map.class);
System.out.println();
return (List<JSONObject> )map1.get("buckets");
}
return null;
}
}
常规 GET POST DELECT PUT 的请求发送
package com.es.utils;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.Map.Entry;
public class HttpUtils {
private static PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
private static RequestConfig requestConfig;
private static final int MAX_TOTAL = 100;
private static final int MAX_TIMEOUT = 7000;
private static final int CONNECT_TIMEOUT = 10000;
private static final int SOCKET_TIMEOUT = 40000;
private static final String CHARSET = "UTF-8";
public HttpUtils() {
}
static {
connMgr.setMaxTotal(100);
connMgr.setDefaultMaxPerRoute(100);
Builder configBuilder = RequestConfig.custom();
configBuilder.setConnectTimeout(10000);
configBuilder.setSocketTimeout(40000);
configBuilder.setConnectionRequestTimeout(7000);
configBuilder.setStaleConnectionCheckEnabled(true);
requestConfig = configBuilder.build();
}
/** * 发送http请求 get post put delete * get 通过url?name=""&&name="" 发送参数 * post 和put ,delete 通过请求体发送参数 json格式 */
public static String httpClient(String postType, String url, String jsonStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
String result = null;
try {
if (postType.equals("Get")) {
//===== get =====//
HttpGet httpGet = new HttpGet(url);
//设置header
httpGet.setHeader("Content-type", "application/json");
httpGet.setHeader("DataEncoding", "UTF-8");
//发送请求
httpResponse = httpClient.execute(httpGet);
} else if(postType.equals("Post")) {
//===== post =====//
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("DataEncoding", "UTF-8");
//增加参数
httpPost.setEntity(new StringEntity(jsonStr.toString(),"UTF-8"));
httpResponse = httpClient.execute(httpPost);
} else if(postType.equals("Put")) {
//===== put =====//
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Content-type", "application/json");
httpPut.setHeader("DataEncoding", "UTF-8");
httpPut.setEntity(new StringEntity(jsonStr.toString(),"UTF-8"));
httpResponse = httpClient.execute(httpPut);
} else if (postType.equals("Delete")) {
//===== delete ===== 不能有参数 //
HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
httpDelete.setHeader("Content-type", "application/json");
httpDelete.setHeader("DataEncoding", "UTF-8");
StringEntity input = new StringEntity(jsonStr.toString(), ContentType.APPLICATION_JSON);
httpDelete.setEntity(input);
httpResponse = httpClient.execute(httpDelete);
}else{
result=null;
System.out.println("没有找到你要的请求");
}
//返回结果
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
// 可以实现 delete 请求 body 传输数据
static class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
@Override
public String getMethod() {
return METHOD_NAME;
}
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() {
super();
}
}
// ------------------------------进阶 get post put delete map集合作为参数 的 请发送
public static String doGet(String url, Map<String, Object> params) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
return result;
} else {
params=params==null?new HashMap<>():params;
List<NameValuePair> pairList = new ArrayList(params.size());
Iterator var4 = params.entrySet().iterator();
while(var4.hasNext()) {
Entry<String, Object> entry = (Entry)var4.next();
NameValuePair pair = new BasicNameValuePair((String)entry.getKey(), entry.getValue().toString());
pairList.add(pair);
}
CloseableHttpResponse response = null;
InputStream instream = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder URIBuilder = new URIBuilder(url);
URIBuilder.addParameters(pairList);
URI uri = URIBuilder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException | URISyntaxException ignored) {
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
if (null != httpclient) {
httpclient.close();
}
}
return result;
}
}
// post请求
public static String doPost(String url, Map<String, Object> map) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
return result;
} else {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
InputStream instream = null;
try {
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("DataEncoding", "UTF-8");
httpPost.setConfig(requestConfig);
Object o = JSON.toJSON(map==null?new HashMap<>():map);
httpPost.setEntity( new StringEntity(o.toString(), "UTF-8"));
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException var14) {
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return result;
}
}
// post请求
public static String doPost(String url, String json) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
return result;
} else {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
InputStream instream = null;
try {
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("DataEncoding", "UTF-8");
httpPost.setConfig(requestConfig);
httpPost.setEntity( new StringEntity(json, "UTF-8"));
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException var14) {
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return result;
}
}
// Put请求
public static String doPut(String url, Map<String, Object> map) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
return result;
} else {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut HttpPut = new HttpPut(url);
CloseableHttpResponse response = null;
InputStream instream = null;
try {
HttpPut.setHeader("Content-type", "application/json");
HttpPut.setHeader("DataEncoding", "UTF-8");
HttpPut.setConfig(requestConfig);
Object o = JSON.toJSON(map==null?new HashMap<>():map);
HttpPut.setEntity( new StringEntity(o.toString(), "UTF-8"));
response = httpClient.execute(HttpPut);
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException var14) {
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return result;
}
}
// Delete 请求
public static String doDelete(String url, Map<String, Object> map) throws Exception {
String result = null;
if (StringUtils.isEmpty(url)) {
return result;
} else {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDeleteWithBody HttpDelete = new HttpDeleteWithBody(url);
CloseableHttpResponse response = null;
InputStream instream = null;
try {
HttpDelete.setHeader("Content-type", "application/json");
HttpDelete.setHeader("DataEncoding", "UTF-8");
HttpDelete.setConfig(requestConfig);
Object o = JSON.toJSON(map==null?new HashMap<>():map);
HttpDelete.setEntity( new StringEntity(o.toString(), "UTF-8"));
response = httpClient.execute(HttpDelete);
HttpEntity entity = response.getEntity();
if (entity != null) {
instream = entity.getContent();
result = IOUtils.toString(instream, "UTF-8");
}
} catch (IOException var14) {
} finally {
if (null != instream) {
instream.close();
}
if (null != response) {
response.close();
}
if (null != httpClient) {
httpClient.close();
}
}
return result;
}
}
}
import com.es.EsApplication;
import com.es.builder.EsPropertiesBuilder;
import com.es.utils.EsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IndexTest {
@Autowired
EsUtils esutils;
//查詢索引是否存在
@Test
public void show(){
System.out.println(esutils.isIndex("index144"));
}
//创建索引
@Test
public void show2(){
System.out.println(esutils.createdIndex("index1"));
}
//创建索引的时候指定分片和副本
@Test
public void show21(){
boolean a=esutils.createdIndex("index05",3,2);
System.out.println(a);
}
//创建索引的同时映射Mapping和指定分片和副本
@Test
public void show2341(){
EsPropertiesBuilder esContentBuilder = EsPropertiesBuilder.jsonBuilder().
fieldName("name").fieldType("type", "text").fieldType("analyzer", "ik_max_word").
fieldName("age").fieldType("type", "long").fieldType("index", "false").
fieldName("dec").fieldType("type", "text").fieldType("analyzer", "ik_max_word");
boolean a=esutils.createdIndexAndMapping("index07",esContentBuilder,3,1);
System.out.println(a);
}
// 删除索引
@Test
public void show1101(){
System.out.println(esutils.deIndex("index07"));
}
// 批量删除索引
@Test
public void deindex(){
String[] index={"index07","index08"};
System.out.println(esutils.deIndexs(index));
}
// 清空ES全部索引
@Test
public void show221(){
System.out.println(esutils.deIndexsAll());
}
}
import com.es.EsApplication;
import com.es.builder.EsPropertiesBuilder;
import com.es.utils.EsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MappingsTest {
@Autowired
EsUtils esutils;
//创建映射
@Test
public void show3(){
EsPropertiesBuilder esContentBuilder = EsPropertiesBuilder.jsonBuilder().
fieldName("name").fieldType("type", "text").fieldType("analyzer", "ik_max_word").
fieldName("age").fieldType("type", "long").fieldType("index", "false").
fieldName("doc").fieldType("type", "text").fieldType("analyzer", "ik_max_word");
System.out.println(esutils.createdMapping("index1",esContentBuilder));
}
//修改映射,原理就是覆盖
@Test
public void show31(){
EsPropertiesBuilder esContentBuilder = EsPropertiesBuilder.jsonBuilder().
fieldName("age").fieldType("type", "long").fieldType("index", "true");
System.out.println(esutils.createdMapping("index1",esContentBuilder));
}
//创建索引的同时映射Mapping
@Test
public void show234(){
EsPropertiesBuilder esContentBuilder = EsPropertiesBuilder.jsonBuilder().
fieldName("name").fieldType("type", "text").fieldType("analyzer", "ik_max_word").
fieldName("age").fieldType("type", "long").fieldType("index", "false").
fieldName("dec").fieldType("type", "text").fieldType("analyzer", "ik_max_word");
boolean a=esutils.createdIndexAndMapping("index06",esContentBuilder);
System.out.println(a);
}
//查询索引映射
@Test
public void show34(){
System.out.println(esutils.getIndexMapping("index2"));
}
}
import com.es.EsApplication;
import com.es.utils.EsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AliasesTest {
@Autowired
EsUtils esutils;
//创建索引别名
@Test
public void show212(){
System.out.println(esutils.createdIndexAliases ("index2","a","b"));
}
//删除索引别名
@Test
public void show2121(){
System.out.println(esutils.deIndexAliases("index2","a","b"));
}
//别名转移
@Test
public void show21121(){
System.out.println(esutils.upIndexAliases("index2","index8","a"));
}
//索引分组 (别名)
@Test
public void show21211(){
System.out.println(esutils.createdIndexAliasesGroup("guop1","index6","index7","index8"));
}
// 查询所有的索引中,是否有索引存在指定别名
@Test
public void show211121(){
System.out.println(esutils.isAliase("index3"));
}
// 查询指定索引中,是否有存在指定别名
@Test
public void show2111121(){
System.out.println(esutils.isIndexAliase("index2","b"));
}
}
import com.es.EsApplication;
import com.es.builder.EsPropertiesBuilder;
import com.es.entity.User;
import com.es.utils.EsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DocTest {
@Autowired
EsUtils esutils;
//添加一条数据 (随机行的id)
@Test
public void doc1(){
User user= new User();
user.setName("");
user.setAge(23);
user.setDoc("你麽啊啊");
System.out.println(esutils.addDocOne("index1",user));
}
//添加一条数据 (指定行的id)
@Test
public void doc2(){
User user= new User();
user.setName("hu");
user.setAge(23);
user.setDoc("asadasdasda");
System.out.println(esutils.addDocOne("index1",2,user));
}
//批量添加条数据 (随机行的id)
@Test
public void doc23(){
User user= new User();
user.setName("hu");
user.setAge(12);
user.setDoc("asadasdasda");
User user1= new User();
user1.setName("hu1");
user1.setAge(12);
user1.setDoc("asadasdasda");
List<Object> list=new ArrayList<Object>() {
{
add(user);
add(user1);
}
};
System.out.println(esutils.addDocMuch("index1",list,5,1000));
}
// 删除一条数据
@Test
public void doc213(){
System.out.println(esutils.deDocOne("index2","cd633554-060e-44b2-a4fe-97fb358eecdf"));
}
// 删除一批条数据
@Test
public void doc2131(){
List<Object> list=new ArrayList<Object>(){{
add("c6ceb5ff-61a5-436a-a6f3-6b4df7d30caf");
add("Yze-FXsB5TLo-t6jtRHe");
add("ZjfAFXsB5TLo-t6jKREd");
}};
System.out.println(esutils.deDocMuch("index1",list,5,1000));
}
// 查询一条数据
@Test
public void doc21131(){
// {_seq_no=3, found=true, _index=index1, _type=_doc,
// _source={"name":"尹恩惠0","doc":"车模、平面模特、模特","age":30},
// _id=ZzfAFXsB5TLo-t6j5hEt, _version=1, _primary_term=1}
System.out.println(esutils.getDocOne("index1","cd633554-060e-44b2-a4fe-97fb358eecdf").get("_source"));
}
// 查询一批_id数据
@Test
public void doc211131(){
List<Object> list=new ArrayList<Object>(){{
add("ZzfAFXsB5TLo-t6j5hEt");
add("7386eb8e-b3fa-4b75-b56b-3525667a8afc");
}};
for (Map map : esutils.getDocMuch("index1", list)) {
System.out.println(map.get("_source"));
}
}
// 更新一_id数据
@Test
public void doc2111131(){
User user=new User();
user.setName("aaa");
user.setAge(22);
user.setDoc("asdadasdsa111111");
System.out.println( esutils.upDocOne("index1", "ZzfAFXsB5TLo-t6j5hEt",user));
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.es.EsApplication;
import com.es.builder.BootEnum;
import com.es.builder.EsSelectBuilder;
import com.es.entity.User;
import com.es.utils.EsUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/* match查询语句,match和term查询的最大区别在于,term查询会将查询词当为词项,并在倒排索引中进行全匹配。match查询会先进行分词处理,再将解析后的词项去查 match_phrase,句子查询,和match的区别,phrase是句子,句子内部要保持信息一致,所以match_phrase查询将全匹配句子所有文字,并且保证文字之间的相对位置。 es提供了slop等查询控制,给用户去调整文字间相对位置的距离。slop:1 以为着 查询词“帅哥”,可以匹配到“帅*哥”,中间可以有一个文本的距离。 注意1:match_phrase_prefix查询,非常消耗资源, */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SelectDSLTest {
@Autowired
EsUtils esutils;
//查询全部数据 (查询的顺序和存储时候可能不一样)
@Test
public void doc12111131(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll();
List<User> index1 =esutils.getQueryDsl("index1", esSelectBuilder,User.class);
System.out.println(index1);
}
// 排序
@Test
public void d2oc121231111131(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
Map map=new HashMap();
map.put("age","desc");
esSelectBuilder.matchAll().sort(map);
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// 统计个数
@Test
public void d2oc121111131(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll();
System.out.println(esutils.getQueryDslCount("index1", esSelectBuilder));
}
//查询指定字段
@Test
public void doc121121131(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.match("doc","车模");
List<User> index1 =esutils.getQueryDsl("index1", esSelectBuilder,User.class);
System.out.println(index1);
}
//查询指定字段
@Test
public void doc1211211311(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchPhrase("doc","车模");
List<User> index1 =esutils.getQueryDsl("index1", esSelectBuilder,User.class);
System.out.println(index1);
}
//查询指定字段
@Test
public void doc12111211311(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchPhrasePrefix("doc","车模");
List<User> index1 =esutils.getQueryDsl("index1", esSelectBuilder,User.class);
System.out.println(index1);
}
//查询全部数据 ,只显示2条(从开头截取)
@Test
public void d2(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll().size(2);
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//查询全部数据 ,从第几条开始显示
@Test
public void d3(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll().from(2);
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//分页查询 0和1都是 第一页 最后一页的限制只能前端进行做判断 ,总数据在返回里都有
@Test
public void d4(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll().limit(2,3);
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//查询指定字段
@Test
public void d5(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.matchAll().source("name","age");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//term 关键字:指定字段查询
@Test
public void d6(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.term("name","aaa");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//terms 关键字:指定字段查询 ,满足一个值就行
@Test
public void d61(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.terms("name","aaa","尹恩惠");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//range 关键字: 用来指定查询指定范围内的文档 (注意使用这个的话 mappings的index不能设置为false)
@Test
public void d7(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.range("age",8,30);
System.out.println(esSelectBuilder.end());
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//prefix 关键字: 用来检索含有指定前缀的关键词的相关文档
@Test
public void d8(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.prefix("name","尹");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符
@Test
public void d9(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.wildcard("name","*恩*");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//fuzzy 关键字: 用来模糊查询含有指定关键字的文档 注意:允许出现的错误必须在0-2之间
@Test
public void d10(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.fuzzy("name","恩惠");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档
@Test
public void d120(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.ids("aTfAFXsB5TLo-t6j5hEt","ZzfAFXsB5TLo-t6j5hEt");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//正则表达式(RegExp)查询 避免使用左通配这样的模式匹配(如: *foo 或 .*foo 这样的正则式)。 因为存在性能问题
@Test
public void d1210(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.regexp("name","尹.+");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// best_fields类型是默认值,从指定的字段中匹配查询,每个字段都计算评分(_score),返回最高的评分数据。
@Test
public void d12101(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.multiMatchBestFields("尹恩惠","name");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// most_fields类型是默认值,匹配多个字段 ,返回的综合评分(非最高分)
@Test
public void d121101(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.multiMatchMostFields("尹恩惠","name");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// 在每个字段上执行查询,然后返回最高的评分,类似于best_fields类型。
@Test
public void d12121091(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.multiMatchPhrase("尹恩惠","name");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//最左前缀原则
@Test
public void d1212101(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.multiMatchPhrasePrefix("尹恩惠","name");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// 该查询类型是把query条件拆分成各个分词,然后在各个字段上执行匹配分词,默认情况下,只要有一个字段匹配,那么返回文档。
@Test
public void d121221091(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
esSelectBuilder.multiMatchCrossFields("尹恩惠","name");
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
// dis_max,返回子查询中最高的评分。
@Test
public void d121201(){
EsSelectBuilder esSelectBuilder = EsSelectBuilder.jsonBuilder();
Map<String,String> map=new HashMap<>();
map.put("name","尹恩惠");
map.put("doc","车模");
esSelectBuilder.multiMatchDisMax(map);
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder));
}
//bool
// 单个条件使用
@Test
public void d11(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.must).match("name","尹恩惠").match("doc","车模").boolEnd();
System.out.println(esSelectBuilder1.end());
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
// 单个条件使用
@Test
public void d112(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.should).match("name","尹恩惠").match("doc","车模").boolEnd();
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
// 单个条件使用
@Test
public void d1112(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.must_not).match("name","尹恩惠").match("doc","车模").boolEnd();
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
// 查询结果后->过滤
@Test
public void d12112(){
// 查询全部的内容
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.must).matchAll().boolEnd();
//从全部查询的结果中 -> 查询(过滤)年龄大于20的
EsSelectBuilder esSelectBuilder2 = EsSelectBuilder.jsonBuilder();
esSelectBuilder2.boolStart(BootEnum.filter).range("age",20).boolEnd();
esSelectBuilder1.assembly(esSelectBuilder2);//组合
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
//查询结果后->然后筛选出指定id的数据
@Test
public void a3121(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.must).matchAll().boolEnd();
EsSelectBuilder esSelectBuilder2 = EsSelectBuilder.jsonBuilder();
esSelectBuilder2.boolStart(BootEnum.filter).ids("1","2").boolEnd();
esSelectBuilder1.assembly(esSelectBuilder2);//组合
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
// 并行组合条件
@Test
public void d112212(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.boolStart(BootEnum.must).match("name","尹恩惠").match("doc","车模").boolEnd();
EsSelectBuilder esSelectBuilder2 = EsSelectBuilder.jsonBuilder();
esSelectBuilder2.boolStart(BootEnum.filter).match("name","尹恩惠").match("doc","车模").boolEnd();
EsSelectBuilder esSelectBuilder3 = EsSelectBuilder.jsonBuilder();
esSelectBuilder3.boolStart(BootEnum.must_not).match("name","尹恩惠").match("doc","车模").boolEnd();
EsSelectBuilder esSelectBuilder4 = EsSelectBuilder.jsonBuilder();
esSelectBuilder4.boolStart(BootEnum.should).match("name","尹恩惠").match("doc","车模").boolEnd();
esSelectBuilder1.assembly(esSelectBuilder2,esSelectBuilder3,esSelectBuilder4); //组合
System.out.println(esutils.getQueryDsl("index1", esSelectBuilder1));
}
// 高亮-附带内容解析
@Test
public void d11112(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.match("name","恩惠").highlight();
String index1 = esutils.getQueryDsl("index1", esSelectBuilder1);
List<User> sourceHighlight = esutils.getSourceHighlight(index1, User.class);
System.out.println(sourceHighlight);
}
// 多字段分词查询(query_String)
@Test
public void d111121(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.queryString("恩惠","name");
String index1 = esutils.getQueryDsl("index1", esSelectBuilder1);
System.out.println(index1);
}
//求查询结果的平均值
@Test
public void show114(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().avg("age");
BigDecimal index1 =(BigDecimal) esutils.getQueryMetric("index1", esSelectBuilder1);
System.out.println(index1);
}
//求查询结果的最大值
@Test
public void show1141(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().max("age");
BigDecimal index1 =(BigDecimal) esutils.getQueryMetric("index1", esSelectBuilder1);
System.out.println(index1);
}
//求查询结果的最小值
@Test
public void show11411(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().min("age");
BigDecimal index1 =(BigDecimal) esutils.getQueryMetric("index1", esSelectBuilder1);
System.out.println(index1);
}
//单列去重 , 只会显示去重的哪一列
@Test
public void sho2w111411(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().group("age",null,null,null,null);
// esSelectBuilder.matchAll().group("age",2,"desc",5); //去重后的筛选
List<JSONObject> index1 = esutils.getQueryMetricDistinct("index1", esSelectBuilder1);
for (JSONObject jsonObject : index1) {
System.out.println("重复的个数:"+ jsonObject.get("key"));
System.out.println("值:"+ jsonObject.get("key"));
}
}
//整行显示,单列去重 注意:去重的字段不能是text类型的 ,只能是默认类型,或者是keyword
//text类型的解决办法 比如name是text类型,现在我们需要对name进行去重聚合.... 我们可以在添加一个字段nameMetric,将name值都复制一份到nameMetric里就行了
@Test
public void sho2w1121411(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().collapse("age");
List<User> index1 = esutils.getQueryDsl("index1", esSelectBuilder1, User.class);
System.out.println(index1);
}
//Percentiles 关键字: 对指定字段的值按从小到大累计每个值对应的文档数的占比,返回指定占比比例对应的值,
@Test
public void sho2w11214111(){
EsSelectBuilder esSelectBuilder1 = EsSelectBuilder.jsonBuilder();
esSelectBuilder1.matchAll().percentiles("age",50);
String index11 = esutils.getQueryMetricPercentiles("index1", esSelectBuilder1);
System.out.println(JSON.parseObject(index11,Map.class).get("50.0") );
}
}
在查询方面我们将几乎es的全部基础和日常所需的查询都进行了封装,但是对聚合查询只支持部分(有数据库不用你用es脑子有问题)
基本ES主要还是用于在搜索方面,所有复杂的数据处理还是在数据库处理好在同步到ES中,
如果真的避免不了的话,那么可以使用上面的http工具,自己组装JSON进行操作
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_45203607/article/details/120249022
内容来源于网络,如有侵权,请联系作者删除!