Elasticsearch
数据格式
es与mysql的数据类型对比
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HUt5mmzv-1634191597434)(/Users/ming/Library/Application Support/typora-user-images/image-20211013105555799.png)]
倒排索引
HTTP操作es
索引操作
es中的index(索引)可以类比于mysql中的数据库
创建一个index
#请求方式:PUT
#请求地址:http://localhost:9200/shopping
#请求结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
#使用postman向e s发送以上请求,会创建一个shopping index
#index具有密等性,不能重复创建
查询创建的索引信息
#请求方式:GET
#请求地址:http://localhost:9200/shopping
#请求结果:
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1634106945424",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "XeJgkDneQEW-Ege-vt7kDQ",
"version": {
"created": "5061299"
},
"provided_name": "shopping"
}
}
}
}
删除索引
#请求方式:DELETE
#请求地址:http://localhost:9200/shopping
#请求结果:
{
"acknowledged": true
}
文档
创建文档
1、不指定ID创建
#请求方式:POST
#请求地址:http://localhost:9200/shopping/_doc
#body:
{
"title":"小米手机",
"compony":"小米",
"price":"2499"
}
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
#可以看出,id是es随机生成的
2、指定ID创建
#请求方式:POST
#请求地址:http://localhost:9200/shopping/_doc/10001
#body:
{
"title":"小米手机",
"compony":"小米",
"price":"2499"
}
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
#指定id之后,文档就具有了密等性,所以也可以使用PUT请求创建文档
查询文档
1、指定ID查询
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_doc/10001
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
}
}
2、查询索引下所有文档
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#请求结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_score": 1.0,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
}
}
]
}
}
更新文档
1、完全更新(全量覆盖)
#请求方式:PUT
#请求地址:http://localhost:9200/shopping/_doc/10001
#body:
{
"title":"华为手机",
"compony":"小米",
"price":"2499"
}
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
2、部分更新(部分覆盖)
#请求方式:POST
#请求地址:http://localhost:9200/shopping/_update/10001
#body:
{
"doc": {
"price": "6799"
}
}
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
删除文档
#请求方式:DELETE
#请求地址:http://localhost:9200/shopping/_doc/10001
#请求结果:
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
条件查询
1、匹配查询(全文检索)
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query":{
"match":{ #匹配查询
"compony":"小米" #查询条件
}
}
}
#请求结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_score": 0.5753642,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
}
}
]
}
}
2、分页查询
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query":{
"match":{
"compony":"小米"
}
},
"from":0, #(页码-1)*页大小
"size":5 #页大小
}
#请求结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 0.12907705,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_score": 0.12907705,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0.12907705,
"_source": {
"title": "小米手机1",
"compony": "小米",
"price": "2499"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 0.12907705,
"_source": {
"title": "小米手机3",
"compony": "小米",
"price": "2499"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1004",
"_score": 0.12907705,
"_source": {
"title": "小米手机4",
"compony": "小米",
"price": "2499"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1005",
"_score": 0.12907705,
"_source": {
"title": "小米手机5",
"compony": "小米",
"price": "2499"
}
}
]
}
}
3、查询指定字段
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query":{
"match":{
"compony":"小米"
}
},
"from":0,
"size":5,
"_source":["title"] #查询指定字段
}
#请求结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 7,
"relation": "eq"
},
"max_score": 0.12907705,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_score": 0.12907705,
"_source": {
"title": "小米手机"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0.12907705,
"_source": {
"title": "小米手机1"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 0.12907705,
"_source": {
"title": "小米手机3"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1004",
"_score": 0.12907705,
"_source": {
"title": "小米手机4"
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1005",
"_score": 0.12907705,
"_source": {
"title": "小米手机5"
}
}
]
}
}
4、排序
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"match": {
"compony": "小米"
}
},
"from": 0,
"size": 5,
"_source": [
"title"
],
"sort": {
"price": {
"order": "desc"
}
}
}
#请求结果:
5、多条件查询
1、a n d查询,多条件同时满足
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"match": {
"compony": "小米"
}
},
"from": 0,
"size": 5,
"_source": [
"title"
],
"sort": {
"price": {
"order": "desc"
}
}
}
#请求结果:
2、o r查询
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米手机"
}
},
{
"match": {
"title": "华为手机"
}
}
]
}
}
}
3、范围查询
查询手机价格大于5000的
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米手机"
}
},
{
"match": {
"title": "华为手机"
}
}
],
"filter":{
"range":{
"price":{
"gt":5000
}
}
}
}
}
}
4、完全匹配查询
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"title": "小米手机6"
}
}
]
}
}
}
#请求结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 2.164663,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "1006",
"_score": 2.164663,
"_source": {
"title": "小米手机6",
"compony": "小米",
"price": "2499"
}
}
]
}
}
5、对查询结果指定字段高亮显示
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"title": "小米手机"
}
}
]
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
#请求结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": 0.58032644,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "rk6ZeHwBUEOTkepO7sR9",
"_score": 0.58032644,
"_source": {
"title": "小米手机",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1002",
"_score": 0.5311859,
"_source": {
"title": "小米手机1",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>1"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1003",
"_score": 0.5311859,
"_source": {
"title": "小米手机3",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>3"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1004",
"_score": 0.5311859,
"_source": {
"title": "小米手机4",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>4"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1005",
"_score": 0.5311859,
"_source": {
"title": "小米手机5",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>5"
]
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1006",
"_score": 0.5311859,
"_source": {
"title": "小米手机6",
"compony": "小米",
"price": "2499"
},
"highlight": {
"title": [
"<em>小</em><em>米</em><em>手</em><em>机</em>6"
]
}
}
]
}
}
聚合查询
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"aggs":{ //聚合操作
"price_group":{//名称,随意起名
"terms":{//分组
"field":"price"//分组字段
}
}
},
"size":0
}
#请求结果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 12,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2499,
"doc_count": 2
},
{
"key": 2899,
"doc_count": 2
},
{
"key": 6799,
"doc_count": 2
},
{
"key": 499,
"doc_count": 1
},
{
"key": 1499,
"doc_count": 1
},
{
"key": 2699,
"doc_count": 1
},
{
"key": 3299,
"doc_count": 1
},
{
"key": 5299,
"doc_count": 1
},
{
"key": 5499,
"doc_count": 1
}
]
}
}
}
平均值
#请求方式:GET
#请求地址:http://localhost:9200/shopping/_search
#body:
{
"aggs":{
"price_avg":{
"avg":{
"field":"price"
}
}
},
"size":0
}
#请求结果:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 12,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"price_avg": {
"value": 3599.0
}
}
}