敞开生长之旅!这是我参与「日新方案 12 月更文应战」的第30天,点击检查活动详

一、ElasticSearch 介绍

Elasticsearch 是一个分布式、高扩展、高实时的查找与数据剖析引擎。它能很方便的使很多数据具有查找、剖析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在出产环境变得更有价值。Elasticsearch 的完成原理主要分为以下几个过程,首先用户将数据提交到Elasticsearch 数据库中,再经过分词控制器去将对应的句子分词,将其权重和分词成果一并存入数据,当用户查找数据时候,再根据权重将成果排名,打分,再将回来成果出现给用户。

ElasticSearch 官网地址:www.elastic.co/cn/

二、环境预备

在开始开发之前,我们需求预备一些环境装备:

  • jdk 1.8 或其他更高版别
  • 开发工具 IDEA
  • 管理依靠 Maven
  • ElasticSearch环境,此处运用docker建立,ElasticSearch 版别为7.17.7
  • Spring Boot 2.X

三、创立Spring Boot项目导入依靠

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

注意:依靠版别有必要与你当时所用的版别保持一致,否则衔接失利

Spring Boot整合分布式搜索引擎ElasticSearch 实现相关操作

Spring Boot整合分布式搜索引擎ElasticSearch 实现相关操作

四、创立高级客户端

新建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<User> 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("<span>").postTags("</span>");
    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<String,Object> source=hit.getSourceAsMap();
        System.out.println("index->"+hit.getIndex());
        System.out.println("id->"+hit.getId());
        for(Map.Entry<String,Object> s:source.entrySet()){
            System.out.println(s.getKey()+"--"+s.getValue());
        }
    }
}

六、总结

  • 依靠版别有必要与你当时所用的版别保持一致,否则衔接失利。
  • 如果添加时不指定文档ID,他就会随机生成一个ID,ID唯一。
  • 创立文档时若该ID已存在,发送创立文档恳求后会更新文档中的数据。
  • 更新文档时需求将实体目标中的属性全部指定值,否则会被设置为空,如果只设置了一个字段,那么只要该字段会被修正成功,其他会被修正为null。
  • hasFailures()办法是回来是否失利,即它的值为false时说明上传成功
  • elasticsearch很耗费内存,极力推荐运用docker部署运行