目录
3.3.2 使用 ik_max_word分词算法,进行细粒度的划分:
4.综合案例---爬取京东信息存入ES并在自己的页面展示这些数据
1.整合准备
1.1 导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<!-- elasticsearch相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.2 创建相关配置类
package com.wt.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 ESConfig {
//该对象可以对我们的ES进行相关的操作
@Bean
public RestHighLevelClient restHighLevelClient(){
//此处的ip为本地ip,可修改为指定对象的ip
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
return client;
}
}
2.ES的操作
在测试类中导入
@Autowired private RestHighLevelClient client;
2.1 创建索引
//创建索引----PUT /zfy-test/_doc{"mapping":{"properties"}}
@Test
void contextLoadss() throws Exception{
//创建索引创建对象,并设置索引名
CreateIndexRequest createIndexRequest = new CreateIndexRequest("zfy-test");
//根据索引对象信息创建索引
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
//判断索引是否创建成功---输出为true则创建成功
System.out.println(createIndexResponse.isAcknowledged());
}
2.2 删除索引
//删除索引-----DELETE /zfy-test
@Test
void deleteIndexReq() throws Exception{
//创建索引删除对象,并设置索引名
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("zfy-test");
//根据相关对象信息删除索引
AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//查看索引是否删除成功,成功则返回true
System.out.println(delete.isAcknowledged());
}
2.3 判断索引是否存在
//判断索引是否存在 GET /zfy-test
@Test
void IndexExistsReq() throws Exception{
//创建查询索引对象,并指定查询的索引
GetIndexRequest indexRequest = new GetIndexRequest("zfy-test");
//通过对象信息查询索引是否存在
boolean exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
//返回true表示存在,否则不存在
System.out.println(exists);
}
2.4 创建文档
//添加文档-----PUT /zfy-test/_doc/1{name:"",age: ,"address":""}--该方法同时拥有修改功能---该id不存在则添加,存在则修改
@Test
void InsertDocReq() throws Exception{
//创建文档信息对象---指定文档所在的索引
IndexRequest indexRequest = new IndexRequest("zfy-test");
//指定文档id
indexRequest.id("1");
//指定文档数据
indexRequest.source(new User("嵇康","嵩山",23),XContentType.JSON);
//根据相关信息创建文档
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
//获取文档创建的结果---首次创建该文档则返回CREATED,若存在该文档则返回UPDATE
System.out.println(index.getResult());
}
2.5 查询文档
将查询出的文档封装到实体类或map中
//查询文档 GET /zfy-test/_doc/1
@Test
void GetDocReq() throws Exception{
//创建查询对象,并指定索引
GetRequest getRequest = new GetRequest("zfy-test");
//指定文档id
getRequest.id("1");
//根据相关信息查询文档是否存在
GetResponse index = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(index);
//获取返回的数据----直接输出index也可
String sourceAsString = index.getSourceAsString();
//若存在该文档则返回该文档的所有数据,若不存在该文档则found为false且没有数据
System.out.println(sourceAsString);
//将获取到的数据封装到实体类User中
User user = JSON.parseObject(sourceAsString, User.class);
System.out.println(user.getAddress());
//将获取的数据封装到Map中
Map<String, Object> sourceAsMap = index.getSourceAsMap();
System.out.println(sourceAsMap.get("name"));
}
2.6 判断文档是否存在
//判断文档是否存在
@Test
void DocExistsReq() throws Exception{
//创建查询对象,并指定索引
GetRequest getRequest = new GetRequest("zfy-test");
//指定文档id
getRequest.id("1");
//判断文档是否存在与查询文档调用方法不同,但参数相同
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
//存在该文档则返回为true,反之为false
System.out.println(exists);
}
2.7 修改文档
//修改文档 POST /zfy-test/1/_update{"doc":{"name":""}}
@Test
void UpdateDocReq() throws Exception{
//创建修改信息对象,并指定索引和文档id
UpdateRequest updateRequest = new UpdateRequest("zfy-test","1");
//创建实体类,并封装数据
User user = new User();
user.setName("康康");
//封装要修改的值----以POST形式修改指定列
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
//输出修改返回的值---成功过则返回UPDATE
System.out.println(update.getResult());
}
2.8 批量添加
//批量添加文档
@Test
void bulkDoc() throws Exception{
//定义容器并指定索引
BulkRequest bulkRequest = new BulkRequest("zfy-test");
//定义集合封装文档信息
List<User> list = new ArrayList<>();
list.add(new User("张三","北京",22));
list.add(new User("张三他妈","天津",45));
list.add(new User("李四","南京",23));
list.add(new User("李四他爸","天津",47));
list.add(new User("赵六","西安",22));
//使用stream形式将list内的数据封装到容器中
list.stream().forEach(item->{
bulkRequest.add(new IndexRequest().source(JSON.toJSONString(item),XContentType.JSON));
});
//将封装好的信息放入该方法中进行批量添加---将会自动生成id
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//判断是否添加失败,若成功添加则返回false
System.out.println(bulk.hasFailures());
}
2.9 删除文档
//删除文档
@Test
void deleteDocReq() throws Exception{
//创建删除对象---指定索引
DeleteRequest deleteRequest = new DeleteRequest("zfy-test");
//指定删除id
deleteRequest.id("1");
//调用删除方法
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
//获取删除返回值----成功返回为true,失败则返回false
System.out.println(delete.getResult());
}
批量删除
//--------批量删除
@Test
void deleteDocReqList() throws Exception{
//创建删除对象---指定索引
DeleteRequest deleteRequest = new DeleteRequest("zfy-test");
//封装集合
String[] arr = {"SPmjpYIBQ7K39xWU2iaZ","SfmjpYIBQ7K39xWU2iaZ","SvmjpYIBQ7K39xWU2iaZ","S_mjpYIBQ7K39xWU2iaZ","TPmjpYIBQ7K39xWU2iaZ"};
Arrays.stream(arr).forEach(item->{
//将要删除id封装到删除对象中
deleteRequest.id(item);
try{
//调用删除方法
DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
//获取删除返回值----成功返回为true,失败则返回false
System.out.println(delete.getResult());
}catch (Exception e){
e.printStackTrace();
}
});
}
2.10 复杂查询
//搜索查询---GET /索引/_search
// {
// "query":{
// "":{}
// },
// "from":
// "size":
// "_source":["",""],
// "sort":{}
// }
//1. 搜索请求对象SearchRequest
//2. 构建一个条件对象SearchSourceBuilder
//3. 把条件对象放入搜索请求对象中
//4. 执行搜索功能
//复杂查询
@Test
void testSearchReq() throws Exception{
//创建复杂查询对象---------
SearchRequest searchRequest = new SearchRequest("zfy-test");
//创建一个条件对象用来封装各种条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//创建一个对象来封装查询条件query
MatchQueryBuilder queryBuilder = new MatchQueryBuilder("name","李");
//使用条件对象来封装查询条件---条件
sourceBuilder.query(queryBuilder);
//指定查询的列------条件
String[] a