Elasticsearch之http-索引与文档

数据格式

Elasticsearch是面向文档型数据库,一条数据在这就是一个文档。下图是Elasticsearch和mysql的概念类比:

        ES 里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个type, Elasticsearch 7.X 中, Type 的概念已经被删除了。

索引

正排索引

倒排索引

 强调关键字和文档编号的关联,表的作用逐渐弱化。

操作

创建

对比关系型数据库,创建索引就等同于创建数据库。

向 ES 服务器发 PUT 请求 : http://127.0.0.1:9200/shopping(PUT请求代表创建)

如果重复发 PUT 请求 : http://127.0.0.1:9200/shopping 添加索引,会返回错误信息 : 

请求只允许使用:PUT, GET, HEAD, DELETE。

查询

查询单个索引:向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping

{
    "shopping": {//索引名
        "aliases": {},//别名
        "mappings": {},//映射
        "settings": {//设置
            "index": {//设置 - 索引
                "creation_date": "1617861426847",//设置 - 索引 - 创建时间
                "number_of_shards": "1",//设置 - 索引 - 主分片数量
                "number_of_replicas": "1",//设置 - 索引 - 主分片数量
                "uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
                "version": {//设置 - 索引 - 主分片数量
                    "created": "7080099"
                },
                "provided_name": "shopping"//设置 - 索引 - 主分片数量
            }
        }
    }
}

查看所有索引:向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/_cat/indices?v

请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引

health:当前服务器健康状态:green(集群完整)、yellow(单点正常、集群不完整)、red(单点不正常)

status:索引打开、关闭状态

index:索引名

uuid:索引统一编号

pri:主分片数量

rep:副本数量

docs.count:可用文档数量

docs.deleted:文档删除状态

store.size:主分片和副分片整体占空间大小

pri.store.size:主分片占空间大小

删除

向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping

再次查看索引:GET http://127.0.0.1:9200/_cat/indices?v

成功删除!

文档

创建文档

        索引已经创建好了(shopping),接下来我们来创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式;

向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc,请求体JSON内容为:

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

返回结果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",//<------------------自定义唯一性标识
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

 如果增加数据时明确数据主键,那么请求方式也可以为 PUT,保证幂等性。

向 ES 服务器发 PUT请求 : http://127.0.0.1:9200/shopping/_doc/1001

查询文档

查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询

向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1001:

查找不存在的内容,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1002:

查看索引下所有数据,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_search:

{

    "took": 6,

    "timed_out": false,

    "_shards": {

        "total": 1,

        "successful": 1,

        "skipped": 0,

        "failed": 0

    },

    "hits": {

        "total": {

            "value": 3,

            "relation": "eq"

        },

        "max_score": 1,

        "hits": [

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1001",

                "_score": 1,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            },

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1002",

                "_score": 1,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            },

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1003",

                "_score": 1,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            }

        ]

    }

}

修改文档

全量修改

和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖

向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc/1001

请求体JSON内容为:

{
    "title":"华为手机",
    "category":"华为",
    "images":"http://www.gulixueyuan.com/hw.jpg",
    "price":1999.00
}

修改成功后,服务器响应结果:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",//<-----------updated 表示数据被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

局部修改

修改数据时,也可以只修改某一给条数据的局部信息

向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_update/1001

{
    "doc": {
        "title":"小米手机",
        "category":"小米"
    }
}

返回结果如下:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",//<-----------updated 表示数据被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

删除文档

删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。

向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping/_doc/1001

返回结果:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted",//<---删除成功
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_doc/1001,查看是否删除成功:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "found": false
}

查询

条件查询

URL带参查询

查找category为小米的文档为例,

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search?q=category:小米

返回结果:

{

    "took": 171,

    "timed_out": false,

    "_shards": {

        "total": 1,

        "successful": 1,

        "skipped": 0,

        "failed": 0

    },

    "hits": {

        "total": {

            "value": 3,

            "relation": "eq"

        },

        "max_score": 0.26706278,

        "hits": [

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1001",

                "_score": 0.26706278,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            },

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1002",

                "_score": 0.26706278,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            },

            {

                "_index": "shopping",

                "_type": "_doc",

                "_id": "1003",

                "_score": 0.26706278,

                "_source": {

                    "title": "小米手机",

                    "category": "小米",

                    "images": "http://www.gulixueyuan.com/xm.jpg",

                    "price": 3999

                }

            }

        ]

    }

}

        URL带参数形式查询,很不安全,或者参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询。

请求体带参查询

JSON请求体,还是查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match":{
            "category":"小米"
        }
    }
}

带请求体方式的查找所有内容

查找所有文档内容,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_all":{}
    }
}

段查询指定字

如果你想查询指定字段,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_all":{}
    },
    "_source":["title"]
}

分页查询

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":2
}

查询排序

        想通过排序查出价格最高的手机,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_all":{}
    },
    "sort":{
        "price":{
            "order":"desc"
        }
    }
}

多条件查询

假设想找出小米牌子,价格为3999元的。

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "price":3999.00
                }
            }]
        }
    }
}

范围查询

假设想找出小米和华为的牌子,价格大于2000元的手机。

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "category":"华为"
                }
            }],
            "filter":{
                "range":{
                    "price":{
                        "gt":2000
                    }
                }
            }
        }
    }
}

全文检索

像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和华为的。

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match":{
            "category" : "小华"
        }
    }
}

完全匹配

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    }
}

高亮查询

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    },
    "highlight":{
        "fields":{
            "category":{}//<----高亮这字段
        }
    }
}

聚合查询

聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值max、平均值avg等等。

按price字段进行分组:

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "aggs":{//聚合操作
        "price_group":{//名称,随意起名
            "terms":{//分组
                "field":"price"//分组字段
            }
        }
    }
}

若想对所有手机价格求平均值:

向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:

{
    "aggs":{
        "price_avg":{//名称,随意起名
            "avg":{//求平均
                "field":"price"
            }
        }
    },
    "size":0
}

  • 21
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值