目录
ElasticSearch 实现分词全文检索 - 概述
ElasticSearch 实现分词全文检索 - ES、Kibana、IK安装
ElasticSearch 实现分词全文检索 - Restful基本操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 索引操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - 测试数据准备 --待发布
ElasticSearch 实现分词全文检索 - term、terms查询 --待发布
ElasticSearch 实现分词全文检索 - id、ids、prefix、fuzzy、wildcard、range、regexp 查询 --待发布
ElasticSearch 实现分词全文检索 - Scroll 深分页 --待发布
ElasticSearch 实现分词全文检索 - delete-by-query --待发布
ElasticSearch 实现分词全文检索 - 复合查询 --待发布
ElasticSearch 实现分词全文检索 - filter查询 --待发布
ElasticSearch 实现分词全文检索 - 高亮查询 --待发布
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality --待发布
ElasticSearch 实现分词全文检索 - 经纬度查询 --待发布
Java 连接 ES
添加依赖,版本号要和安装的版本一致
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.3</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
ESClient.java 连接类
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
public class ESClient {
public static RestHighLevelClient getClient(){
//创建HttpHost对象
HttpHost httpHost = new HttpHost("172.16.3.88",9200);
//创建RestClientBuilder
RestClientBuilder clientBuilder= RestClient.builder(httpHost);
//创建RestHighLevelClient
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
return client;
}
}
创建索引
SpringBoot 创建 ES 索引
@Test
void createIndex() throws Exception{
String indexName="person";
RestHighLevelClient client = ESClient.getClient();
//1. 准备索引的 settings
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
//2. 准备索引的结构 Mappings
XContentBuilder mappings = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("name")
.field("type","text")
.endObject()
.startObject("age")
.field("type","integer")
.endObject()
.startObject("birthday")
.field("type","date")
.field("format","yyyy-MM-dd")
.endObject()
.endObject()
.endObject();
//3. 将 Settings 和 Mappings 封装到一个Request 对象中
CreateIndexRequest request = new CreateIndexRequest(indexName)
.settings(settings)
.mapping(mappings);
//4. 通过 client 对象去连接ES并执行创建索引
CreateIndexResponse resp = client.indices().create(request, RequestOptions.DEFAULT);
//5. 输出
System.out.println("resp:"+resp.toString());
}
检查索引
@Test
void exists() throws Exception {
String indexName = "person";
RestHighLevelClient client = ESClient.getClient();
//准备 request 对象
GetIndexRequest request = new GetIndexRequest(indexName);
//通过client去操作
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println("exists => " + exists);
}
删除索引
@Test
void delete() throws Exception {
String indexName = "person";
RestHighLevelClient client = ESClient.getClient();
//准备 request 对象
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
//通过client去操作
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println("delete => " + delete);
}
标签:Java,SpringBoot,--,全文检索,client,ElasticSearch,待发布,分词
From: https://www.cnblogs.com/vipsoft/p/17150157.html