文章目录
- 创建索引
- 查询索引
- 查询 shopping索引
- 查询所有索引
- 删除索引
- 数据操作
- 添加
- 查询
- 查询指定id数据
- 查询所有数据
- 修改
- 完全覆盖
- 局部修改
- 删除
- 复杂查询操作
- 条件查询
- 根据字段名和值查询对应数据
- 全量查询
- 分页查询
- 显示指定字段
- 排序查询
- 多条件查询
- must(and)
- filter (where)
- 完全匹配
- 高亮查询
- 聚合查询
- 分组
- 求平均
创建索引
创建索引等同于创建数据库
创建一个shopping的索引需要 在Postman中,向ES服务器发送 PUT
请求:
http://127.0.0.1:9200/shopping
{"acknowledged": true,"shards_acknowledged": true,"index": "shopping"
}
PUT
具有幂等性再次请求相同名称的索引会出现问题,提示你重复了。
查询索引
查询 shopping索引
获取 shopping 索引相关信息。
用 GET
请求发送
http://127.0.0.1:9200/shopping
{"shopping": {"aliases": {},"mappings": {},"settings": {"index": {"routing": {"allocation": {"include": {"_tier_preference": "data_content"}}},"number_of_shards": "1","provided_name": "shopping","creation_date": "1670935714623","number_of_replicas": "1","uuid": "vC5bzEg2SK-yTYK20D2jMA","version": {"created": "7160299"}}}}
}
查询所有索引
GET
http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases 2iBY2CVPTaSv3xUEjYU_2g 1 0 40 0 38mb 38mb
yellow open shopping vC5bzEg2SK-yTYK20D2jMA 1 1 0 0 226b 226b
删除索引
DELETE
http://127.0.0.1:9200/shopping
{"acknowledged": true
}
数据操作
添加
POST
http://127.0.0.1:9200/shopping/_doc/1001 并且传递json参数
{"title":"小米手机","category":"小米","images":"1.png","price":1999
}
_doc 文档数据
/1001 指定id
id可以不填发送 http://127.0.0.1:9200/shopping/_doc 会随机创建id
返回json
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 1,"_primary_term": 1
}
查询
查询指定id数据
GET
http://127.0.0.1:9200/shopping/_doc/1001 无需携带body
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 1,"_seq_no": 1,"_primary_term": 1,"found": true,"_source": {"title": "小米手机","category": "小米","images": "1.png","price": 1999}
}
查询所有数据
GET
http://127.0.0.1:9200/shopping/_search 无需携带body
{"took": 173,"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": "nveSC4UBUGCCAe-4B2he","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "1.png","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "1001","_score": 1.0,"_source": {"title": "小米手机","category": "小米","images": "1.png","price": 1999}}]}
}
修改
完全覆盖
PUT
http://127.0.0.1:9200/shopping/_doc/1001
{"title":"小米手机","category":"小米","images":"1.png","price":4999
}
返回json
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 2,"_primary_term": 1
}
局部修改
POST
http://127.0.0.1:9200/shopping/_update/1001
{"doc":{"title":"华为手机"}
}
返回json
{"_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://127.0.0.1:9200/shopping/_doc/1001
{"_index": "shopping","_type": "_doc","_id": "1001","_version": 4,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 4,"_primary_term": 1
}
复杂查询操作
条件查询
根据字段名和值查询对应数据
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match":{"category":"小米"}}
}
返回json
{"took": 6,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 0.26706278,"hits": [{"_index": "shopping","_type": "_doc","_id": "nveSC4UBUGCCAe-4B2he","_score": 0.26706278,"_source": {"title": "小米手机","category": "小米","images": "1.png","price": 1999}}]}
}
全量查询
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}}
}
分页查询
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}},"from":0, // 偏移量"size":2 // 每页数量
}
显示指定字段
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}},"from":0, // 偏移量"size":2, // 每页数量"_source":["title"]
}
排序查询
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}},"from":0, // 偏移量"size":2, // 每页数量"_source":["title"],"sort":{"price":{"order":"desc" // asc}}
}
多条件查询
must(and)
同时查询品牌是小米,价格为1999的手机
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"bool":{"must":[ // 多个条件同时成立(and) 还有 should(or){"match":{"category":"小米"}},{"match":{"price":1999}}]}}
}
filter (where)
查询类型是小米或者华为的价格大于 1000 的手机
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"bool":{"should":[{"match":{"category":"小米"}},{"match":{"category":"华为"}}],"filter":{"range":{"price":{"gt":1000}}}}}
}
完全匹配
我们之前使用都是 全文检索匹配
输入的内容会被分词查询。如果想要其精准匹配需要使用 完全匹配
match_phrase
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_phrase":{"category":"小米"}}
}
高亮查询
对查询出来结果的数据 category
进行高亮显示
GET
http://127.0.0.1:9200/shopping/_search
{"query":{"match_phrase":{"category":"小米"}},"highlight":{"fields":{"category":{}}}
}
聚合查询
分组
对价格进行分组
GET
http://127.0.0.1:9200/shopping/_search
{"aggs":{ // 聚合操作"price_group":{ // 分组后名称,随意起"terms":{ // 分组"field":"price" // 分组字段}}}
}
求平均
对价格进行平均
GET
http://127.0.0.1:9200/shopping/_search
{"aggs":{ // 聚合操作"price_avg":{ // 分组后名称,随意起"avg":{ // 分组"field":"price" // 分组字段}}}
}