Python 官方文档:入门教程 => 点击学习
目录1.创建项目2.创建配置文件3.测试4.更新文档5.删除文档1.创建项目 修改依赖版本 2.创建配置文件 package com.huanmingj
修改依赖版本
package com.huanmingjie.elasticsearch.config;
import org.apache.Http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
return client;
}
}
3.1索引操作
1.创建索引
2.判断索引是否存在
3.删除索引
索引操作代码
package com.huanmingjie.elasticsearch;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
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.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class ElasticsearchApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//创建索引 PUT zoomy_index
@Test
void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("zoomy_index");
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
//判断索引是否存在
@Test
void getIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("zoomy_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//删除索引
@Test
void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
}
3.2文档操作
创建实体类
package com.huanmingjie.elasticsearch.pojo;
import org.springframework.stereotype.Component;
@Component
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1.添加文档
2.获取文档,判断是否存在
3.获取文档信息
3.3实战操作
批量创建数据
查询
package com.huanmingjie.elasticsearch;
import com.alibaba.fastJSON.jsON;
import com.huanmingjie.elasticsearch.pojo.User;
import com.huanmingjie.elasticsearch.utils.ESConstant;
import net.minidev.json.JSONObject;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
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.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class ElasticsearchApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//创建索引 PUT zoomy_index
@Test
void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("zoomy_index");
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
}
//判断索引是否存在
@Test
void getIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("zoomy_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//删除索引
@Test
void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("zoomy_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
//添加文档 PUT zoomy_index/_doc/1
@Test
void aDDDocument() throws IOException {
User user = new User("zoomy", 21);
IndexRequest request = new IndexRequest("zoomy_index");
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求,获取响应结果
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
//命令返回的状态
System.out.println(indexResponse.status());
}
//获取文档,判断是否存在
@Test
void exitDocument() throws IOException {
GetRequest request = new GetRequest("zoomy_index", "1");
//不获取返回的_source的上下文,效率更高
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//获取文档信息
@Test
void getDocument() throws IOException {
GetRequest request = new GetRequest("zoomy_index", "1");
GetResponse getResponse = restHighLevelClient.get(request, RequestOptions.DEFAULT);
//打印文档内容
System.out.println(getResponse.getSourceAsString());
//返回全部内容
System.out.println(getResponse);
}
//更新文档 POST zoomy_index/_doc/1/_update
@Test
void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("zoomy_index", "1");
request.timeout(TimeValue.timeValueSeconds(1));
User user = new User("zoomy", 22);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
//删除文档
@Test
void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("zoomy_index", "1");
DeleteResponse deleteResponse = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
//批量处理数据
@Test
void bulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout(TimeValue.timeValueSeconds(10));
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("zoomy1", 21));
userList.add(new User("zoomy2", 22));
userList.add(new User("zoomy3", 23));
for (int i = 0; i < userList.size(); i++) {
bulkRequest.add(
new IndexRequest("zoomy_index")
.id("" + (i + 1))
.source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
}
BulkResponse bulkItemResponses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkItemResponses.hasFailures());
}
//批量处理数据
@Test
void searchRequest() throws IOException {
SearchRequest searchRequest = new SearchRequest(ESConstant.ZOOMY_INDEX);
//构建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//查询条件QueryBuilders工具 termQuery 精确查询 matchAllQuery匹配所有
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zoomy1");
// MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(termQueryBuilder);
//from size有默认参数
// searchSourceBuilder.from();
// searchSourceBuilder.size();
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
for (SearchHit hit : searchResponse.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
}
以上就是springboot集成elasticsearch7的详细内容,更多关于springboot集成elasticsearch7的资料请关注编程网其它相关文章!
--结束END--
本文标题: springboot集成elasticsearch7的图文方法
本文链接: https://lsjlt.com/news/126845.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0