Es7 JavaApi 常用方法

pom

<!--es-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${es.version}</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${es.version}</version>
</dependency>

准备配置

@Configuration
public class EsUtils {
//链接
private static String HOST_NAME = "10.xx.xx.xx";
//端口
private static int PORT = 9200;
//传输协议
private static String SCHEME ="http";
//创建client
// public static RestHighLevelClient client = null;

//url
private static final HttpHost[] HTTP_HOSTS = {
new HttpHost(HOST_NAME,PORT,SCHEME),
new HttpHost("10.xx.xx.xx",PORT,SCHEME),
new HttpHost("10.xx.xx.xx",PORT,SCHEME)
};
@Bean(name="getEs")
public static RestHighLevelClient client(){
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HTTP_HOSTS));
return client;
}

 }

 

引入

@Autowired

@Qualifier("getEs")
private RestHighLevelClient esClient;

 

常用Api

1、在指定索引下添加数据  (没有该索引会自动创建),source里最好放map

IndexRequest request = new IndexRequest().index("索引名").source(要添加的数据); //添加数据时候不需要指定id,es会自动生成id
IndexResponse index = EsUtils.client().index(request,RequestOptions.DEFAULT);

2、模糊查询()

SearchRequest request = new SearchRequest("索引名"); 
SearchSourceBuilder builder = new SearchSourceBuilder();
String key = "查询的数据";
builder.query(QueryBuilders.multiMatchQuery(key,"","")); //key 后面如果加要搜索的字段,不加默认检索全部。
request.source(builder);
SearchResponse response = esUtils.client().search(request,RequestOptions.DEFAULT); //搜索操作
SearchHit[] hits = response.getHits().getHits(); //获取搜索之后的数据

3、修改 (修改需要指定es的id,doc里放的是新数据。)

需要注意的是es里的修改其实是覆盖了原先id里的数据。如果是要修改某一部分数据,需要把全部数据都重新传一遍

UpdateRequest request = new UpdateRequest().index("TEST").id("1").doc(map);
UpdateResponse response = esUtils.client().update(request,RequestOptions.DEFAULT);

4、删除(删除需要提供es该条数据的id)

DeleteRequest deleteRequest = new DeleteRequest().index("").id("");
DeleteResponse delete = EsUtils.client().delete(deleteRequest, RequestOptions.DEFAULT);

 

5、查看索引是否存在
public static void getIndex(String indexName){
try{
GetIndexRequest request = new GetIndexRequest(indexName);
boolean exists = esUtils.client().indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}catch (Exception e){
logger.info("{}:索引库已存在!",indexName);
}
}

6、批量操作

BulkRequest bulkRequest = new BulkRequest();

//  bulkRequest.add 里可以写增删改操作,批量操作可以理解成    运行之后一次性执行装填在bulk里的操作

 bulkRequest.add(new IndexRequest().index(“索引名”).source(“数据”));

BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT); 

搜索的一些JavaApi

 https://blog.csdn.net/weixin_39977641/article/details/114168065 

这是最基础的增删改查。

 

以下是es其他api介绍,看介绍用到哪个再去查

1.多条件查询结构BoolQueryBuilder boolQueryBuilder=QuerBuilders.boolQuery()
2.boolQueryBuilder.must(QuerBuilders.termQuery(精确查找单个))且条件,可拼接多个
3.QuerBuilders.termsQuery(精确查找集合)
4.boolQueryBuilder.mustNot(且非)
5.QuerBuilders.rangeQuery(字段).gt(值)大于,时间有效
6.QuerBuilders.rangeQuery(字段).lt(值)小于,时间有效
7.QuerBuilders.rangeQuery(字段).gte(值)大于等于,时间有效
8.QuerBuilders.rangeQuery(字段).lte(值)小于等于,时间有效
9.QuerBuilders.queryStringQuery(’’’’+值+"").defaultFiled(字段)包含
10.QuerBuilders.prefixQuery(字段,值)开头是
11.QuerBuilders.existQuery(字段),QuerBuilders.wildcardQuery(字段,"")配合使用可以判断不为空
12.boolQueryBuilder.mustNot(QuerBuilders.wildcardQuery(字段,""))可以判断为空
13.RestClientBuilder builder = RestClient.builder(new httpHost(ip,端口,“http”)),RestHighLevelClient restHighLevelClient= new RestHighLevelClient(builder),创建es客户端
14.SerachSourceBuilder serachSourceBuilder = new SerachSourceBuilder(),创建搜索工具类
15.SerachRequest serachRequest = new SerachRequest(索引名),serachRequest.setTypes(类型),创建搜索工具类
16.serachSourceBuilder.from(起始页),serachSourceBuilder.size(页数)
17.serachSourceBuilder.sort(字段,sort-顺序还是逆序))
18.serachSourceBuilder.timeout(),设置超时时间
19.serachSourceBuilder.query(boolQueryBuilder),放入拼接好的查询条件
20.serachSourceBuilder.trackTotalHits(true),设置支持千万级别数据查询
21.serachRequest.source(serachSourceBuilder),设置查询参数
22.SearchResponse response =restHighLevelClient.search(serachRequest,RequestOptions.DEFAULT),执行查询
23.restHighLevelClient.close()释放客户端
24.for(SearchHit searchHit : response.getHits().getHits()){Map<String,object> sourceMap=searchHit.getSourceMap() 遍历Map获取数据},从ES查询结果里面获取查询的结果数据
25.response.getHits().getTotalHits().value(),获取符合条件的所有数据条数
26.BulkRequest request =new BulkRequest(),创建插入数据的请求
27.request.add(new IndexRequest(索引名,类型名,数据ID).sourc(数据json,XcontentType.Json)),拼接书记入入库请求
28.restHighLevelClient.bulk(request,RequestOptions.DEFAULT),数据入库

 

posted on 2021-11-17 10:48  bigbig钊  阅读(1335)  评论(0编辑  收藏  举报