Elasticsearch是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。
Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。
org.springframework.boot
spring-boot-starter-data-elasticsearch
io.searchbox
jest
spring:
elasticsearch:
jest:
uris:
- http://localhost:9200
read-timeout: 5000
package com.zx.log.controller;
import com.zx.log.entity.UserOperationLog;
import com.zx.log.service.visit.UserOperationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Properties;
import java.util.UUID;
/**
* @Date 2022/7/20 11:05
* 用着测试用的“”:“”“:”“:”“
* @Version 1.0
*/
@RestController
@Api(tags = "测试测试")
@RequestMapping("/mytest")
public class MyTestController {
@Autowired
private UserOperationLogService userOperationLogService;
/**
* 创建es索引
*
* @return
*/
@RequestMapping(value = "/createIndex")
public String createIndex() {
userOperationLogService.createIndex("test");
return "ok";
}
/**
* 新增行
*
* @return
*/
@RequestMapping(value = "/saveEntity")
public String saveEntity() {
UserOperationLog userOperationLog = new UserOperationLog();
userOperationLog.setNumId(UUID.randomUUID().toString().replace("-", ""));
userOperationLog.setAppName("111");
userOperationLog.setAppEnName("appEnName");
userOperationLog.setFunName("funName");
userOperationLog.setFunEnName("funEnName");
userOperationLog.setOpType("opType");
userOperationLogService.saveEntity(userOperationLog);
return "ok";
}
@RequestMapping(value = "/delEntity")
public String delEntity() {
userOperationLogService.delEntity("JkJSIIIBXZZ9IO25Drr1");
return "ok";
}
}
package com.zx.log.service.visit;
import com.zx.log.entity.UserOperationLog;
import java.util.List;
/**
* @Author bo
* @Version 1.0
*/
public interface UserOperationLogService {
/**
* 单个插入ES
* @param entity
*/
void saveEntity(UserOperationLog entity);
/**
* 删除某条ES
* @param id
*/
void delEntity(String id);
/**
* 批量保存到ES
* @param entityList
*/
void saveEntity(List entityList);
/**
* 在ES中搜索内容
*/
List searchEntity(String searchContent);
/**
* 创建索引
*
* @param indexName
*/
void createIndex(String indexName);
/**
* 删除索引
*
* @param indexName
*/
void deleteIndex(String indexName);
}
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
/**
* @Author 波
* @Date 2022/7/21 11:05
* @Version 1.0
*/
@Service
public class UserOperationLogServiceImpl implements UserOperationLogService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserOperationLogServiceImpl.class);
@Autowired
private JestClient jestClient;
@Override
public void saveEntity(UserOperationLog entity) {
Index index = new Index.Builder(entity).id(entity.getNumId()).index("test").type("news").build();
try {
jestClient.execute(index);
LOGGER.info("插入完成");
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
}
}
@Override
public void delEntity(String id) {
Delete index = new Delete.Builder(id).index("test").type("news").build();
try {
jestClient.execute(index);
LOGGER.info("删除完成");
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
}
}
@Override
public void saveEntity(List entityList) {
Bulk.Builder bulk = new Bulk.Builder();
for (UserOperationLog entity : entityList) {
Index index = new Index.Builder(entity).id(String.valueOf(entity.getNumId())).index("test").type("news").build();
bulk.addAction(index);
}
try {
jestClient.execute(bulk.build());
LOGGER.info("批量插入完成");
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
}
}
/**
* 在ES中搜索内容
*/
@Override
public List searchEntity(String searchContent) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("studentName", searchContent));
Search search = new Search.Builder(searchSourceBuilder.toString())
.addIndex("test").addType("news").build();
try {
JestResult result = jestClient.execute(search);
return result.getSourceAsObjectList(UserOperationLog.class);
} catch (IOException e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 创建索引
*
* @param indexName
*/
@Override
public void createIndex(String indexName) {
try {
CreateIndex createIndex = new CreateIndex.Builder(indexName).build();
JestResult result = jestClient.execute(createIndex);
LOGGER.info("result", result.getJsonString());
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
}
}
/**
* 删除索引
*
* @param indexName
*/
@Override
public void deleteIndex(String indexName) {
try {
DeleteIndex deleteIndex = new DeleteIndex.Builder(indexName).build();
JestResult result = jestClient.execute(deleteIndex);
LOGGER.info("result", result.getJsonString());
} catch (Exception e) {
LOGGER.error(e.getMessage());
e.printStackTrace();
}
}
}
上面的实体类大家可以自由发挥,随便创建一个就行并指定数据行ID,主要方便数据后续的删除。