1.启动es

docker pull elasticsearch:6.8.7

2.创建配置文件

vim /home/lys/es.yml

cluster.name: elasticsearch-cluster
node.name: es-node
http.port: 9200
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"

3.启动程序

docker run -d --name es -p 9210:9200 -p 9310:9300 --restart always  -v /home/lys/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml -e "discovery.type=single-node" elasticsearch:6.8.7

4.pom引入依赖包

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>

</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.7</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>

5.JAVA API操作ES

package cn.abel.es;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
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.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Auther: liuysh
* @Date: 2021/2/3 21:27
* @Description:
*/
public class RestHighLevelClientUtils {
public static RestHighLevelClient getClient() throws Exception{
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("10.150.10.65", 9210, "http")));
}

public static void index(String index,String type,Map<String, Object> source ,String id) throws Exception {
IndexRequest request = new IndexRequest();
request.index(index);
request.type(type);

if(!StringUtils.isEmpty(id)){
request.id(id);
}

request.source(source);
IndexResponse result = getClient().index(request, RequestOptions.DEFAULT);
System.out.println(result);
getClient().close();
}


/**
* 批量插入,可以指定id
* @param index
* @param type
* @param list
* @param id
* @throws Exception
*/
public static void bulkPutIndex(String index, String type, List<Map<String, Object>> list,String id) throws Exception {
BulkRequest request = new BulkRequest();

if(!StringUtils.isEmpty(id)){
list.stream().forEach(map->{
request.add(new IndexRequest(index,type).opType("index").id(map.get(id).toString()).source(map));
});
}else{
list.stream().forEach(map->{
request.add(new IndexRequest(index,type).opType("index").source(map));
});
}


BulkResponse result = getClient().bulk(request, RequestOptions.DEFAULT);
System.out.println(result);
getClient().close();
}

/**
* 创建索引
* @param index
* @param jsonBuilder
* @throws Exception
*/
public static void createIndex(String index,XContentBuilder jsonBuilder)throws Exception{
//创建索引对象
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
//设置参数
createIndexRequest.settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "2"));


//指定映射
/*XContentBuilder jsonBuilder= XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("name").field("type","keyword").field("index",true).endObject()
.startObject("age").field("type","long").field("index",false).endObject()
.startObject("height").field("type","long").field("index",false).endObject()
.endObject()
.endObject();*/

createIndexRequest.mapping("_doc", jsonBuilder);

//操作索引客户端
IndicesClient indicesClient = RestHighLevelClientUtils.getClient().indices();
//执行创建索引库
CreateIndexResponse createIndexResponse = indicesClient.create(createIndexRequest,RequestOptions.DEFAULT);
//得到响应
boolean isSuccess = createIndexResponse.isAcknowledged();

System.out.println(isSuccess);
}

/**
* 删除指定id
* @param index
* @param type
* @param id
* @throws Exception
*/
public static void delete(String index, String type,String id) throws Exception{
//创建请求
DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
//发起请求
DeleteResponse deleteResponse = RestHighLevelClientUtils.getClient().delete(deleteRequest,RequestOptions.DEFAULT);
//响应结果
DocWriteResponse.Result result = deleteResponse.getResult();

System.out.println(result);
}



public static void search()throws Exception{
SearchSourceBuilder sourceBuilder;
sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(1);
sourceBuilder.sort(new FieldSortBuilder("user").order(SortOrder.ASC));


BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();


boolQueryBuilder.should(QueryBuilders.termQuery("user", "weq"));



System.out.println("dsl 表达式: "+boolQueryBuilder.toString(true));

sourceBuilder.query(boolQueryBuilder);

SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("ods_test_index");
searchRequest.source(sourceBuilder);



SearchResponse searchResponse = RestHighLevelClientUtils.getClient().search(searchRequest, RequestOptions.DEFAULT);

SearchHits searchHits = searchResponse.getHits();

System.out.println("searchHits 返回式: "+searchHits.toString());

searchHits.forEach(
searchHit->{
System.out.println(searchHit.toString());
System.out.println(searchHit.getSourceAsString());
}
);




System.out.println(searchHits.getTotalHits());

RestHighLevelClientUtils.getClient().close();
}






public static void main(String[] args) throws Exception{


List<Map<String, Object>> list=new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> s = new HashMap<>();
s.put("user", "liudehua weq");
s.put("post_date", "2009-11-16T14:12:12");
s.put("message", "trying out Elasticsearch");
s.put("age", i);
s.put("id", i);
list.add(s);
}


RestHighLevelClientUtils.bulkPutIndex("test","test",list,"id");
RestHighLevelClientUtils.getClient().close();
}












}

参考文件: https://yq.aliyun.com/articles/758421?type=2