es請求方式呼叫

赤叶秋枫發表於2024-05-09

Es基礎

關係:

ElasticSearch-> mysql

index (索引)-> 資料庫

Documents(文件) -> row(行)

Fileds(欄位)-> column

正排索引 id 內容,類似表格

倒排索引 :keywords : ids

Postman訪問例項

建立索引:建立庫

ip/索引名

請求路徑:PUT http://127.0.0.1:9200/shopping
請求體:none

成功:

{
	"acknowledged": true,
	"shards_acknowledged": true,
	"index": "shopping"
}
查詢當前存在索引:

ip/_cat/indices?v=

請求路徑:GET  http://127.0.0.1:9200/_cat/indices?v=
請求體:none

成功:

health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .geoip_databases 5vZtZiLXTw-ZnE-gxFK4RA   1   0         33           35     34.1mb         34.1mb
yellow open   user1            XvuPXH4GR3qu9kYgI1vMTg   1   1          0            0       226b           226b
yellow open   product          OuJtZ2GNQjaANql9jHIhdw   1   1          0            0       226b           226b
yellow open   user             84VHenNTTtaJyKUQasAZXA   1   1          3            0      4.8kb          4.8kb
yellow open   shopping         vqraISHNSFioVa4h58y_4w   1   1         10            6       28kb           28kb

建立文件:新增行資料

ip/索引名/_doc

請求路徑: POST  http://127.0.0.1:9200/shopping/_doc
請求體:
{
    "name": "小米",
    "price": 1999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "0i0_WI8Bs7gKHbbSH-sS",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 20,
	"_primary_term": 7
}

指定id建立文件:

ip/索引名/_doc/id

請求路徑:POST http://127.0.0.1:9200/shopping/_doc/1006
請求體:
{
    "name": "魅族21",
    "price": 2999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1006",
	"_version": 3,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 21,
	"_primary_term": 7
}
查詢單挑索引:查詢單條資料

ip/索引/_doc/id

請求路徑: GET  http://127.0.0.1:9200/shopping/_doc/1001
請求體:none

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 6,
	"_seq_no": 7,
	"_primary_term": 1,
	"found": true,
	"_source": {
		"name": "小米",
		"price": 3999,
		"url": "htp123"
	}
}
查詢文件列表:列表查詢資料

ip/索引/_search

請求路徑: GET  http://127.0.0.1:9200/shopping/_search
請求體:none

成功:

{
	"took": 2,
	"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": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "華為",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
條件查詢:拆詞查詢
請求路徑: GET  http://127.0.0.1:9200/shopping/_search

{
  "query": {
 "match": { //拆詞單個查詢
   "name": "華"
   }
  }
}

成功:

{
	"took": 6,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1.3340157,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1.3340157,
				"_source": {
					"name": "華為",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1.3340157,
				"_source": {
					"name": "華為",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1.1120224,
				"_source": {
					"name": "華為1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
全詞匹配查詢高亮查詢:
請求路徑: GET  http://127.0.0.1:9200/shopping/_search
請求體:
{
    "query": {
        "match_phrase": { //全詞匹配查詢
            "name": "華為1"
        }
    },
    "highlight": { //高亮顯示這個欄位
        "fields": {
            "name": {}
        }
    }
}

成功:

{
	"took": 58,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 4.0541162,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 4.0541162,
				"_source": {
					"name": "華為1",
					"price": 9999,
					"url": "htp123"
				},
				"highlight": {
					"name": [
						"<em>華</em><em>為</em><em>1</em>"
					]
				}
			}
		]
	}
}
多條件範圍查詢:
請求路徑: GET  http://127.0.0.1:9200/shopping/_search

請求體:
{
    "query": {
        "bool": {
            "must": [ //should表示或,must表示並
                {
                    "match": {
                        "name": "小米"
                    }
                },
                {
                    "match": {
                        "price": 3999
                    }
                }
            ],
            "filter": {
                "range": {
                    "price": {
                        "lt": 5000
                    }
                }
            }
        }
    }
}

成功:

{
	"took": 18,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.4093566,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			}
		]
	}
}
聚合查詢:
請求路徑: GET  http://127.0.0.1:9200/shopping/_search

請求體:
{
    "aggs": {
        "price_group": { //隨意取名
            "terms": { //分組
                "field": "price" //分組欄位
            }
        }
    }
}
{
	"took": 38,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 10,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "0i0_WI8Bs7gKHbbSH-sS",
				"_score": 1,
				"_source": {
					"name": "魅族",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1,
				"_source": {
					"name": "華為",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1002",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1003",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "華為",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1006",
				"_score": 1,
				"_source": {
					"name": "魅族21",
					"price": 2999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "華為1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	},
	"aggregations": {
		"price_group": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": [
				{
					"key": 1999,
					"doc_count": 3
				},
				{
					"key": 3999,
					"doc_count": 3
				},
				{
					"key": 9999,
					"doc_count": 2
				},
				{
					"key": 999,
					"doc_count": 1
				},
				{
					"key": 2999,
					"doc_count": 1
				}
			]
		}
	}
}
文件修改:修改行資料

ip/索引/_doc/id

請求路徑: PUT  http://127.0.0.1:9200/shopping/_doc/1001
請求體:
{
    "name": "小米",
    "price": 9999,
    "url": "htp123"
}

成功:

{
  "_index": "shopping",
  "_type": "_doc",
  "_id": "1001",
  "_version": 7,
  "result": "updated",
  "_shards": {
   "total": 2,
   "successful": 1,
  "failed": 0
  },
  "_seq_no": 22,
  "_primary_term": 7
}
區域性修改文件(某列)

ip/索引/_update/id

請求路徑:PUT  http://127.0.0.1:9200/shopping/_update/1001
請求體:
{
    "doc": {
        "name": "華為1"
    }
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 8,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 23,
	"_primary_term": 7
}

刪除文件:

請求路徑:DELETE  http://127.0.0.1:9200/shopping/_doc/1001
請求體:none

相關文章