ElasticSearch 弹性检索

一、全文检索ElasticSearch

我们的应用经常要添加检索功能,开源的ElasticSearch是目前全文搜索引擎的首选。它可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的搜索功能支持。

ElasticSearch是一个分布式搜索服务,提供Restful API,基于底层Lucence(一个开源的搜索引擎,ElasticSearch是对它的封装。),采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,维基百科、github等大型的站点也是采用了ElasticSearch作为其搜索服务。

ElasticSearch是使用Java语言编写的。

官网说明:
1、Elasticsearch
 是一个实时的分布式搜索分析引擎,它能让你以前所未有的速度和规模,去探索你的数据。 它被用作全文检索、结构化搜索、分析以及这三个功能的组合。
2、Elasticsearch 是面向文档的,意味着它存储整个对象或文档。在Elasticsearch 中,你对文档进行索引、检索、排序和过滤,而不是对行列数据。这是一种完全不同的思考数据的方式,也是Elasticsearch 能支持复杂全文检索的原因。
3、Elasticsearch 使用JSON作为文档的序列化格式。

springboot es 设置搜索结果权重 springboot全文检索_搜索


4、所有其他语言可以使用 RESTful API 通过端口 

9200

 和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch 。

二、ElasticSearch的安装和使用

[root@localhost ~]# docker search elasticsearch
[root@localhost ~]# docker pull elasticsearch    --下载官方版本
[root@localhost ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
docker.io/elasticsearch   latest              5acf0e8da90b        2 years ago         486 MB   
[root@localhost ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 IMAGE_ID
-e :  默认初始占用2G的内存空间,虚拟机内存空间可能不够,使用-e限制堆内存使用  Xms初始堆大小 Xmx最大堆大小
-d : 后台运行
-p : 端口映射。进行web通信使用9200端口,分布式个节点间的通信使用9300端口
--name : 自定义命名

安装完成后的网页测试:http://ip:9200

springboot es 设置搜索结果权重 springboot全文检索_搜索_02

三、快速入门

3.1 官方文档:Elasticsearch:官方分布式搜索和分析引擎 | Elastic  

springboot es 设置搜索结果权重 springboot全文检索_官网_03


springboot es 设置搜索结果权重 springboot全文检索_搜索_04


springboot es 设置搜索结果权重 springboot全文检索_Elastic_05


springboot es 设置搜索结果权重 springboot全文检索_官网_06

3.2 官方文档详解-1

springboot es 设置搜索结果权重 springboot全文检索_官网_07

3.3 官方文档详解-2

索引(名词):一个 索引 类似于传统关系数据库中的一个 数据库 ,是一个存储关系型文档的地方。

索引(动词): 索引一个文档 就是存储一个文档到一个 索引 (名词)中以便被检索和查询。这非常类似于 SQL 语句中的 INSERT 关键词,除了文档已存在时,新文档会替换旧文档情况之外。

索引员工文档 | Elasticsearch: 权威指南 | Elastic索引 (名词,存储的地方)-》类型(类比,数据库表) -》 文档 -》 属性

springboot es 设置搜索结果权重 springboot全文检索_官网_08

springboot es 设置搜索结果权重 springboot全文检索_官网_09

springboot es 设置搜索结果权重 springboot全文检索_官网_10

springboot es 设置搜索结果权重 springboot全文检索_Elastic_11

3.3 结合官网的例子,测试Put(发送数据)

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

3.4 结合官网的例子,测试Get(获取数据)

springboot es 设置搜索结果权重 springboot全文检索_搜索_12

3.5 结合官网的例子,测试Delete(删除数据)

springboot es 设置搜索结果权重 springboot全文检索_官网_13

3.6 结合官网的例子,测试Head(检查数据是否存在)

springboot es 设置搜索结果权重 springboot全文检索_Elastic_14


springboot es 设置搜索结果权重 springboot全文检索_搜索_15


PUT 可以用来添加文档

GET 可以用来检索文档

DELETE 命令来删除文档,以及使用 

HEAD 指令来检查文档是否存在

如果想更新已存在的文档,只需再次 PUT    --Restful风格

四、高级搜索

上面的PUT、GET、DELETE、HEAD命令太简单了,下面讲一些稍微高级的搜索功能。

① GET /megacorp/employee/_search
② GET /megacorp/employee/_search?q=last_name:Smith   --q:查询字符串。搜索姓氏为 ``Smith`` 的雇员
③ GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}     ---使用查询表达式搜索。需要将查询条件放到查询体中,所以我们得发POST请求

④ 更复杂的搜索,只要我们将查询体写得足够复杂。 -- 发POST请求
match 查询 
range 过滤器 , 它能找到年龄大于 30 的文档,其中 gt 表示_大于 。

⑤ 短语搜索  -- 发POST请求
match_phrase 精确匹配一系列单词或者_短语。
GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

⑥ 高亮搜索  -- 发POST请求
GET /megacorp/employee/_search
highlight 显示高亮
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}