ElasticSearch-(四)Java使用ElasticSearch
本文最后更新于:February 26, 2022 pm
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎。Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
目录
创建一个maven项目。并导入依赖
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
|
索引(数据库)
创建索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.tothefor.ES;
import org.apache.http.HttpHost; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
CreateIndexRequest request = new CreateIndexRequest("loong"); CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT); boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("添加状态" + acknowledged);
esClient.close(); } }
|
查询索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.tothefor.ES;
import org.apache.http.HttpHost; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
GetIndexRequest request = new GetIndexRequest("loong"); GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT); System.out.println(getIndexResponse); System.out.println(getIndexResponse.getMappings()); System.out.println(getIndexResponse.getSettings()); System.out.println(getIndexResponse.getAliases());
esClient.close(); } }
|
删除索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.tothefor.ES;
import org.apache.http.HttpHost; import org.apache.ibatis.annotations.Delete; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.GetIndexRequest; import org.elasticsearch.client.indices.GetIndexResponse;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
DeleteIndexRequest request = new DeleteIndexRequest("loong"); AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged());
esClient.close(); } }
|
文档(数据)
添加文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.tothefor.ES;
import com.fasterxml.jackson.databind.ObjectMapper; import com.tothefor.pojo.dto.ESUser; import com.tothefor.pojo.entity.User; import org.apache.http.HttpHost; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
IndexRequest request = new IndexRequest(); request.index("loong").id("1007");
ESUser user = new ESUser(); user.setName("墨水记忆7777"); user.setAge(23); user.setPhone("165632536");
ObjectMapper mapper = new ObjectMapper(); String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close(); } }
|
注意:在用PostMan测试时发现,如果是密码属性(即自定义类中有密码password属性,在查询时不会显示出来),则不会显示。
修改文档
这里是局部修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.tothefor.ES;
import com.fasterxml.jackson.databind.ObjectMapper; import com.tothefor.pojo.dto.ESUser; import com.tothefor.pojo.entity.User; import org.apache.http.HttpHost; 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.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
UpdateRequest request = new UpdateRequest(); request.index("loong").id("1008"); request.doc(XContentType.JSON,"age",13);
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close(); } }
|
查询文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.tothefor.ES;
import org.apache.http.HttpHost; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
GetRequest request = new GetRequest(); request.index("loong").id("1008");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
esClient.close(); } }
|
删除文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.tothefor.ES;
import org.apache.http.HttpHost; 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.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
DeleteRequest request = new DeleteRequest(); request.index("loong").id("1001");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
esClient.close(); } }
|
批量操作
批量添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.tothefor.ES;
import org.apache.http.HttpHost; 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.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("loong").id("11").source(XContentType.JSON,"name","loong11")); request.add(new IndexRequest().index("loong").id("12").source(XContentType.JSON,"name","loong12")); request.add(new IndexRequest().index("loong").id("13").source(XContentType.JSON,"name","loong13"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook()); System.out.println(response.getItems());
esClient.close(); } }
|
批量删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.tothefor.ES;
import org.apache.http.HttpHost; 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.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class EsTest_client { public static void main(String[] args) throws IOException { RestHighLevelClient esClient = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) );
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("loong").id("11")); request.add(new DeleteRequest().index("loong").id("12")); request.add(new DeleteRequest().index("loong").id("13"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook()); System.out.println(response.getItems());
esClient.close(); } }
|