ElasticSearch的简单api介绍
1:ElasticSearch是什么?
- Elasticsearch 是一个分布式的免费开源搜索和分析引擎
- 适用于包括文本、数字、地理空间、结构化和非结构化数据等在内的所有类型的数据
- Elasticsearch 以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名是Elastic Stack 的核心组件;
- Elastic Stack 是一套适用于数据采集、扩充、存储、分析和可视化的免费开源工具
2:ElasticSearch的用处?
- 分布式的搜索引擎和数据分析引擎 比如百度 京东 淘宝的搜索引擎都是它
- 全文检索,结构化检索,数据分析
- 可以对海量数据进行近实时的处理
- 3:简单介绍一下ElasticSearch的REST风格:
3:ElasticSearch和ElasticSearch-head和Kibanan和ik分词器 都是es的常用插件下载地址:
ElasticSearch: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
ElasticSearch-head:https://github.com/mobz/elasticsearch-head
kibana:https://mirrors.huaweicloud.com/kibana/?C=N&O=D
IK分词器:https://github.com/medcl/elasticsearch-analysis-ik/releases
4:开启简单的测试:
创建索引:
PUT jietest/type/1
{
"name":"小杰",
"age":"18"
}
成功提示:
{
"_index" : "jietest",
"_type" : "type",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
介绍一下es的类型:
字符串类型:
text ,keyword
区别 text 数据类型不能用来排序和聚合。
但可以被用来检索过滤、排序和聚合。keyword 类型字段只能用本身来进行检索。
数值类型:
long,integer,short,byte,double,float ....等
布尔类型:
double
日期类型
date
.......等等
指定字段的类型(创建规则):
#这里给name age sex 分别定义了类型
PUT xiaojie
{
"mappings": {
"properties": {
"name":{
"type":"text"
},
"age":{
"type": "integer"
},
"sex":{
"type": "text"
}
}
}
}
#成功:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "xiaojie"
}
获取它的索引规则:
GET xiaojie
{
"xiaojie" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"name" : {
"type" : "text"
},
"sex" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1620193321070",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "0C2DfwhiS06OTfLzH1Iwwg",
"version" : {
"created" : "7060199"
},
"provided_name" : "xiaojie"
}
}
}
}
添加索引数据:
PUT xiaojie/_doc/2
{
"name":"小陆",
"age":"5",
"sex":"男"
}
修改索引数据 POST:
POST xiaojie/_doc/1/_update
{
"doc":{
"name":"小杰杰",
"age":"19"
}
}
删除文档或者索引 DELECT:
DELETE xiaojie/_doc/1
//根据自己的要求删除文档还是索引数据
进阶:
文档的查询操作:
和mysql...其他数据库一样 查询那一块是最难的
来给大家简单介绍一下:
先PUT一些数据进去来模拟:
PUT xiaojie/_doc/1
{
"name":"小杰学java",
"age":"25",
"sex":"男"
}
PUT xiaojie/_doc/2
{
"name":"小云学java",
"age":"18",
"sex":"女"
}
PUT xiaojie/_doc/3
{
"name":"小陆学java",
"age":"15",
"sex":"男"
}
简单查询:
//获取_id为1的索引数据
GET /xiaojie/_doc/1
//查询age等于15的索引
GET /xiaojie/_doc/_search?q=age:15
进阶:排序,分页,关键字高亮,模糊查询和精准查询
模糊查询:
//模糊查询
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"杰"
}
}
}
//也可以过滤数据只需要name和age 只需要在_source中保留字段即可
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"小杰"
}
},
"_source":["age","name"]
}
排序:
//利用sort然后把需要排序的规则规定一下
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"小杰"
}
},
"sort":[{
"age":"asc"
}]
}
分页:
//我这里是从0到1 需要分页加from起点和size终点即可
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"小杰"
}
},
"sort":[{
"age":"asc"
}],
"from":0,
"size":1
}
布尔值条件查询:
AND:
//must和sql里面的and一样 既满足名字有java 和age等于15
GET xiaojie/_doc/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "java"
}},
{"match": {
"age": 15
}}
]
}
}
}
OR
//should和sql的OR(或)是一样的作用 这里是满足名字有java或者age等于15
GET xiaojie/_doc/_search
{
"query": {
"bool": {
"should": [
{"match": {
"name": "java"
}},
{"match": {
"age": 15
}}
]
}
}
}
NOT 不等于:
// must_not和sql里面的not一样 下面是查找age不等于18的索引数据
GET xiaojie/_doc/_search
{
"query": {
"bool": {
"must_not": [
{"match": {
"age":18
}}]}}
}
过滤查询:
//查看name有java且 age小于20的索引
GET xiaojie/_doc/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "java"
}}
],
"filter": [
{"range": {
"age": {
"lt": 20
}
}}
]
}
}}
//查看name有java且 0<age<20的索引
GET xiaojie/_doc/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "java"
}}
],
"filter": [
{"range": {
"age": {
"lt": 16,
"gt":0
}
}}
]
}
}}
多条件查询:
//查看name有杰 学的索引 用空格隔开即可多条件查询
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"杰 学"
}
}
}
高亮查询:
//默认的高亮 默认是用em标签
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"杰"
} },
"highlight":{
"fields": {
"name": {}
}}
}
//查询结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808291,
"hits" : [
{
"_index" : "xiaojie",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808291,
"_source" : {
"name" : "小杰学java",
"age" : "25",
"sex" : "男"
},
"highlight" : {
"name" : [
"小<em>杰</em>学java"]}
}]}
}
可以自定义高亮:
//利用pre_tags和post_tags来设置自定义标签
GET /xiaojie/_doc/_search
{
"query":{
"match":{
"name":"杰"
} },
"highlight":{
"pre_tags": "<span class='key' style='color:red'>",
"post_tags": "</span>",
"fields": {
"name": {}
}}
}
//结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808291,
"hits" : [
{
"_index" : "xiaojie",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808291,
"_source" : {
"name" : "小杰学java",
"age" : "25",
"sex" : "男"
},
"highlight" : {
"name" : [
"小<span class='key' style='color:red'>杰</span>学java"]}}]
}}
每天更新Spring Boot 整合ElasticSearch
结合狂神的视频所做的一些笔记:视频地址:https://www.bilibili.com/video/BV17a4y1x7zq?p=13&spm_id_from=pageDriver