创建springboot或maven项目
导入坐标
编写配置文件yml或yaml
编写配置类
使用高级客户端测试
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.liflygroupId>
<artifactId>springboot-esartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot-esname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<elasticsearch.version>6.2.1elasticsearch.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>6.2.1version>
dependency>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>6.2.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
lifly:
elasticsearch:
hostList: ${eshostlist:127.0.0.1:9200}
ElasticsearchConfig
@Configuration
public class ElasticSearchConfig {
@Value("${lifly.elasticsearch.hostList}")
private String hostlist;
@Bean
public RestHighLevelClient restHighLevelClient(){
//解析hostlist的配置信息(拿到多个ES节点的地址及端口信息)
String[] hostList = hostlist.split(",");
HttpHost[] httpHostArray = new HttpHost[hostList.length];
for(int i=0;i<hostList.length;i++){
String item = hostList[i];
httpHostArray[i] = new HttpHost(
item.split(":")[0],
Integer.parseInt(item.split(":")[1]),"http");
}
//创建RestHighLevelClient客户端
return new RestHighLevelClient(RestClient.builder(httpHostArray));
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticSearchTest {
//拿到客户端对象
@Autowired
@Qualifier("restHighLevelClient")
RestHighLevelClient client;
private MatchQueryBuilder matchQueryBuilder;
/**
* 创建映射测试
*/
@Test
public void createMappingTest() throws IOException {
//创建索引库
CreateIndexRequest index = new CreateIndexRequest("lifly-no1");
//索引库信息配置
index.settings(Settings.builder()
.put("number_of_shards",1)
.put("number_of_replicas",0)
);
//创建映射
index.mapping("doc","{\n" +
" \"properties\": {\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"search_analyzer\": \"ik_smart\"\n" +
" },\n" +
" \"name\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"pic\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": false\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"timestamp\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd\"\n" +
" }\n" +
" }\n" +
" }", XContentType.JSON);
IndicesClient indices = client.indices();
//创建索引库及映射,CreateIndexResponse对象中包含的就是创建结果
CreateIndexResponse createIndexResponse = indices.create(index);
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建结果:"+acknowledged);
}
/**
* 删除索引库
* @throws IOException
*/
@Test
public void testDeleteIndex() throws IOException {
//删除请求索引对象
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("lifly-no1");
//删除索引
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
//获取删除索引响应结果
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
/**
* 添加文档
* @throws IOException
*/
@Test
public void testAddDoc() throws IOException {
HashMap<String, Object> map = new HashMap<>();
map.put("name","spring data实战");
map.put("description","注解讲解怎样从入门到放弃");
map.put("studymodel","202003");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy‐MM‐dd HH:mm:ss");
map.put("timestamp",simpleDateFormat.format(new Date()));
map.put("price",5.8f);
//获取索引请求对象
IndexRequest indexRequest = new IndexRequest("lifly-no1", "doc");
//指定索引文档内容
indexRequest.source(map);
//获取索引文档对象
IndexResponse indexResponse = client.index(indexRequest);
//获取响应结果
DocWriteResponse.Result result = indexResponse.getResult();
System.out.println(result);
}
/**
* 获取文档
* @throws IOException
*/
@Test
public void testGetDoc() throws IOException {
//通过文档的索引,类型,以及id来获取请求对象
GetRequest getRequest = new GetRequest("lifly-no1", "doc", "PgPqRHsBMAV6luTvynTM");
//获取响应对象
GetResponse getResponse = client.get(getRequest);
//判断是否存在
boolean exists = getResponse.isExists();
//获取文档对象
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
}
/**
* 更新文档
* @throws IOException
*/
@Test
public void testUpdateDoc() throws IOException {
//获取请求对象
UpdateRequest updateRequest = new UpdateRequest("lifly-no1", "doc", "PgPqRHsBMAV6luTvynTM");
//设置数据
HashMap<String, String> map = new HashMap<>();
map.put("name","spring cloud实战");
//添加map到请求对象中
updateRequest.doc(map);
//更新文档
UpdateResponse update = client.update(updateRequest);
//获取状态
RestStatus status = update.status();
System.out.println(status);
}
/**
* 根据id删除文档
* @throws IOException
*/
@Test
public void testDelDoc() throws IOException {
//设置删除文档id
String id = "PgPqRHsBMAV6luTvynTM";
//删除索引请求对象
DeleteRequest deleteRequest = new DeleteRequest("lifly-no1", "doc", id);
//执行删除
DeleteResponse deleteResponse = client.delete(deleteRequest);
//获取响应结果
DocWriteResponse.Result result = deleteResponse.getResult();
System.out.println(result);
}
/**
* 搜索type下的全部记录
* @throws IOException
*/
@Test
public void testSearchAll() throws IOException {
//获取请求
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置请求对象
searchRequest.types("doc");
//执行查询
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});
//将字段过滤添加到请求对象中
searchRequest.source(searchSourceBuilder);
//执行查询
SearchResponse searchResponse = client.search(searchRequest);
//获取结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
//遍历结果并输出
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
String description = (String)sourceAsMap.get("description");
System.out.println(name);
System.out.println(studymodel);
System.out.println(description);
}
}
/**
* 分页查询
* @throws IOException
*/
@Test
public void testSearchPages() throws IOException {
//获取请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置类型
searchRequest.types("doc");
//创建searchSourceBuilder
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//分页查询,设置起始下标,从0开始
searchSourceBuilder.from(0);
//每页显示个数
searchSourceBuilder.size(2);
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"},new String[]{});
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
//获取结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
//遍历结果并输出
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
System.out.println(name);
System.out.println(studymodel);
}
}
/**
* 精确查询
* @throws IOException
*/
@Test
public void testSearchTerm() throws IOException {
//获取请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置类型
searchRequest.types("doc");
//创建searchSourceBuilder
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("name","spring data实战"));
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"},new String[]{});
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
//获取结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
//遍历结果并输出
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
System.out.println(name);
System.out.println(studymodel);
}
}
/**
* 根据id精确查询
* @throws IOException
*/
@Test
public void testSearchId() throws IOException {
//获取请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置类型
searchRequest.types("doc");
//创建searchSourceBuilder
String[] split = new String[]{"PwMoRXsBMAV6luTvYHQP", "QAMpRXsBMAV6luTvC3S0"};
List<String> idList = Arrays.asList(split);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termsQuery("_id",idList));
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"},new String[]{});
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
//获取结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
//遍历结果并输出
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
System.out.println(name);
System.out.println(studymodel);
}
}
/**
* MatchQuery查询
* @throws IOException
*/
@Test
public void testMatchQuery() throws IOException {
//获取请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置类型
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"},new String[]{});
//匹配关键字
searchSourceBuilder.query(QueryBuilders.matchQuery("description","注解").operator(Operator.OR));
//设置匹配占比
//MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("description", "注解").minimumShouldMatch("80%");
//提升权重来提高查询
//MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架", "name", "description") .minimumShouldMatch("50%");
//multiMatchQueryBuilder.field("name",10);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
System.out.println(name);
System.out.println(studymodel);
}
}
/**
* 布尔查询
*/
@Test
public void testBooleanQuery() throws IOException {
//布尔查询,将搜索关键字分词,拿分词去索引库搜索
//创建搜索请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置doc
searchRequest.types("doc");
//创建搜索配置对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[]{"name","studymodel"},new String[]{});
//multiQuery
String keyword = "spring boot实战";
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "description").minimumShouldMatch("50%");
multiMatchQueryBuilder.field("name",10);
//termQuery
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", 202001);
//布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(multiMatchQueryBuilder);
boolQueryBuilder.must(termQueryBuilder);
//设置布尔查询对象
searchSourceBuilder.query(boolQueryBuilder);
//设置搜索源配置
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
/**
* 过滤器
*/
@Test
public void testFilter() throws IOException {
//创建搜索请求对象
SearchRequest searchRequest = new SearchRequest("lifly-no1");
//设置doc
searchRequest.types("doc");
//创建搜索配置对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});
searchRequest.source(searchSourceBuilder);
//multiQuery
String keyword = "spring boot实战";
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "description").minimumShouldMatch("50%");
//设置匹配占比
multiMatchQueryBuilder.minimumShouldMatch("50%");
//提升领个字段的boost值
multiMatchQueryBuilder.field("name",10);
searchSourceBuilder.query(multiMatchQueryBuilder);
//布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(searchSourceBuilder.query());
//过滤
boolQueryBuilder.filter(QueryBuilders.termQuery("studymodel","202001"));
//设置搜索源配置
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
/**
* 排序
*/
public void testSort() throws IOException {
SearchRequest searchRequest = new SearchRequest("lifly-no1");
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});
searchRequest.source(searchSourceBuilder);
//布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//过滤
//排序
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(1));
searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));
searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
Map<String,Object>sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
String name = (String) sourceAsMap.get("name");
String studymodel = (String)sourceAsMap.get("studymodel");
System.out.println(name);
System.out.println(studymodel);
}
}
/**
* 高亮显示
*/
@Test
public void testHighLight() throws IOException {
SearchRequest searchRequest = new SearchRequest("lifly-no1");
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//source源字段过滤
searchSourceBuilder.fetchSource(new String[]{"name","studymodel","description"},new String[]{});
searchRequest.source(searchSourceBuilder);
//布尔查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//过滤
//排序
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(1));
searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));
searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
//高亮显示
HighlightBuilder highlightBuilder = new HighlightBuilder();
//设置前缀
highlightBuilder.preTags("" );
//设置后缀
highlightBuilder.postTags("");
//设置高亮字段
highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
searchSourceBuilder.highlighter(highlightBuilder);
SearchResponse searchResponse = client.search(searchRequest);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//名称
String name = (String) sourceAsMap.get("name");
//取出高亮字段内容
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (highlightFields != null){
HighlightField nameField = highlightFields.get("name");
if (nameField != null){
Text[] fragments = nameField.getFragments();
StringBuffer stringBuffer = new StringBuffer();
for (Text str : fragments) {
stringBuffer.append(str.string());
}
name = stringBuffer.toString();
}
}
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsMap);
String studymodel = (String)sourceAsMap.get("studymodel");
String description = (String)sourceAsMap.get("description");
System.out.println(name);
System.out.println(studymodel);
System.out.println(description);
}
}