elasticsearch

es官网:https://www.elastic.co/cn/elasticsearch/

统一协定
(协议) : //(域名):(端口)/(索引)
(协议):http
(域名):localhost
(端口):9200
后面统称叫做域名:http://localhost:9200

数据格式

elasticsearch => MySQL
index(索引) => database(数据库)
type(类型) => table(表)
documents(文档) => row(行)
fields(字段) => column(列)

索引

创建索引

# PUT 请求
(域名)/(索引)
http://localhost:9200/shopping

# 返回数据
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}
# acknowledged 返回为 true 代表成功
# index 就是你创建成功的索引名称

查询索引

# GET请求
(域名)/(索引)
http://localhost:9200/shopping

# 返回数据
{
    "shopping": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "shopping",
                "creation_date": "1619079690286",
                "number_of_replicas": "1",
                "uuid": "yyyKZcQAQVe4WAHQUxL4AQ",
                "version": {
                    "created": "7120099"
                }
            }
        }
    }
}


# 查看全部的索引
(域名)/_cat/indices?v
http://localhost:9200/_cat/indices?v

# 返回数据
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping yyyKZcQAQVe4WAHQUxL4AQ   1   1          0            0       208b           208b

删除索引

# DELETE 请求
(域名)/(索引)
http://localhost:9200/shopping

# 返回数据
{
    "acknowledged": true
}

文档

创建文档

# POST请求 后面不带ID。ES自动生成ID
(域名)/(索引)/_doc
http://localhost:9200/shopping/_doc
# body里面要带数据 json格式
{
    "title" : "小米手机",
    "category" : "小米",
    "images" : "https://tupan.baidu.com",
    "price" : 3999.00
}

# 返回数据
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "zae--HgBCnwSbz972M2Y",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
_index 所属的索引
_id  id相当于ID主键
_version 版本

# POST请求或者PUT 自定义ID
# PUT请求时 当ID不存在创建 存在就全覆盖修改
(域名)/(索引)/_doc
http://localhost:9200/shopping/_doc/1

# 创建
(域名)/(索引)/_create/(自定义ID)
http://localhost:9200/user/_create/333

查询文档

# GET请求 主键查询
(域名)/(索引)/_doc/主键
http://localhost:9200/shopping/_doc/3
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 6,
    "_seq_no": 6,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "https://tupan.baidu.com",
        "price": 3999.00
    }
}
通过  found 来判断是否查询到数据 没有查到数据 found 返回为false

# GET请求 全部查询
(域名)/(索引)/_search
http://localhost:9200/shopping/_search

# 返回数据
{
    "took": 382,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://tupan.baidu.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

# GET请求  条件查询
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
# body 带查询参数 json
{
    "query" :{
        "match" : {
            "category" : "小米"
        }
    }
}
query 查询要带的参数
match 代表匹配查询

# GET请求 查询全部数据并且分页
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "query" :{
        "match_all" : {}
    },
    "from" : 0,
    "size" : 1,
    "_source" : ["title"],
    "sort" : {
        "price" : {
            "order" : "asc"
        }
    }
}
query 查询要带的参数
match_all代表匹配查询
from 代表起始的数据条数
size 每页的数量
_source 要显示那些字段,要全部显示不传_source 即可
sort 排序 后面跟字段 字段跟 order 然后传是 asc还是desc

# GET请求 多条件查询
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "query" :{
        "bool" : {
            "must" : [
                {
                    "match" : {
                        "category" : "小米"
                    }
                },
                {
                    "match" : {
                        "price" : 3999.00
                    }
                }
            ]
        }
    }
}
query 代表查询
bool 条件参数
must 里面的条件必须都成立
should 或者的意思,(把上面的must 改成should即可)
match 匹配查询

# 范围查询
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "query" :{
        "bool" : {
            "should" : [
                {
                    "match" : {
                        "category" : "小米"
                    }
                },
                {
                    "match" : {
                        "price" : 3999.00
                    }
                }
            ],
            "filter" : {
                "range" : {
                    "price" : {
                        "gt" : 2000
                    }
                }    
            }
        }
    }
}

filter 过滤
range 范围 
price 字段  gt 大于

# GET请求 高亮
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "query" :{
        "bool" : {
            "should" : [
                {
                    "match" : {
                        "category" : "小米"
                    }
                },
                {
                    "match" : {
                        "price" : 3999.00
                    }
                }
            ],
            "filter" : {
                "range" : {
                    "price" : {
                        "gt" : 2000
                    }
                }    
            }
        }
    },
    "highlight" : {
        "fields" : {
            "category" : {

            }
        },
        "pre_tags": [
          "<em class=\"hlt1\">"
        ],
        "post_tags": [
            "</em>"
        ],
        "tags_schema" : "styled"
    }
}

# GET请求 聚合操作 分组
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "aggs" : {
        "price_group" : {
            "terms" : {
                "field" : "price"
            }
        }
    },
    "size" : 0
}
aggs 聚合操作
price_group 这个是自定义的名称,也就是分组的名称
terms 分组
field 分组字段
size 0 代表不要原始数据

# GET请求 聚合操作 平均值
(域名)/(索引)/_search
http://localhost:9200/shopping/_search
{
    "aggs" : {
        "price_avg" : {
            "avg" : {
                "field" : "price"
            }
        }
    },
    "size" : 0
}
aggs 聚合操作
price_avg 这个是自定义的名称,也就是平均值的名称
avg 平均值
field 平均值字段
size 0 代表不要原始数据

修改文档

# PUT请求 全量修改
(域名)/(索引)/_doc/(文档ID)
http://localhost:9200/shopping/_doc/555
{
    "title" : "小米手机",
    "category" : "小米",
    "images" : "https://tupan.baidu.com",
    "price" : 3999.00
}

# POST请求 局部修改
(域名)/(索引)/_update/(文档ID)
http://localhost:9200/shopping/_update/555
# body数据写上修改的
{
    "doc" : {
        "title" : "八八八"
    }
}

删除文档

# DELETE请求
(域名)/(索引)/_doc/(文档ID)
http://localhost:9200/shopping/_doc/555

# 返回结果
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "555",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 15,
    "_primary_term": 1
}
通过 result 判断操作

映射关系

# PUT请求 创建映射关系
(域名)/(索引)/_mapping
http://localhost:9200/user/_mapping
{
    "properties" : {
        "name" : {
            "type" : "text",
            "index" : true
        },
        "sex" : {
            "type" : "keyword",
            "index" : true
        },
        "tel" : {
            "type" : "keyword",
            "index" : false
        }
    }
}
properties 属性的映射关系
name 字段
type  字段类型 text 可以被分词  keyword不分词
index 是否可以查询


# GET请求 获取映射关系
(域名)/(索引)/_mapping
http://localhost:9200/user/_mapping
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值