当前位置:   article > 正文

Elasticsearch - HTTP

Elasticsearch - HTTP

安装

https://www.elastic.co/downloads/past-releases/elasticsearch-7-17-0
下载完成启动bin/elasticsearch服务,可以在Postman调试各种请求。

基本语法

索引

创建索引
PUT: http://127.0.0.1:9200/shopping

Response:
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

多次创建同一个索引时,会报错,说明创建请求是幂等性的(同一个操作执行多次和执行一次效果相同,不会因为多次执行而产生不同的效果)。所以应该用PUT而非POSTPOST每次都会创建一条新的记录,对于同一个请求,并不会由于索引重复而报错。

查看索引

GET: http://127.0.0.1:9200/shopping
在这里插入图片描述
包含了每个索引的健康状况、状态、名称、唯一ID(UUID)、分片数(primary和replica)、包含的文档数、删除的文档数、以及存储大小所有主分片占用的物理磁盘空间总。

删除索引

DELETE: http://127.0.0.1:9200/shopping

文档

创建文档
POST: http://127.0.0.1:9200/shopping/_doc`或者`POST: http://127.0.0.1:9200/shopping/_create

Request body:
{
    "name": "xiaomi su7",
    "type": 1,
    "price": 29.99
}

Response:
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "bhajbY8BDgHLWJjh9Xp7",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

如果觉得id不好记忆,可以指定id创建文档:POST/PUT: http://127.0.0.1:9200/shopping/_doc/1001,重复调用会更新文档,如果明确用_create,重复调用会有冲突。

更新文档
POST: http://127.0.0.1:9200/shopping/_update/1001

Request body:
{
    "doc": {
        "compony": "xiaomi "
    }
}

Response:
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 9,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 10,
    "_primary_term": 1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

如果加上doc就是增量更新,否则为全亮更新。

匹配查询
GET: http://127.0.0.1:9200/shopping/_search

Request Body:
{
    "query": {
        "match_all": {
        }
        /**
        "match": {
            "compony": "xiaomi"
        }
		*/

    "from": 0,
    "size": 2,
    "_source": ["type", "price"],
    "sort": {
        "price": {
            "order": "desc"
        }
    }
}

Response:
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1003",
                "_score": null,
                "_source": {
                    "price": 39.99,
                    "type": 1
                },
                "sort": [
                    39.99
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "bhajbY8BDgHLWJjh9Xp7",
                "_score": null,
                "_source": {
                    "price": 29.99,
                    "type": 1
                },
                "sort": [
                    29.99
                ]
            }
        ]
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
多条件查询
GET: http://127.0.0.1:9200/shopping/_search

Request Body:
{
    "query": {
        "bool": {
        	// 数组里的条件同时满足
            "must": [
                {
                    "match": {
                        "name": "xiaomi su7"
                    }
                },
                {
                    "match": {
                        "type": 1
                    }
                }
            ],
            // 数组里的条件满足一个即可
            "should": [
                {
                    "match": {
                        "price": 39.99
                    }
                }
            ],
            // 对结果进行范围过滤
            "filter": {
                "range": {
                    "price": {
                        "gt": 30
                    }
                }
            }
        }
    }
}

Response:
{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.2391143,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1003",
                "_score": 2.2391143,
                "_source": {
                    "name": "xiaomi su7 pro",
                    "type": 1,
                    "price": 39.99
                }
            }
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

match 是分词检索,doc在存储的时候是倒排索引,会把里面的字段按空格(实验结果)拆成一些词,每个词都会对应一条id的数据。默认查询时用的并不是全词匹配,比如:

{
    "query": {
        "match": {
            "name": "su7"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

能匹配到name = "xiaomi su7" / "xiaomi su7 pro" 的数据。

聚合查询
GET: http://127.0.0.1:9200/shopping/_search

Request Body:
{
    "aggs": {
        "price_group": {
            "terms": { // 分组
                "field": "price"
            }
        }
    },
    "size": 0
}

Response:
{
    "took": 15,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 29.989999771118164,
                    "doc_count": 2
                },
                {
                    "key": 39.9900016784668,
                    "doc_count": 1
                }
            ]
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
映射

可以为某个索引设置一些映射的字段,包括字段是否能用分词查询(否则全亮匹配)、是否能用来查询。

PUT: http://127.0.0.1:9200/user/_mapping

Request Body:
{
    "properties": {
        "name": {
            "type": "text", // 可分词查询
            "index": true.  // 可作为查询字段   
        },
        "sex": {
            "type": "keyword", // 只能全亮匹配
            "index": true
        },
        "tel": {
            "type": "keyword",
            "index": false     // 不可作为查询字段
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/580264
推荐阅读
相关标签
  

闽ICP备14008679号