客户端
ES 提供多种不同的客户端:
1.TransportClient
ES提供的传统客户端,官方计划8.0版本删除此客户端。
2.RestClient
RestClient是官方推荐使用的,它包括两种:Java Low Level REST Client和 Java High Level REST Client。
ES在6.0之后提供 Java High Level REST Client, 两种客户端官方更推荐使用 Java High Level REST Client,不过当前它还处于完善中,有些功能还没有。
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest‐high‐level‐client</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.1</version>
</dependency>
创建搜索工程
导入es 依赖及工具测试依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
yml 配置文件
server:
port: 40100
spring:
application:
name: xc-service-search
#自定义参数
search:
elasticsearch:
hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔
配置类
我在配置类里配置了俩个客户端,分别是 RestHighLevelClient,RestClient
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Value("${search.elasticsearch.hostlist}")
private String hostlist;
// 客户端 resthighlevelclient 配置
@Bean
public RestHighLevelClient restHighLevelClient(){
String[] hostStrs = hostlist.split(",");
HttpHost[] httpHostArray = new HttpHost[hostStrs.length];
// 指针
int i = 0;
for (String hostStr : hostStrs) {
// 切割 端口 127.0.0.1:9200
String[] split = hostStr.split(":"); // split[0] ip // split[1] port
httpHostArray[i++] = new HttpHost(split[0],Integer.parseInt(split[1]) ,"http");
}
return new RestHighLevelClient(RestClient.builder(httpHostArray));
}
//客户端 restClient配置
@Bean
public RestClient restClient(){
String[] hostStrs = hostlist.split(",");
HttpHost[] httpHostArray = new HttpHost[hostStrs.length];
// 指针
int i = 0;
for (String hostStr : hostStrs) {
// 切割 端口 127.0.0.1:9200
String[] split = hostStr.split(":"); // split[0] ip // split[1] port
httpHostArray[i++] = new HttpHost(split[0],Integer.parseInt(split[1]) ,"http");
}
return RestClient.builder(httpHostArray).build();
}
}
springboot 启动类
@SpringBootApplication
public class SearchApplication {
public static void main(String[] args){
SpringApplication.run(SearchApplication.class, args);
}
}
创建索引库
方法一
创建索引
put http://localhost:9200/索引名称
{
"settings":{
"index":{
"number_of_shards":1, #分片的数量
"number_of_replicas":0 #副本数量
}
}
}
创建映射:
put http://localhost:9200/索引库名称/类型名称/_mapping
{
"properties": {
"name": {
"type": "text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
},
"description": {
"type": "text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
},
"studymodel": {
"type": "keyword"
},
"price": {
"type": "float"
},
"timestamp": {
"type": "date",
"format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"
}
}
}
方法二
java Client
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
@Test
public void fun01() throws Exception {
//创建索引请求对象,并设置索引名称
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test05");
//设置索引参数
createIndexRequest.settings(Settings.builder().put("number_of_shards",1).put("number_of_replicas",0));
//映射
createIndexRequest.mapping("doc","{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"timestamp\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
//创建索引操作客户端
IndicesClient indices = client.indices();
//创建响应对象
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
//得到响应结果
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
}
删除索引库
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.delete.DeleteIndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
@Test //删除索引库
public void delIndex() throws Exception {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test05");//删除索引
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);//删除索引响应结果
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
}
添加文档
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.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
@Test
public void testAddDocument() throws Exception {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("name", "小红帽");
jsonMap.put("description","这是小红帽卖火柴的励志故事");
jsonMap.put("studymodel","201001" );
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
jsonMap.put("timestamp", dateFormat.format(new Date()));
jsonMap.put("price", 5.6f);
//索引请求对象
IndexRequest indexRequest = new IndexRequest("test05","doc");
//指定索引文档内容
indexRequest.source(jsonMap);
//索引响应对象
IndexResponse indexResponse = client.index(indexRequest);
DocWriteResponse.Result result = indexResponse.getResult();
System.out.println(result);
}
}
搜索文档
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.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
// 查询文档
@Test
public void testFindDocument() throws IOException {
GetRequest getRequest = new GetRequest("test05","doc", "zUGVE2gBgTQT4AAg65BL");
GetResponse getResponse = client.get(getRequest);
boolean exists = getResponse.isExists();
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
更新文档
ES更新文档的顺序是:先检索到文档、将原来的文档标记为删除、创建新文档、删除旧文档,创建新文档就会重建索引。
通过请求Url有两种方法:
1、完全替换
Post:http://localhost:9200/索引库/type/id
{
"key1" : "value1",
"key2" : "value2"
}
2. 局部替换
post: http://localhost:9200/索引库/type/id/_update
{
"type":{
"key1":"value1"
}
}
Java Client
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.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
@Test
public void testUpdate() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("test05", "doc", "3");
Map<String,Object> jsonMap = new HashMap<>();
jsonMap.put("name", "鸭梨牌洗衣机");
updateRequest.doc(jsonMap);
UpdateResponse update = client.update(updateRequest);
RestStatus status = update.status();
System.out.println(status);
}
}
删除文档
根据id删除,格式如下:
DELETE /{index}/{type}/{id}
搜索匹配删除,将搜索出来的记录删除,格式如下:
POST /{index}/{type}/_delete_by_query
搜索的请求体如下
{
"query":{
"term":{
"name":"鸭梨牌洗衣机"
}
}
}
java client
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.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
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.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class TestES {
@Resource
RestHighLevelClient client;
@Resource
RestClient restClient;
@Test
public void testDelDoc() throws IOException {
//索引请求对象
DeleteRequest deleteRequest = new DeleteRequest("test05","doc","3");
// 删除索引请求对象
DeleteResponse delete = client.delete(deleteRequest);
//获取响应结果
DocWriteResponse.Result result = delete.getResult();
System.out.println(result);
}
}