下载es
本人使用的是docker-compose
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- http.cors.enabled=true
- http.cors.allow-origin=*
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic
volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local
networks:
elastic:
driver: bridge
查阅spring官方说明
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.2.3</version>
</dependency>
创建RestClientConfig类
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
进行简单测试
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
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.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient highLevelClient;
@Test
void createIndex() throws IOException {
Long fileId = 222222L;
Map<String, Object> map = new HashMap<>(16);
map.put("fileId", fileId);
map.put("itemId", 7777777L);
map.put("fileName", "ANSDFJK.doc");
map.put("text", "来得及分类考试的");
IndexRequest request = new IndexRequest("file")
.id(fileId.toString())
.source(map)
.setRefreshPolicy(IMMEDIATE);
IndexResponse response = highLevelClient.index(request, RequestOptions.DEFAULT);
}
@Test
void search() throws IOException {
//指定要查询的索引
SearchRequest searchRequest = new SearchRequest("file");
//设置分页条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//输入的关键字匹配的字段
//TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("itemId", 666666666L);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("itemId",666666666L))
.must(QueryBuilders.matchQuery("text","束你"));
searchSourceBuilder.query(boolQuery);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 高亮
/*HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("<span class=\"highlight\">");
highlightBuilder.postTags("</span>");
highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
searchSourceBuilder.highlighter(highlightBuilder);*/
//执行
searchRequest.source(searchSourceBuilder);
SearchResponse search = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
ArrayList<Map<String, Object>> list = new ArrayList<>();
if (search.getHits().getHits().length != 0) {
for (SearchHit documentFields : search.getHits().getHits()) {
list.add(documentFields.getSourceAsMap());
}
System.out.println(list);
} else {
HashMap<String, Object> map = new HashMap<>();
map.put("code", 404);
map.put("msg", "没有相关数据");
list.add(map);
System.out.println("没有相关数据");
}
}
/**
* 判断索引是否存在
*/
@Test
public void checkIndexExists() {
GetIndexRequest request = new GetIndexRequest("file");
try {
boolean exists = highLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
}
/* GetIndexRequest request = new GetIndexRequest().indices("file");
try {
boolean exists = highLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
}*/
}
@Test
public void update() throws IOException {
Long fileId = 222222L;
Map<String, Object> map = new HashMap<>(16);
map.put("fileId", fileId);
map.put("itemId", 7777777L);
map.put("fileName", "ANSDFJK.doc");
map.put("text", "来得及分类考试的11111");
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new UpdateRequest("file", fileId.toString()).doc(map));
highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
}
@Test
public void delete() throws IOException {
Long fileId = 222222L;
DeleteRequest deleteRequest = new DeleteRequest("file", fileId.toString());
DeleteResponse deleteResponse = highLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse);
}
}