h1. 创建索引
PUT http://localhost:9200/shopping
- {
- "acknowledged": true,
- "shards_acknowledged": true,
- "index": "shopping"
- }
重复创建索引
- {
- "error": {
- "root_cause": [
- {
- "type": "resource_already_exists_exception",
- "reason": "index [shopping/UgVEWxBdTOWi7DO9TfZUfw] already exists",
- "index_uuid": "UgVEWxBdTOWi7DO9TfZUfw",
- "index": "shopping"
- }
- ],
- "type": "resource_already_exists_exception",
- "reason": "index [shopping/UgVEWxBdTOWi7DO9TfZUfw] already exists",
- "index_uuid": "UgVEWxBdTOWi7DO9TfZUfw",
- "index": "shopping"
- },
- "status": 400
- }
2. 查看所有索引
GET http://localhost:9200/_cat/indices?v (v代表携带表头)
- health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
- yellow open holmes_im_message_202207 a3Y4s1kNQj6TNgknOAcZEQ 5 1 0 0 1.2kb 1.2kb
- yellow open shopping UgVEWxBdTOWi7DO9TfZUfw 5 1 0 0 1.1kb 1.1kb
- yellow open users RA2vnPmkQhqjegKPcwiYag 5 1 2 0 9.3kb 9.3kb
- yellow open flink_kafka_to_es_user pYsiaUTfQm6qVtR82dTRIA 5 1 10 0 24kb 24kb
- yellow open my_index YzWyj2KrS7-ZBba0tTUa6g 5 1 5 0 13.6kb 13.6kb
- yellow open nba v2yMIG0ZRkCZ54w9KBHXWA 5 1 10 0 40.3kb 40.3kb
- yellow open woodpecker_clue_life_cycle_log_202207 4puKGmyjQJe9zx2VckkC7g 5 1 791 0 577.4kb 577.4kb
- green open .kibana nZWGcHfKTCCtzrmCXdtN1A 1 0 2 0 10.8kb 10.8kb
3. 删除索引
DELETE http://localhost:9200/shopping
4. 创建文档
POST http://localhost:9200/shopping/_doc (POST不是幂等性,PUT是幂等性,添加数据每次返回ID是唯一标识,所以要用POST)
- {
- "title": "小米手机",
- "category": "小米",
- "images": "https://www.baidu.com",
- "price": 3999.00
- }
返回结果
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "lIuU2IIBp4Wa2GZ3RcL_",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 0,
- "_primary_term": 1
- }
PUT http://localhost:9200/shopping/_doc/1001 (这里1001指定了ID 所以满足了幂等性,可以用PUT,但是_version会+1)
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "1001",
- "_version": 1,
- "result": "created",
- "_shards": {
- "total": 2,
- "successful": 1,
- "failed": 0
- },
- "_seq_no": 1,
- "_primary_term": 1
- }
5. 查询文档
GET http://localhost:9200/shopping/_doc/1001
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "1001",
- "_version": 2,
- "found": true,
- "_source": {
- "title": "小米手机",
- "category": "小米",
- "images": "https://www.baidu.com",
- "price": 3999.00
- }
- }
6. 查询索引下全部文档
GET http://localhost:9200/shopping/_search
- {
- "took": 178,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "skipped": 0,
- "failed": 0
- },
- "hits": {
- "total": 2,
- "max_score": 1.0,
- "hits": [
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "lIuU2IIBp4Wa2GZ3RcL_",
- "_score": 1.0,
- "_source": {
- "title": "小米手机",
- "category": "小米",
- "images": "https://www.baidu.com",
- "price": 3999.00
- }
- },
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "1001",
- "_score": 1.0,
- "_source": {
- "title": "小米手机",
- "category": "小米",
- "images": "https://www.baidu.com",
- "price": 3999.00
- }
- }
- ]
- }
- }
7. 更新索引文档
7.1 全量更新
PUT http://localhost:9200/shopping/_doc/1001
- {
- "price": 4999.00
- }
返回结果:别的字段都没有了 ,所以要包含全部的字段和值!
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "1001",
- "_version": 3,
- "found": true,
- "_source": {
- "price": 4999.00
- }
- }
7.2 局部更新
POST http://localhost:9200/shopping/_update/1001
- {
- "doc":{
- "price": 4999.00
- }
- }
更新后结果:
- {
- "_index": "shopping",
- "_type": "_doc",
- "_id": "1001",
- "_version": 2,
- "_seq_no": 1,
- "_primary_term": 1,
- "found": true,
- "_source": {
- "title": "小米手机",
- "category": "小米",
- "images": "https://www.baidu.com",
- "price": 4999.0
- }
- }
8. 删除索引文档
DELETE http://localhost:9200/shopping/_doc/1001
9. 文档查询
GET http://localhost:9200/shopping/_search?q=category:小米
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "match":{
- "category": "大米"
- }
- }
- }
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "match_all":{
- }
- }
- }
10. 分页查询
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "match_all":{
- }
- },
- "from":0,
- "size":3
- }
11. 筛选查询结果字段 排序
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "match_all":{
- }
- },
- "from":0,
- "size":3,
- "_source": ["title","price"],
- "sort": {
- "price":{
- "order": "desc"
- }
- }
- }
12. 查询must or should
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "bool":{
- "should":[
- {
- "match":{
- "category": "小米"
- }
- },
- {
- "match":{
- "price": 4999.0
- }
- }
- ]
- }
- }
- }
13. 查询 过滤 filter
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "bool":{
- "should":[
- {
- "match":{
- "category": "小米"
- }
- },
- {
- "match":{
- "price": 4999.0
- }
- }
- ],
- "filter":{
- "range":{
- "price":{
- "gt": 4000
- }
- }
- }
- }
- }
- }
14. 查询 match_phrase
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "bool":{
- "should":[
- {
- "match_phrase":{
- "category": "小米"
- }
- }
- ]
- }
- }
- }
原理:18.match_phrase的用法 - outback123 - 博客园
15. 高亮查询
GET http://localhost:9200/shopping/_search
- {
- "query":{
- "bool":{
- "must":[
- {
- "match_phrase":{
- "category": "小米"
- }
- }
- ]
- }
- },
- "highlight":{
- "fields":{
- "category": {}
- }
- }
- }