微服务解决方案 – 高效搜索 Elastic Search
7.6.2 (上)
前言
为什么要写这篇,首先写这个的技术的肯定不止我一个,我将其视为我自己学习的一个路程,自己如果在工作中使用到了,也可以拿来直接上手。我认为在工作中,以最少知识原则去构建项目,当这些最少知识不足以满足需求的时候,就得学习。
Elastic Search
是什么
Elastic Search
(以下简称ES
),ES
是一个基于Lucene
的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web
接口,使用Java
语言开发。
Lucene
是Apache
软件基金会Jakarta
项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构。
为什么使用Elastic Search
相对于以前我们需要模糊查询一个字段通常直接访问数据库,使用like %%
,才能做到。这样的查询很慢,而且数据量一单上来了以后,效率很低。
谁在使用
- 维基百科
Stack Overflow
Github
Alibaba
等许多公司或平台都使用了ES
,这么多公司都在用,足以证明这个产品的好。相同类型的产品也有一个叫Solr
(读作Solar
)也是一款搜索引擎。
Elastic Search
使用
安装
我们继续采用docker compose
的方式来安装。实际上生产的时候可以考虑传统安装或者docker
或者k8s
的方式,具体看公司,生产不推荐本次docker compose
的配置方式进行安装。编写docker-compose.yml
version: '3.1'
services:
elasticsearch:
image: daocloud.io/library/elasticsearch:7.6.2
restart: always
container_name: elasticsearch
ports:
- 9200:9200
environment:
discovery.type: single-node
# kibana图形化插件
kibana:
image: daocloud.io/library/kibana:7.6.2
restart: always
environment:
SERVER_NAME: kibana
ELASTICSEARCH_URL: http://192.168.8.4:9200
ports:
- 5601:5601
depends_on:
- elasticsearch
启动docker-compose up -d
,测试(这里切换到我阿里云的学生机器)
curl http://120.26.114.23:9200
## 输出
{
"name" : "82877e7f04ad",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "x-bP78brST-9y8kpLUtrxw",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
浏览器访问5601端口
Elastic Search api
简介
上面说了ES
是使用了RESTFul Web
接口,也就是说我们可以通过http
请求的方式来查询修改删除我们的数据。
method | url 地址 |
描述 | 补充 |
---|---|---|---|
PUT | :9200/索引名称/类型名称/文档ID | 创建文档(指定文档) | |
POST | :9200/索引名称/类型名称 | 创建文档随机ID | 最好将唯一ID写入文档ID |
POST | :9200/索引名称/类型名称/文档ID/_update | 修改文档 | 常用更新 |
DELETE | :9200/索引名称/类型名称/文档ID | 删除文档 | |
GET | :9200/索引名称/类型名称/文档ID | 查询文档 | 指定ID |
POST | :9200/索引名称/类型名称/_search | 查询对应数据 | 模糊查询 |
api
使用
我们既然安装了kibana
就使用它的图像界面去请求对应的api
。
点击Dev Tools
就是我们写api
的地方
获取ES
基本信息
GET /
# 输出
{
"name" : "82877e7f04ad",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "x-bP78brST-9y8kpLUtrxw",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
创建索引 – 仅解构
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
## 输出
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "test2"
}
获取索引解构信息
GET /test2
## 输出
{
"test2" : {
"aliases" : {
},
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1594478433505",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "ZcECl47_SPWdWSh-xifv5w",
"version" : {
"created" : "7060299"
},
"provided_name" : "test2"
}
}
}
}
创建文档 – 指定文档Id
,_doc
表示默认类型(后续ES
会在某个版本舍弃)
PUT /test2/_doc/1
{
"name":"laoshiren",
"age":1,
"birthday":"2020-07-10"
}
## 输出
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
获取指定文档
GET /test2/_doc/1
## 输出
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "laoshiren",
"age" : 1,
"birthday" : "2020-07-10"
}
}
修改文档(部分属性更新)_update
必须跟上,不然属性会有属性为null
,现在已经更新语法
POST /{index}/_update/{id}
,_version
表示我们更新的版本。
POST /test2/_doc/1/_update
{
"doc": {
"name": "Kakarotto"
}
}
## 输出
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "noop",
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
删除文档,必须指定Id
,再次获取就会返回found: false
DELETE /test2/_doc/2
## 输出
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
简单查询 _score
表示文档对应查询条件的匹配程度
GET /test2/_doc/_search
{
"query": {
"match": {
"name": "laoshiren"
}
}
}
## 输出
{
"took" : 849,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.8713851,
"hits" : [
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.8713851,
"_source" : {
"name" : "laoshiren",
"age" : 1,
"birthday" : "2020-07-10"
}
},
{
"_index" : "test2",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.429556,
"_source" : {
"name" : "laoshiren is a good boy",
"age" : 1,
"birthday" : "2020-07-10"
}
}
]
}
}
限制查询字段
{
"query": {
"match": {
"name": "laoshiren"
}
},
"_source":[ "name","birthday" ]
}
分页查询
{
"query": {
"match": {
"name": "laoshiren"
}
},
"from": 0,
"size": 2
}
更多api
操作可以看下其他人的文档,这里只举了常用的。
文档来源
【狂神说Java】ElasticSearch7.6.x最新完整教程通俗易懂 - 遇见狂神说
特别推荐狂神说的视频简单清晰易懂而且免费。