当前位置:   article > 正文

Elasticsearch 索引管理_hostlist: ${eshostlist:127.0.0.1:9200}

hostlist: ${eshostlist:127.0.0.1:9200}

客户端

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,不过当前它还处于完善中,有些功能还没有。

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest‐high‐level‐client</artifactId>
  4. <version>6.2.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch</groupId>
  8. <artifactId>elasticsearch</artifactId>
  9. <version>6.2.1</version>
  10. </dependency>

 

 

创建搜索工程

导入es 依赖及工具测试依赖

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>6.2.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch</groupId>
  8. <artifactId>elasticsearch</artifactId>
  9. <version>6.2.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-test</artifactId>
  14. <scope>test</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.alibaba</groupId>
  18. <artifactId>fastjson</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.commons</groupId>
  22. <artifactId>commons-io</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.commons</groupId>
  26. <artifactId>commons-lang3</artifactId>
  27. </dependency>

 

 

yml 配置文件

  1. server:
  2. port: 40100
  3. spring:
  4. application:
  5. name: xc-service-search
  6. #自定义参数
  7. search:
  8. elasticsearch:
  9. hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔

 

配置类

我在配置类里配置了俩个客户端,分别是  RestHighLevelClient,RestClient

  1. import org.apache.http.HttpHost;
  2. import org.elasticsearch.client.RestClient;
  3. import org.elasticsearch.client.RestHighLevelClient;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class ElasticSearchConfig {
  9. @Value("${search.elasticsearch.hostlist}")
  10. private String hostlist;
  11. // 客户端 resthighlevelclient 配置
  12. @Bean
  13. public RestHighLevelClient restHighLevelClient(){
  14. String[] hostStrs = hostlist.split(",");
  15. HttpHost[] httpHostArray = new HttpHost[hostStrs.length];
  16. // 指针
  17. int i = 0;
  18. for (String hostStr : hostStrs) {
  19. // 切割 端口 127.0.0.1:9200
  20. String[] split = hostStr.split(":"); // split[0] ip // split[1] port
  21. httpHostArray[i++] = new HttpHost(split[0],Integer.parseInt(split[1]) ,"http");
  22. }
  23. return new RestHighLevelClient(RestClient.builder(httpHostArray));
  24. }
  25. //客户端 restClient配置
  26. @Bean
  27. public RestClient restClient(){
  28. String[] hostStrs = hostlist.split(",");
  29. HttpHost[] httpHostArray = new HttpHost[hostStrs.length];
  30. // 指针
  31. int i = 0;
  32. for (String hostStr : hostStrs) {
  33. // 切割 端口 127.0.0.1:9200
  34. String[] split = hostStr.split(":"); // split[0] ip // split[1] port
  35. httpHostArray[i++] = new HttpHost(split[0],Integer.parseInt(split[1]) ,"http");
  36. }
  37. return RestClient.builder(httpHostArray).build();
  38. }
  39. }

 

springboot 启动类

  1. @SpringBootApplication
  2. public class SearchApplication {
  3. public static void main(String[] args){
  4. SpringApplication.run(SearchApplication.class, args);
  5. }
  6. }

 

 

创建索引库

方法一

创建索引

put http://localhost:9200/索引名称

  1. {
  2. "settings":{
  3. "index":{
  4. "number_of_shards":1, #分片的数量
  5. "number_of_replicas":0 #副本数量
  6. }
  7. }
  8. }

创建映射:

put http://localhost:9200/索引库名称/类型名称/_mapping

  1. {
  2. "properties": {
  3. "name": {
  4. "type": "text",
  5. "analyzer":"ik_max_word",
  6. "search_analyzer":"ik_smart"
  7. },
  8. "description": {
  9. "type": "text",
  10. "analyzer":"ik_max_word",
  11. "search_analyzer":"ik_smart"
  12. },
  13. "studymodel": {
  14. "type": "keyword"
  15. },
  16. "price": {
  17. "type": "float"
  18. },
  19. "timestamp": {
  20. "type": "date",
  21. "format": "yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"
  22. }
  23. }
  24. }

 

方法二

 java Client

  1. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  3. import org.elasticsearch.client.IndicesClient;
  4. import org.elasticsearch.client.RestClient;
  5. import org.elasticsearch.client.RestHighLevelClient;
  6. import org.elasticsearch.common.settings.Settings;
  7. import org.elasticsearch.common.xcontent.XContentType;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.springframework.boot.test.context.SpringBootTest;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import javax.annotation.Resource;
  13. @SpringBootTest(classes = SearchApplication.class)
  14. @RunWith(SpringRunner.class)
  15. public class TestES {
  16. @Resource
  17. RestHighLevelClient client;
  18. @Resource
  19. RestClient restClient;
  20. @Test
  21. public void fun01() throws Exception {
  22. //创建索引请求对象,并设置索引名称
  23. CreateIndexRequest createIndexRequest = new CreateIndexRequest("test05");
  24. //设置索引参数
  25. createIndexRequest.settings(Settings.builder().put("number_of_shards",1).put("number_of_replicas",0));
  26. //映射
  27. createIndexRequest.mapping("doc","{\n" +
  28. " \"properties\": {\n" +
  29. " \"name\": {\n" +
  30. " \"type\": \"text\",\n" +
  31. " \"analyzer\":\"ik_max_word\",\n" +
  32. " \"search_analyzer\":\"ik_smart\"\n" +
  33. " },\n" +
  34. " \"description\": {\n" +
  35. " \"type\": \"text\",\n" +
  36. " \"analyzer\":\"ik_max_word\",\n" +
  37. " \"search_analyzer\":\"ik_smart\"\n" +
  38. " },\n" +
  39. " \"studymodel\": {\n" +
  40. " \"type\": \"keyword\"\n" +
  41. " },\n" +
  42. " \"price\": {\n" +
  43. " \"type\": \"float\"\n" +
  44. " },\n" +
  45. " \"timestamp\": {\n" +
  46. " \"type\": \"date\",\n" +
  47. " \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\"\n" +
  48. " }\n" +
  49. " }\n" +
  50. "}", XContentType.JSON);
  51. //创建索引操作客户端
  52. IndicesClient indices = client.indices();
  53. //创建响应对象
  54. CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
  55. //得到响应结果
  56. boolean acknowledged = createIndexResponse.isAcknowledged();
  57. System.out.println(acknowledged);
  58. }
  59. }

 

删除索引库

  1. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  3. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  5. import org.elasticsearch.client.IndicesClient;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.common.settings.Settings;
  9. import org.elasticsearch.common.xcontent.XContentType;
  10. import org.junit.Test;
  11. import org.junit.runner.RunWith;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. import javax.annotation.Resource;
  15. @SpringBootTest(classes = SearchApplication.class)
  16. @RunWith(SpringRunner.class)
  17. public class TestES {
  18. @Resource
  19. RestHighLevelClient client;
  20. @Resource
  21. RestClient restClient;
  22. @Test //删除索引库
  23. public void delIndex() throws Exception {
  24. DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("test05");//删除索引
  25. DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);//删除索引响应结果
  26. boolean acknowledged = deleteIndexResponse.isAcknowledged();
  27. System.out.println(acknowledged);
  28. }
  29. }

 

添加文档

  1. import org.elasticsearch.action.DocWriteResponse;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  6. import org.elasticsearch.action.index.IndexRequest;
  7. import org.elasticsearch.action.index.IndexResponse;
  8. import org.elasticsearch.client.IndicesClient;
  9. import org.elasticsearch.client.RestClient;
  10. import org.elasticsearch.client.RestHighLevelClient;
  11. import org.elasticsearch.common.settings.Settings;
  12. import org.elasticsearch.common.xcontent.XContentType;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.boot.test.context.SpringBootTest;
  16. import org.springframework.test.context.junit4.SpringRunner;
  17. import javax.annotation.Resource;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. @SpringBootTest(classes = SearchApplication.class)
  23. @RunWith(SpringRunner.class)
  24. public class TestES {
  25. @Resource
  26. RestHighLevelClient client;
  27. @Resource
  28. RestClient restClient;
  29. @Test
  30. public void testAddDocument() throws Exception {
  31. Map<String, Object> jsonMap = new HashMap<String, Object>();
  32. jsonMap.put("name", "小红帽");
  33. jsonMap.put("description","这是小红帽卖火柴的励志故事");
  34. jsonMap.put("studymodel","201001" );
  35. SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  36. jsonMap.put("timestamp", dateFormat.format(new Date()));
  37. jsonMap.put("price", 5.6f);
  38. //索引请求对象
  39. IndexRequest indexRequest = new IndexRequest("test05","doc");
  40. //指定索引文档内容
  41. indexRequest.source(jsonMap);
  42. //索引响应对象
  43. IndexResponse indexResponse = client.index(indexRequest);
  44. DocWriteResponse.Result result = indexResponse.getResult();
  45. System.out.println(result);
  46. }
  47. }

 

搜索文档

  1. import org.elasticsearch.action.DocWriteResponse;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  6. import org.elasticsearch.action.get.GetRequest;
  7. import org.elasticsearch.action.get.GetResponse;
  8. import org.elasticsearch.action.index.IndexRequest;
  9. import org.elasticsearch.action.index.IndexResponse;
  10. import org.elasticsearch.client.IndicesClient;
  11. import org.elasticsearch.client.RestClient;
  12. import org.elasticsearch.client.RestHighLevelClient;
  13. import org.elasticsearch.common.settings.Settings;
  14. import org.elasticsearch.common.xcontent.XContentType;
  15. import org.junit.Test;
  16. import org.junit.runner.RunWith;
  17. import org.springframework.boot.test.context.SpringBootTest;
  18. import org.springframework.test.context.junit4.SpringRunner;
  19. import javax.annotation.Resource;
  20. import java.io.IOException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. @SpringBootTest(classes = SearchApplication.class)
  26. @RunWith(SpringRunner.class)
  27. public class TestES {
  28. @Resource
  29. RestHighLevelClient client;
  30. @Resource
  31. RestClient restClient;
  32. // 查询文档
  33. @Test
  34. public void testFindDocument() throws IOException {
  35. GetRequest getRequest = new GetRequest("test05","doc", "zUGVE2gBgTQT4AAg65BL");
  36. GetResponse getResponse = client.get(getRequest);
  37. boolean exists = getResponse.isExists();
  38. Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
  39. System.out.println(sourceAsMap);
  40. }
  41. }

 

 

更新文档

ES更新文档的顺序是:先检索到文档、将原来的文档标记为删除、创建新文档、删除旧文档,创建新文档就会重建索引。

通过请求Url有两种方法:

1、完全替换

Post:http://localhost:9200/索引库/type/id

  1. {
  2. "key1" : "value1",
  3. "key2" : "value2"
  4. }

 

2. 局部替换 

post: http://localhost:9200/索引库/type/id/_update

  1. {
  2. "type":{
  3. "key1":"value1"
  4. }
  5. }

 

Java Client

  1. import org.elasticsearch.action.DocWriteResponse;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  6. import org.elasticsearch.action.get.GetRequest;
  7. import org.elasticsearch.action.get.GetResponse;
  8. import org.elasticsearch.action.index.IndexRequest;
  9. import org.elasticsearch.action.index.IndexResponse;
  10. import org.elasticsearch.action.update.UpdateRequest;
  11. import org.elasticsearch.action.update.UpdateResponse;
  12. import org.elasticsearch.client.IndicesClient;
  13. import org.elasticsearch.client.RestClient;
  14. import org.elasticsearch.client.RestHighLevelClient;
  15. import org.elasticsearch.common.settings.Settings;
  16. import org.elasticsearch.common.xcontent.XContentType;
  17. import org.elasticsearch.rest.RestStatus;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.springframework.boot.test.context.SpringBootTest;
  21. import org.springframework.test.context.junit4.SpringRunner;
  22. import javax.annotation.Resource;
  23. import java.io.IOException;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Date;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. @SpringBootTest(classes = SearchApplication.class)
  29. @RunWith(SpringRunner.class)
  30. public class TestES {
  31. @Resource
  32. RestHighLevelClient client;
  33. @Resource
  34. RestClient restClient;
  35. @Test
  36. public void testUpdate() throws IOException {
  37. UpdateRequest updateRequest = new UpdateRequest("test05", "doc", "3");
  38. Map<String,Object> jsonMap = new HashMap<>();
  39. jsonMap.put("name", "鸭梨牌洗衣机");
  40. updateRequest.doc(jsonMap);
  41. UpdateResponse update = client.update(updateRequest);
  42. RestStatus status = update.status();
  43. System.out.println(status);
  44. }
  45. }

 

删除文档

根据id删除,格式如下:

DELETE    /{index}/{type}/{id}

 

搜索匹配删除,将搜索出来的记录删除,格式如下:
POST        /{index}/{type}/_delete_by_query

搜索的请求体如下

  1. {
  2. "query":{
  3. "term":{
  4. "name":"鸭梨牌洗衣机"
  5. }
  6. }
  7. }

 

java client

  1. import org.elasticsearch.action.DocWriteResponse;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  6. import org.elasticsearch.action.delete.DeleteRequest;
  7. import org.elasticsearch.action.delete.DeleteResponse;
  8. import org.elasticsearch.action.get.GetRequest;
  9. import org.elasticsearch.action.get.GetResponse;
  10. import org.elasticsearch.action.index.IndexRequest;
  11. import org.elasticsearch.action.index.IndexResponse;
  12. import org.elasticsearch.action.update.UpdateRequest;
  13. import org.elasticsearch.action.update.UpdateResponse;
  14. import org.elasticsearch.client.IndicesClient;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.common.settings.Settings;
  18. import org.elasticsearch.common.xcontent.XContentType;
  19. import org.elasticsearch.rest.RestStatus;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.springframework.boot.test.context.SpringBootTest;
  23. import org.springframework.test.context.junit4.SpringRunner;
  24. import javax.annotation.Resource;
  25. import java.io.IOException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. @SpringBootTest(classes = SearchApplication.class)
  31. @RunWith(SpringRunner.class)
  32. public class TestES {
  33. @Resource
  34. RestHighLevelClient client;
  35. @Resource
  36. RestClient restClient;
  37. @Test
  38. public void testDelDoc() throws IOException {
  39. //索引请求对象
  40. DeleteRequest deleteRequest = new DeleteRequest("test05","doc","3");
  41. // 删除索引请求对象
  42. DeleteResponse delete = client.delete(deleteRequest);
  43. //获取响应结果
  44. DocWriteResponse.Result result = delete.getResult();
  45. System.out.println(result);
  46. }
  47. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/68349?site
推荐阅读
相关标签
  

闽ICP备14008679号