Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。
ElasticSearch 官网地址:www.elastic.co/cn/
在开始开发之前,我们需要准备一些环境配置:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-starter-test
test
复制代码
注意:依赖版本必须与你当前所用的版本保持一致,否则连接失败
新建config目录存放项目配置类
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("服务器IP地址", 9200, "http")));
return client;
}
}
复制代码
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引
**/
@Test
void testCreateIndex() throws IOException {
//1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("lt");
//2.客户端执行请求IndicesClient,执行create方法创建索引,请求后获得响应
CreateIndexResponse response=
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
/**
* 判断索引是否存在
**/
@Test
void testExistIndex() throws IOException {
//1.查询索引请求
GetIndexRequest request=new GetIndexRequest("lt");
//2.执行exists方法判断是否存在
boolean exists=restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
System.out.println(exists ? "存在" : "消失");
}
/**
* 删除索引
**/
@Test
void testDeleteIndex() throws IOException {
//1.删除索引请求
DeleteIndexRequest request=new DeleteIndexRequest("lt");
//执行delete方法删除指定索引
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged() ? "删除成功" : "删除失败");
}
}
复制代码
创建实体类User
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private String name;
private Integer age;
private String[] funny;
}
复制代码
文档相关操作代码
/**
* 创建文档
**/
@Test
void testAddUser() throws IOException {
//1.创建对象
User user=new User("小赵",21,new String[]{"篮球","吃饭"});
//2.创建请求
IndexRequest request=new IndexRequest("lt");
//3.设置规则 PUT /lt/_doc/1
//设置文档id=6,设置超时=1s等,不设置会使用默认的
//同时支持链式编程如 request.id("6").timeout("1s");
request.id("6");
request.timeout("1s");
//4.将数据放入请求,要将对象转化为json格式
//XContentType.JSON,告诉它传的数据是JSON类型
request.source(JSONValue.toJSONString(user), XContentType.JSON);
//5.客户端发送请求,获取响应结果
IndexResponse indexResponse=restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
/**
* 获取文档数据
**/
@Test
void testGetUser() throws IOException {
//1.创建请求,指定索引、文档id
GetRequest request=new GetRequest("lt","1");
GetResponse getResponse=restHighLevelClient.get(request,RequestOptions.DEFAULT);
System.out.println(getResponse);//获取响应结果
//getResponse.getSource() 返回的是Map集合
System.out.println(getResponse.getSourceAsString());//获取响应结果source中内容,转化为字符串
}
/**
* 更新文档
**/
@Test
void testUpdateUser() throws IOException {
//1.创建请求,指定索引、文档id
UpdateRequest request=new UpdateRequest("lt","6");
User user =new User("xiaozhao",21,new String[]{"xxx","xxx"});
//将创建的对象放入文档中
request.doc(JSONValue.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse=restHighLevelClient.update(request,RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
/**
* 删除文档
**/
@Test
void testDeleteUser() throws IOException {
//创建删除请求,指定要删除的索引与文档ID
DeleteRequest request=new DeleteRequest("lt","6");
DeleteResponse updateResponse=restHighLevelClient.delete(request,RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
复制代码
/**
* 批量插入数据
**/
@Test
void testBulkAddUser() throws IOException {
BulkRequest bulkRequest=new BulkRequest();
//设置超时
bulkRequest.timeout("10s");
ArrayList list=new ArrayList<>();
list.add(new User("Java",25,new String[]{"内卷"}));
list.add(new User("Go",18,new String[]{"内卷"}));
list.add(new User("C",30,new String[]{"内卷"}));
list.add(new User("C++",26,new String[]{"内卷"}));
list.add(new User("Python",20,new String[]{"内卷"}));
int id=1;
//批量处理请求
for (User u :list){
//不设置id会生成随机id
bulkRequest.add(new IndexRequest("ljx666")
.id(""+(id++))
.source(JSONValue.toJSONString(u),XContentType.JSON));
}
BulkResponse bulkResponse=restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());//是否执行失败,false为执行成功
}
复制代码
@Test
void testSearch() throws IOException {
SearchRequest searchRequest=new SearchRequest("ljx666");//里面可以放多个索引
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();//构造搜索条件
//此处可以使用QueryBuilders工具类中的方法
//1.查询所有
sourceBuilder.query(QueryBuilders.matchAllQuery());
//2.查询name中含有Java的
sourceBuilder.query(QueryBuilders.multiMatchQuery("java","name"));
//3.分页查询
sourceBuilder.from(0).size(5);
//4.按照score正序排列
//sourceBuilder.sort(SortBuilders.scoreSort().order(SortOrder.ASC));
//5.按照id倒序排列(score会失效返回NaN)
//sourceBuilder.sort(SortBuilders.fieldSort("_id").order(SortOrder.DESC));
//6.给指定字段加上指定高亮样式
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name").preTags("").postTags("");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
//获取总条数
System.out.println(searchResponse.getHits().getTotalHits().value);
//输出结果数据(如果不设置返回条数,大于10条默认只返回10条)
SearchHit[] hits=searchResponse.getHits().getHits();
for(SearchHit hit :hits){
System.out.println("分数:"+hit.getScore());
Map source=hit.getSourceAsMap();
System.out.println("index->"+hit.getIndex());
System.out.println("id->"+hit.getId());
for(Map.Entry s:source.entrySet()){
System.out.println(s.getKey()+"--"+s.getValue());
}
}
}
复制代码
hasFailures()
方法是返回是否失败,即它的值为false时说明上传成功