SpringBoot整合7.6.x版本ES入门学习

三猪技术团队

1.Elaticsearch概述

1.1Elaticsearch是什么

The Elastic Stack, 包括 Elasticsearch、Kibana、Beats 和 Logstash(也称为 ELK Stack)。能够安全可靠地获取任何来源、任何格式的数据,然后实时地对数据进行搜索、分析和可视化。Elaticsearch,简称为ES, ES是一个开源的高扩展的分布式全文搜索引擎,是整个Elastic Stack技术栈的核心。它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。

1.2Elaticsearch And Solr

Elasticsearch和Solr,这两款都是基于Lucene搭建的,可以独立部署启动的搜索引擎服务软件。由于内核相同,所以两者除了服务器安装、部署、管理、集群以外,对于数据的操作 修改、添加、保存、查询等等都十分类似。

1.3Elaticsearch的应用案例

GitHub、维基百科、SoundCloud、百度、新浪、阿里、Stack Overflow

2.相关软件的下载

2.1Elaticsearch7.6.x版本下载

在elasticsearch-7.6.1/config/elasticsearch.yml配置文件加上xpack.ml.enabled: false;则可进入到bin目录,./elasticsearch启动Elaticsearch。

新开一个黑窗口输入:curl localhost:9200,收到如下信息说明启动成功。

默认只允许本机访问,如果想远程访问可以修改config/elasticsearch.yml,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动Elasticsearch。

2.2Head插件

打开谷歌浏览器搜索谷歌商城,打开搜索elasticsearch head即可看见如下:

点击添加至chrome,即可在扩展程序找到打开elasticsearch head。

2.3 Kibana7.6.1

打开kibana.yml配置文件,将 i18n.locale: "en"-->i18n.locale: "zh-CN",可以将kibana汉化。进入bin目录,./kibana启动。

2.4ik分词器

将下载好的ik文件放到elasticsearch的plugins文件夹下面,重启elasticsearch即可生效。

3.springboot-2.2.5整合ES-7.6.1

3.1依赖

<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
<!--&lt;!&ndash;        自定义es版本依赖,保证和本地一致&ndash;&gt;-->
        <elasticsearch.version>7.6.1</elasticsearch.version>
    </properties>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

3.2配置ES连接

package com.sanzhu.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 ElasticSearchConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")
                )
        );
    }

}

3.3索引的CRUD

//1.测试索引的创建
 @Test
 public void test01() throws IOException {
 //1.创建索引请求
 CreateIndexRequest request = new CreateIndexRequest("sanzhu_index");
 //2.执行创建请求
 CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
 System.out.println("response.index() = " + response.index());
    }
​
//2.获取索引
 @Test
 public void text02() throws IOException {
 //1.获取索引请求
 GetIndexRequest request = new GetIndexRequest("sanzhu_index");
 boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
 System.out.println("exists = " + exists);
    }
//3.删除索引
 @Test
 public void test03() throws IOException {
 DeleteIndexRequest request = new DeleteIndexRequest("sanzhu_index");
 AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
 System.out.println("response.isAcknowledged() = " + response.isAcknowledged());
    }

3.4文档的CRUD

//4.添加文档
 @Test
 public void text04() throws IOException {
 User user = new User();
 user.setName("陈辉");
 user.setAge(18);
 IndexRequest request = new IndexRequest("sanzhu_index");
 // 添加数据的规则  PUT /sanzhu_index/_doc/1
 request.id("1");
 request.timeout(TimeValue.timeValueSeconds(1));
 request.timeout("1s");
 //添加数据进索引库
 request.source(JSON.toJSONString(user), XContentType.JSON);
 IndexResponse response = client.index(request, RequestOptions.DEFAULT);
 System.out.println("response.status() = " + response.status());
 System.out.println("response.toString() = " + response.toString());
    }
​

//5.判断文档是否存在
 @Test
 public void test05() throws IOException {
 GetRequest getRequest = new GetRequest("sanzhu_index", "1");
 getRequest.fetchSourceContext();
 boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
 System.out.println("exists = " + exists);
    }
​

//6.获取文档信息
 @Test
 public void test06() throws IOException {
 GetRequest getRequest = new GetRequest("sanzhu_index", "1");
 GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
 System.out.println("documentFields.getSourceAsString() = " + documentFields.getSourceAsString());
 System.out.println("documentFields = " + documentFields);
    }
​

//7.更新文档信息
 @Test
 public void test07() throws IOException {
 UpdateRequest updateRequest = new UpdateRequest("sanzhu_index", "1");
 updateRequest.timeout("1s");
 User user = new User();
 user.setName("张家辉");
 user.setAge(1918);
 updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
 UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
 System.out.println("update.status() = " + update.status());
 System.out.println("update.toString() = " + update.toString());
    }
​

//8.删除文档请求
 @Test
 public void test08() throws IOException {
 DeleteRequest deleteRequest = new DeleteRequest("sanzhu_index", "1");
 deleteRequest.timeout();
 DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);
 System.out.println("response.status() = " + response.status());
 System.out.println("response.toString() = " + response.toString());
    }
​

//9.批量插入数据
 @Test
 public void test09() throws IOException {
 BulkRequest bulkRequest = new BulkRequest();
 bulkRequest.timeout("10s");
​
 ArrayList<User> users = new ArrayList<>();
 for (int i = 0; i < 10; i++) {
 User user = new User();
 user.setName("张家辉"+i+"号");
 user.setAge(i+20);
 users.add(user);
        }
​
 for (int i = 0; i < users.size(); i++) {
 bulkRequest.add(
 new IndexRequest("sanzhu_index").id("" + (i + 2))
                            .source(JSON.toJSONString(users.get(i)), XContentType.JSON)
            );
        }
 
 BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
 System.out.println("responses.status() = " + responses.status());
 System.out.println("responses.hasFailures() = " + responses.hasFailures());
    }
​
//单条件查询
 @Test
 public void test14() throws IOException {
 SearchRequest searchRequest = new SearchRequest("sanzhu_index");
 SearchSourceBuilder builder = new SearchSourceBuilder();
 builder.timeout(TimeValue.timeValueSeconds(2));
 //过滤字段
 builder.fetchSource("name","age");
 //分页
 builder.from(0);
 builder.size(3);
 //排序
 builder.sort("_id", SortOrder.DESC);
 //条件查询
 TermQueryBuilder query = QueryBuilders.termQuery("age", "99");
 builder.query(query);
 searchRequest.source(builder);
 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
 SearchHit[] hits = response.getHits().getHits();
 for (SearchHit hit : hits) {
 System.out.println("hit = " + hit);
 System.out.println("hit = " + hit.getSourceAsMap());
        }
    }
​
 //  多条件查询
 @Test
 public void test15() throws IOException{
 SearchRequest searchRequest = new SearchRequest("sanzhu_index");
 SearchSourceBuilder builder = new SearchSourceBuilder();
 builder.timeout(TimeValue.timeValueSeconds(2));
 //多条件查询器
 BoolQueryBuilder query = QueryBuilders.boolQuery();
 //必须满足
 query.must(QueryBuilders.matchQuery("name","peter"));
 //必须不满足
 query.mustNot(QueryBuilders.matchQuery("age","999"));
 //或者  should查询和must查询一起用会无效
//        query.should(QueryBuilders.matchQuery("age","777"));
//        query.should(QueryBuilders.matchQuery("age","888"));
 builder.query(query);
 searchRequest.source(builder);
 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
 SearchHit[] hits = response.getHits().getHits();
 for (SearchHit hit : hits) {
 System.out.println("hit = " + hit);
        }
    }
​
//范围查询
 @Test
 public void test16() throws Exception{
 SearchRequest searchRequest = new SearchRequest("sanzhu_index");
 SearchSourceBuilder builder = new SearchSourceBuilder();
 RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(200).lte(666);
 builder.query(queryBuilder);
 searchRequest.source(builder);
 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
 SearchHit[] hits = response.getHits().getHits();
 for (SearchHit hit : hits) {
 System.out.println("hit = " + hit);
        }
    }
​
//高亮查询
@Test
 public void test13() throws IOException {
 SearchRequest searchRequest = new SearchRequest("sanzhu_index");
 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
 TermQueryBuilder query = QueryBuilders.termQuery("name", "peter");
 searchSourceBuilder.query(query);
 searchSourceBuilder.timeout(TimeValue.timeValueSeconds(1));
 searchSourceBuilder.from(1);
 searchSourceBuilder.size(5);
 searchRequest.source(searchSourceBuilder);
 //高亮
 HighlightBuilder highlightBuilder = new HighlightBuilder();
 highlightBuilder.field("name");
 highlightBuilder.preTags("<span style='color:red'>");
 highlightBuilder.postTags("</span>");
 highlightBuilder.requireFieldMatch(false);//关闭多个高亮,只第一个高亮
 searchSourceBuilder.highlighter(highlightBuilder);
​
 SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
 SearchHit[] hits = response.getHits().getHits();
 System.out.println("hits.length = " + hits.length);
 for (SearchHit hit : hits) {
 //获取高亮字段
 Map<String, HighlightField> map = hit.getHighlightFields();
 HighlightField name = map.get("name");
 Map<String, Object> map1 = hit.getSourceAsMap();
 //用高亮的字段替换原来的字段
 if (name!=null){
 Text[] fragments = name.fragments();//获取高亮内容
 String n_name = "";
 for (Text fragment : fragments) {
 n_name+=fragment;
                }
 map1.put("name",n_name);
            }
 System.out.println("map1 = " + map1);
        }
阅读 1.2k

想你所想争取给你最全面的亚马逊开发知识交流通道x118422

6 声望
6 粉丝
0 条评论

想你所想争取给你最全面的亚马逊开发知识交流通道x118422

6 声望
6 粉丝
文章目录
宣传栏