Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够实现近乎实时的搜索。

Elasticsearch官网https://www.elastic.co/cn/

这篇文章主要简单介绍一下Elasticsearch,Elasticsearch的java API博主也在学习中,文章会持续更新~

目录

第一步:下载Elasticsearch

下载7.6.2版本

下载其他版本

第二步:安装Elasticsearch

第三步:安装kibana

第四步:Springboot整合Elasticsearch

1、创建springboot项目

2、在pom.xml中添加依赖

3、修改配置文件

4、创建数据库和es的实体类

5 、创建mapper接口及映射文件

6、创建Elasticsearch的查询接口

7、开启mapper包扫描

第五步:从mysql导入数据到es

第六步:学习DSL

1、无条件查询,默认返回10条数据

2、指定返回的数据条数

3、指定查询字段

4、分页查询

5、查询指定ID的数据

6、删除索引

7、条件查询

第七步:在java中使用Elasticsearch

1、通过ElasticsearchRepository

2、通过ElasticsearchRestTemplate

3、通过RestHighLevelClient

第一步:下载Elasticsearch

下载7.6.2版本

文章中使用的Elasticsearch版本是7.6.2,可以通过以下网盘链接下载,里面有7.6.2版本的ik分词器,链接永久有效。

elasticsearch7.6.2下载https://pan.baidu.com/s/1D_HS8w_WW3dfQllGzNGv8A?pwd=p3aa

下载完成后,把下面选中的Elasticsearch压缩文件解压到D盘

然后把ik分词器解压到Elasticsearch的plugins目录下,并把文件夹名称修改为ik。

下载其他版本

如需安装其他版本,可自行在官网下载。

1、访问官网,在首页点击页面上方的【文档】

2、点击All Elastic docs

3、点击选择other versions

4、在左侧版本列表选择对应的版本,点击对应链接,比如选择7.6.2版本

5、然后点击Installing the Elastic Stack

6、 在页面找到Elasticsearch,点击后面的install instructions(安装说明)

7、然后找到对应操作系统,比如windows,只需要点击Install Elasticsearch with .zip on Windows

8、在打开的页面向下滚动,找到Download and install the .zip package

9、点击后面的zip压缩文件链接开始下载https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-windows-x86_64.zip

第二步:安装Elasticsearch

把下载下来的压缩包解压到系统盘,建议解压到D盘,如图

打开bin目录,双击下面的文件启动Elasticsearch

稍微等一下,启动完成后访问localhost:9200,如果返回了以下格式的数据,说明elasticsearch到此安装完成

第三步:安装kibana

问了方便使用elasticsearch的Query DSL(Domain Specified Language,领域专用语言),需要安装一下Elasticsearch的Kibana可视化控制台管理工具。

安装步骤和第二步类似,这里就不赘述了,安装完成后,还是解压到D盘

然后修改一下config目录下的kibaba.yml配置文件

使用文本编辑器打开yml文件,找到以下内容并取消注释,修改为对应的值

server.port: 5601

server.host: "localhost"

server.name: "heyunlin" # 这个可以随意取名

elasticsearch.hosts: ["http://localhost:9200"] # elasticsearch服务器的地址

i18n.locale: "zh-CN" # 页面使用中文

最后打开bin目录,双击kibaba.bat启动kibaba

启动完成后,在浏览器地址栏输入http://localhost:5601/app/kibana#/dev_tools/console

然后就可以在左边写我们的DSL了,点击右边的运行按钮即可直接执行查询语句。

第四步:Springboot整合Elasticsearch

这一步是java程序员最注重的,怎么在java中使用elasticsearch。

1、创建springboot项目

首先,创建一个springboot项目elastic

2、在pom.xml中添加依赖

提供了完整的pom文件,可直接复制。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.3.4.RELEASE

com.example

elastic

0.0.1-SNAPSHOT

1.8

1.1.21

8.0.28

1.18.22

2.2.2

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

mysql

mysql-connector-java

${mysql.version}

org.projectlombok

lombok

${lombok.version}

com.alibaba

druid

${druid.version}

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis.version}

org.springframework.boot

spring-boot-starter-data-elasticsearch

org.springframework.boot

spring-boot-maven-plugin

3、修改配置文件

修改elastic.yml,复制以下内容

spring:

# 数据库配置

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/elastic

type: com.alibaba.druid.pool.DruidDataSource

server:

port: 9021

mybatis:

mapper-locations: classpath:mapper/*Mapper.xml

4、创建数据库和es的实体类

创建entity包,然后创建Song.java,@Document(indexName = "songs")注解指定索引名为songs,@Field注解配置字段的类型,只有text类型的字段会被分词。

package com.example.elastic.entity;

import lombok.Data;

import org.springframework.data.annotation.Id;

import org.springframework.data.elasticsearch.annotations.Document;

import org.springframework.data.elasticsearch.annotations.Field;

import org.springframework.data.elasticsearch.annotations.FieldType;

/**

* 歌曲

* @author heyunlin

* @version 1.0

*/

@Data

@Document(indexName = "songs")

public class Song {

@Id

@Field(type= FieldType.Keyword)

private String id;

/**

* 歌曲名

*/

@Field(type= FieldType.Text, analyzer = "ik_max_word")

private String name;

/**

* 歌手

*/

@Field(type= FieldType.Text, analyzer = "ik_max_word")

private String singer;

/**

* 描述信息

*/

@Field(type= FieldType.Text, analyzer = "ik_max_word")

private String note;

/**

* 歌曲文件

*/

private String url;

/**

* 歌曲文件是否存在/是否已上传

*/

@Field(type= FieldType.Long)

private Integer uploaded;

// /**

// * 最后一次修改时间

// */

// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

// private LocalDateTime lastUpdateTime;

}

5 、创建mapper接口及映射文件

创建mapper包,创建一个接口SongMapper.java

package com.example.elastic.mapper;

import com.example.elastic.entity.Song;

import org.springframework.stereotype.Repository;

import java.util.List;

/**

* @author heyunlin

* @version 1.0

*/

@Repository

public interface SongMapper {

List selectAll();

}

在resources目录下创建mapper包,然后创建SongMapper.xml

6、创建Elasticsearch的查询接口

创建一个接口继承ElasticsearchRepository接口,该接口的第一个参数类型为实体类型,二个参数类型是实体类的ID属性的数据类型,在这里是String。

package com.example.elastic.repository;

import com.example.elastic.entity.Song;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import org.springframework.stereotype.Repository;

/**

* @author heyunlin

* @version 1.0

*/

@Repository

public interface SongRepository extends ElasticsearchRepository {

}

7、开启mapper包扫描

创建一个配置类MybatisConfig,在类上面使用@Configuration将该类声明为配置类,通过@MapperScan注解指定mapper包扫描路径。

package com.example.elastic.config;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.context.annotation.Configuration;

/**

* Mybatis配置类

* @author heyunlin

* @version 1.0

*/

@Configuration

@MapperScan(basePackages = "com.example.elastic.mapper")

public class MybatisConfig {

}

第五步:从mysql导入数据到es

通过上一步骤,已经成功完成es的整合,接下来查询mysql数据库,把mysql的数据保存到es中。

接下来,需要创建数据库elastic,然后运行sql脚本,SQL脚本文件在文章末尾的项目链接对应项目上,在这里就不贴出来了,太长了。

修改测试类,运行initData()方法

package com.example.elastic;

import com.example.elastic.entity.Song;

import com.example.elastic.mapper.SongMapper;

import com.example.elastic.repository.SongRepository;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest

class ElasticApplicationTests {

@Autowired

private SongMapper songMapper;

@Autowired

private SongRepository repository;

@Test

void initData() {

List list = songMapper.selectAll();

for (Song song : list) {

repository.save(song);

}

}

}

第六步:学习DSL

完成第五步之后,我们的es里已经有了803条歌曲的数据了,接下来学习DSL的使用,DSL就是Elasticsearch特有的查询语言。

DSL的格式:

其中index_name指的是Elasticsearch中的索引名,我们歌曲对应的索引名通过@Document注解指定为了songs

GET /index_name/_search {json请求体数据}

接下来介绍一下Elasticsearch中常用的DSL

1、无条件查询,默认返回10条数据

GET /songs/_search

{

"query": {

"match_all": {}

}

}

返回的数据格式:为了避免太占位置,只查询了5条记录。

hits里是查询结果信息,hits.total.value表示符合查询条件的总记录数,hits.hits表示的是返回的数据,_source里是具体的数据。

{

"took" : 2,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 808,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "songs",

"_type" : "_doc",

"_id" : "20210522154945",

"_score" : 1.0,

"_source" : {

"_class" : "com.example.elastic.entity.Song",

"id" : "20210522154945",

"name" : "诺言",

"singer" : "陈洁丽",

"note" : "《百变机兽之洛洛历险记》动画ED",

"uploaded" : 0

}

},

{

"_index" : "songs",

"_type" : "_doc",

"_id" : "20210522155349",

"_score" : 1.0,

"_source" : {

"_class" : "com.example.elastic.entity.Song",

"id" : "20210522155349",

"name" : "快乐星猫",

"singer" : "牛奶咖啡",

"note" : "《快乐星猫》动画主题曲",

"uploaded" : 0

}

},

{

"_index" : "songs",

"_type" : "_doc",

"_id" : "20210522155118",

"_score" : 1.0,

"_source" : {

"_class" : "com.example.elastic.entity.Song",

"id" : "20210522155118",

"name" : "无别",

"singer" : "张信哲",

"note" : "《天官赐福》动画OP",

"uploaded" : 0

}

},

{

"_index" : "songs",

"_type" : "_doc",

"_id" : "20210522154331",

"_score" : 1.0,

"_source" : {

"_class" : "com.example.elastic.entity.Song",

"id" : "20210522154331",

"name" : "爱一点",

"singer" : "王力宏、章子怡",

"note" : "",

"uploaded" : 0

}

},

{

"_index" : "songs",

"_type" : "_doc",

"_id" : "20210522154139",

"_score" : 1.0,

"_source" : {

"_class" : "com.example.elastic.entity.Song",

"id" : "20210522154139",

"name" : "多肉少女",

"singer" : "赵芷彤Cassie",

"note" : "",

"uploaded" : 0

}

}

]

}

}

2、指定返回的数据条数

通过size指定需要返回的结果数,以下查询语句将会返回20条数据,而非默认的10条

GET /songs/_search

{

"query": {

"match_all": {}

},

"size": 20

}

3、指定查询字段

_source是一个数组,指定需要返回哪些字段,设置为false则不会返回数据。

GET /songs/_search

{

"query": {

"match_all": {}

},

"size": 5,

"_source": ["name", "singer", "note"]

}

4、分页查询

通过from+size实现分页查询,下面查询了第6-10条记录,相当于mysql中的limit 5, 5(和mysql类似,from默认为0)

GET /songs/_search

{

"query": {

"match_all": {}

},

"from": 5,

"size": 5

}

5、查询指定ID的数据

GET /songs/_doc/20210522155349

6、删除索引

发送路径为/songs的delete请求即可删除songs这个索引。

DELETE /songs

7、条件查询

以下是查询歌曲名中包含“爱”字的歌曲,不指定返回的结果数则默认返回前10条。

GET /songs/_search

{

"query": {

"match": {

"name": "爱"

}

}

}

第七步:在java中使用Elasticsearch

这个章节会介绍三种通过java操作Elasticsearch的方式,提供了简单的使用案例代码。

1、通过ElasticsearchRepository

ElasticsearchRepository有一套标准的方法命名规范,符合规范的方法名在输入的时候就会有提示,比如findByXxx(Object xxx),ElasticsearchRepository会自动为其实现类中符合命名规范的方法生成对应的DSL语句。

我们在之前的SongRepository接口中声明一个findByName()方法,根据歌曲名查询歌曲列表。

package com.example.elastic.repository;

import com.example.elastic.entity.Song;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import org.springframework.stereotype.Repository;

import java.util.List;

/**

* @author heyunlin

* @version 1.0

*/

@Repository

public interface SongRepository extends ElasticsearchRepository {

List findByName(String name);

}

然后通过测试类测试该方法

package com.example.elastic;

import com.example.elastic.entity.Song;

import com.example.elastic.repository.SongRepository;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import java.util.Optional;

/**

* @author heyunlin

* @version 1.0

*/

@SpringBootTest

public class ElasticsearchRepositoryTests {

@Autowired

private SongRepository songRepository;

@Test

void testFindByName() {

List list = songRepository.findByName("雨爱");

System.out.println("共查询到" + list.size() + "条记录。");

System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

for (Song song : list) {

System.out.println(song);

}

}

@Test

void testSave() {

Song song = new Song();

song.setId("2023");

song.setName("雨爱");

song.setNote("雨爱");

Song save = songRepository.save(song);

System.out.println("save = " + save);

}

@Test

void testDelete() {

Song song = new Song();

song.setId("2023");

song.setName("雨爱");

song.setNote("雨爱");

songRepository.delete(song);

}

@Test

void testDeleteById() {

songRepository.deleteById("2023");

testFindByName();

}

@Test

void testDeleteAll() {

songRepository.deleteAll();

}

@Test

void testExistsById() {

songRepository.existsById("2023");

}

@Test

void testFindById() {

testSave();

Optional optional = songRepository.findById("2023");

if (optional.isPresent()) {

Song song = optional.get();

System.out.println(song);

}

}

@Test

void testCount() {

long count = songRepository.count();

System.out.println(count);

}

}

如图,查询出来73首符合条件的歌曲。

2、通过ElasticsearchRestTemplate

下面通过简单的案例来介绍ElasticsearchRestTemplate的使用,相关的API博主也在学习中。

package com.example.elastic;

import com.example.elastic.entity.Song;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import org.springframework.data.elasticsearch.core.IndexOperations;

import org.springframework.data.elasticsearch.core.SearchHit;

import org.springframework.data.elasticsearch.core.SearchHits;

import org.springframework.data.elasticsearch.core.query.Query;

import java.util.List;

/**

* @author heyunlin

* @version 1.0

*/

@SpringBootTest

public class ElasticsearchRestTemplateTests {

@Autowired

private ElasticsearchRestTemplate elasticsearchRestTemplate;

@Test

void test() {

SearchHits search = elasticsearchRestTemplate.search(Query.findAll(), Song.class);

List> searchHits = search.getSearchHits();

for (SearchHit searchHit : searchHits) {

System.out.println(searchHit);

}

}

@Test

void testIndexOps() {

IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Song.class);

System.out.println(indexOperations.exists());

}

}

3、通过RestHighLevelClient

例如,下面代码删除了songs索引中ID为2023的文档。

package com.example.elastic;

import org.elasticsearch.action.delete.DeleteRequest;

import org.elasticsearch.action.delete.DeleteResponse;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.Requests;

import org.elasticsearch.client.RestHighLevelClient;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**

* @author heyunlin

* @version 1.0

*/

@SpringBootTest

public class RestHighLevelClientTests {

@Autowired

RestHighLevelClient restHighLevelClient;

@Test

void test() throws IOException {

DeleteRequest songs = Requests.deleteRequest("songs");

songs.id("2023");

DeleteResponse deleteResponse = restHighLevelClient.delete(songs, RequestOptions.DEFAULT);

int status = deleteResponse.status().getStatus();

System.out.println(status);

}

}

DeleteRequest表示一次删除请求,必须通过id()方法设置文档ID,否则会抛出异常,执行delete()方法会得到一个删除操作的响应对象,可以通过getStatus()得到响应状态码,就和我们的http请求响应状态码一样。

因为type这个概念在Elasticsearch7.x版本已经被弃用了,所以type()这个方法也被声明了已废弃。

那么DeleteRequest又要通过什么方式得到呢?

点开DeleteRequest的源码,在类的注释上已经告诉我们最好的创建方式是通过Requests.deleteRequest()方法。

然后我们点开这个方法,这个方法的参数是String类型,变量名为index,很显然这就是我们需要操作的索引的名字。这个方法的注释上说了必须设置id和type,因为type的概念已经被删除,则需要设置id,这个id顾名思义就是文档ID。

/**

* Creates a delete request against a specific index. Note the {@link DeleteRequest#type(String)} and

* {@link DeleteRequest#id(String)} must be set.

*

* @param index The index name to delete from

* @return The delete request

* @see org.elasticsearch.client.Client#delete(org.elasticsearch.action.delete.DeleteRequest)

*/

public static DeleteRequest deleteRequest(String index) {

return new DeleteRequest(index);

}

好了,文章就分享到这里了,看完不要忘了点赞+收藏哦~

项目已开源,可按需获取:

Springboot整合Elasticsearchhttps://gitee.com/he-yunlin/elastic.git

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。