Elasticsearch

Elasticsearch

Elasticsearch 是一个分布式、可扩展、实时的搜索与数据分析引擎

安装

windows安装

  1. 官网下载Elasticsearch

    Past Releases of Elastic Stack Software | Elastic

  2. 官网下载kibana(es客户端,版本要和es一致)

    Past Releases of Elastic Stack Software | Elastic

  3. 安装中文分词插件,在es的bin目录下执行以下命令(版本要和es一致):elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip

  4. 到es和kibana的bin目录下,点击运行elasticsearch.bat和kibana.bat

  5. 访问localhost:5601打开kibana界面,点击dev tools来输入es的命令

Linux安装

见微服务笔记的分布式搜索(1)

Spring Data Elasticsearch

是Spring提供的一种以Spring Data风格来操作数据存储的方式,它可以避免编写大量的样板代码

官方文档

实例

  1. 导入坐标

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!--注意这里elasticsearch.version版本一定!!!要和elasticsearch的版本一致-->
    <properties>
    <java.version>8</java.version>
    <elasticsearch.version>7.17.3</elasticsearch.version>
    </properties>

    <!--Spring Data Elasticsearch-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
  2. 编写配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    # yml配置elasticsearch客户端地址(可配置项有限)
    spring:
    elasticsearch:
    uris: http://127.0.0.1:9200 # elasticsearch 连接地址
    #username: elastic # 用户名
    #password: 123456 # 密码
    connection-timeout: 10s # 连接超时时间(默认1s)
    socket-timeout: 30s # 数据读取超时时间(默认30s)
  3. 编写保存到es中的实体

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @Document(indexName = "user")	// 索引名
    @Setting(shards = 3, replicas = 0) // 刷新时间和分片数
    public class User {
    @Id
    private Integer id;
    @Field(type = FieldType.Keyword)
    private String name;
    @Field(type = FieldType.Integer)
    private Integer age;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String address;

    public User(Integer id, String name, Integer age, String address) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.address = address;
    }
    }
  4. 编写操作类(类似mysql的mapper)

    1
    2
    3
    @Repository
    public interface UserRepository extends ElasticsearchRepository<User, Integer> {
    }
  5. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Autowired
    private UserRepository repository;

    @Test
    public void test() {
    User user = new User(2, "张三", 18, "上海市闵行区");
    User save = repository.save(user);

    Optional<User> optionalUser = repository.findById(1);
    System.out.println(optionalUser.get());
    }

常用注解

注解 作用
@Document 标示映射到Elasticsearch文档上的领域对象
@Id 表示是文档的id,文档可以认为是mysql中表行的概念
@Field 设置字段类型、是否倒排索引等信息
@Query 可以直接写DSL语句
@Setting 分片数、刷新时间等参数不再在@Docement中声明,而是在@Setting中声明

@Field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public @interface Field {
//文档中字段的类型
FieldType type() default FieldType.Auto;
//是否建立倒排索引
boolean index() default true;
//是否进行存储
boolean store() default false;
//分词器名次
String analyzer() default "";
}

//为文档自动指定元数据类型
public enum FieldType {
Text,//会进行分词并建了索引的字符类型
Integer,
Long,
Date,
Float,
Double,
Boolean,
Object,
Auto,//自动判断字段类型
Nested,//嵌套对象类型
Ip,
Attachment,
Keyword//不会进行分词建立索引的类型
}

@Query

可以直接写DSL语句

1
2
@Query("{"bool" : {"must" : {"field" : {"name" : "?0"}}}}")
Page<EsProduct> findByName(String name,Pageable pageable);

方法生成机制

对于操作类接口方法的命名,根据规则可以自动实现

关键词 示例 对应es搜索查询字符串
And findByNameAndPrice { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } }, { "query_string" : { "query" : "?", "fields" : [ "price" ] } } ] } }}
Or findByNameOrPrice { "query" : { "bool" : { "should" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } }, { "query_string" : { "query" : "?", "fields" : [ "price" ] } } ] } }}
Is findByName { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } } ] } }}
Not findByNameNot { "query" : { "bool" : { "must_not" : [ { "query_string" : { "query" : "?", "fields" : [ "name" ] } } ] } }}
Between findByPriceBetween { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}
LessThan findByPriceLessThan { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : false } } } ] } }}
LessThanEqual findByPriceLessThanEqual { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}
GreaterThan findByPriceGreaterThan { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : false, "include_upper" : true } } } ] } }}
GreaterThanEqual findByPriceGreaterThan { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : true, "include_upper" : true } } } ] } }}
Before findByPriceBefore { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : null, "to" : ?, "include_lower" : true, "include_upper" : true } } } ] } }}
After findByPriceAfter { "query" : { "bool" : { "must" : [ {"range" : {"price" : {"from" : ?, "to" : null, "include_lower" : true, "include_upper" : true } } } ] } }}
Like findByNameLike { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}
StartingWith findByNameStartingWith { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}
EndingWith findByNameEndingWith { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "*?", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}
Contains/Containing findByNameContaining { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "*?*", "fields" : [ "name" ] }, "analyze_wildcard": true } ] } }}
In(当注释为 FieldType.关键字时) findByNameIn(Collection<String>names) { "query" : { "bool" : { "must" : [ {"bool" : {"must" : [ {"terms" : {"name" : ["?","?"]}} ] } } ] } }}
In findByNameIn(Collection<String>names) { "query": {"bool": {"must": [{"query_string":{"query": "\"?\" \"?\"", "fields": ["name"]}}]}}}
NotIn(当注释为 FieldType.关键字时) findByNameNotIn(Collection<String>names) { "query" : { "bool" : { "must" : [ {"bool" : {"must_not" : [ {"terms" : {"name" : ["?","?"]}} ] } } ] } }}
NotIn findByNameNotIn(Collection<String>names) {"query": {"bool": {"must": [{"query_string": {"query": "NOT(\"?\" \"?\")", "fields": ["name"]}}]}}}
True findByAvailableTrue { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "true", "fields" : [ "available" ] } } ] } }}
False findByAvailableFalse { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "false", "fields" : [ "available" ] } } ] } }}
OrderBy findByAvailableTrueOrderByNameDesc { "query" : { "bool" : { "must" : [ { "query_string" : { "query" : "true", "fields" : [ "available" ] } } ] } }, "sort":[{"name":{"order":"desc"}}] }
Exists findByNameExists {"query":{"bool":{"must":[{"exists":{"field":"name"}}]}}}
IsNull findByNameIsNull {"query":{"bool":{"must_not":[{"exists":{"field":"name"}}]}}}
IsNotNull findByNameIsNotNull {"query":{"bool":{"must":[{"exists":{"field":"name"}}]}}}
IsEmpty findByNameIsEmpty {"query":{"bool":{"must":[{"bool":{"must":[{"exists":{"field":"name"}}],"must_not":[{"wildcard":{"name":{"wildcard":"*"}}}]}}]}}}
IsNotEmpty findByNameIsNotEmpty {"query":{"bool":{"must":[{"wildcard":{"name":{"wildcard":"*"}}}]}}}

Elasticsearch
http://xwww12.github.io/2023/02/09/中间件/Elasticsearch/
作者
xw
发布于
2023年2月9日
许可协议