本文介紹了使用postman和es瀏覽器插件操作elasticsearch API的常用方法
本文使用的es瀏覽器插件時(shí)edge下的elasticvue,可以在edge的應(yīng)用商店直接搜索安裝,相較于es-head,這個(gè)插件一直在維護(hù)更新,使用還是很方便的
?
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-725255.html
索引管理
查看索引
查看索引主要使用get方法,可以查看單個(gè)or多個(gè)索引,也可以查看索引的詳細(xì)信息
?
創(chuàng)建索引
創(chuàng)建索引主要使用put方法,可以指定創(chuàng)建索引的主分片數(shù)和副本數(shù),如果不指定參數(shù),則默認(rèn)創(chuàng)建5個(gè)分片及1個(gè)副本
創(chuàng)建好后,可以通過(guò)elasticvue插件查看
可以看到一個(gè)指定參數(shù)的索引,只有主分片沒(méi)有副本
進(jìn)一步點(diǎn)擊查看索引詳細(xì)信息
可以通過(guò)uuid在elasticsearch部署主機(jī)找到相應(yīng)的數(shù)據(jù)存放目錄
?修改索引
?通過(guò)put方法修改索引副本數(shù)
?
刪除索引
使用delete方法即可刪除索引
可以看到linux-beretxj-02索引已經(jīng)被刪除
索引別名?
使用post方法,可同時(shí)創(chuàng)建多個(gè)索引的別名
?可以通過(guò)get方法or瀏覽器插件查看別名
?
可以通過(guò)post方法刪除索引別名
在刪除前再給linux-bertxj-01創(chuàng)建一個(gè)別名
?
?
?
如果要修改索引別名,可以通過(guò)先刪除,再新增的方法來(lái)實(shí)現(xiàn)
比如將linux-beretxj-03的bieming-03修改為aaaaa
?
打開(kāi)關(guān)閉索引
關(guān)閉索引以后,索引將不可用,但數(shù)據(jù)還在,在生產(chǎn)中,可以先關(guān)閉,待確認(rèn)清楚后再刪除,以確保安全,如果確認(rèn)后索引還有用,可以再打開(kāi),恢復(fù)索引的使用
?
可以看到索引狀態(tài)已顯示為close
使用post方法調(diào)用_open接口就可以打開(kāi)
此外還可以使用通配符的方法同時(shí)對(duì)多個(gè)索引進(jìn)行打開(kāi)or關(guān)閉
?
管理文檔?
創(chuàng)建文檔?
以上為不指定id創(chuàng)建文檔,也可以指定id創(chuàng)建文檔
?
?查看文檔
也可以通過(guò)指定id,查看單條文檔
修改文檔
覆蓋更新一條文檔
?局部更新
?
可以看到增加了一個(gè)字段?
刪除文檔?
使用delete方法,通過(guò)指定id刪除
批量操作?
批量創(chuàng)建文檔?
?
批量創(chuàng)建時(shí),發(fā)現(xiàn)有報(bào)錯(cuò),查閱文檔后發(fā)現(xiàn),es7以后的版本批量創(chuàng)建時(shí),不用指定文檔類型,但es6版本需要指定
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
?按照官方案例進(jìn)行修改
{ "create": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1001} }
{ "name": "吳京","movie": ["太極宗師","流浪地球"],"country": "China"}
{ "create": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1002} }
{ "name": "a","movie": ["aaa","aaaa"],"country": "China"}
{ "create": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1003} }
{ "name": "b","movie": ["bbb","bbbb"],"country": "China"}
{ "create": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1004} }
{ "name": "c","movie": ["ccc","cccc"],"country": "China"}
?
批量修改
{ "update": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1002} }
{ "doc": { "name": "a1","movie": ["1aaa","1aaaa"]} }
{ "update": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1003} }
{ "doc": { "name": "b1","movie": ["1bbb","1bbbb"]} }
{ "update": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1004} }
{ "doc": { "name": "c1","movie": ["1ccc","1cccc"]} }
?
?
批量刪除?
{ "delete": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1002} }
{ "delete": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1003} }
{ "delete": { "_index": "linux-beretxj-01", "_type" : "_doc","_id": 1004} }
?
批量查看?
通過(guò)查看官方文檔,批量查看需要使用_mget
?
如下方式進(jìn)行查詢
{
"docs": [
{
"_index": "linux-beretxj-01",
"_type": "_doc",
"_id": "OQlfkIUBwAMQjjM14rzC"
},
{
"_index": "linux-beretxj-01",
"_type": "_doc",
"_id": "1001"
}
]
}
?
映射?
創(chuàng)建索引時(shí)指定映射關(guān)系及ip案例?
這里直接使用官方的案例
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"ip_addr": {
"type": "ip"
}
}
}
}
}
PUT my_index/_doc/1
{
"ip_addr": "192.168.1.1"
}
GET my_index/_search
{
"query": {
"term": {
"ip_addr": "192.168.0.0/16"
}
}
}
?創(chuàng)建索引時(shí)指定映射關(guān)系
造測(cè)試數(shù)據(jù)
指定ip網(wǎng)段進(jìn)行查詢
?
為已創(chuàng)建的索引修改映射關(guān)系?
?根據(jù)官網(wǎng),6.8版本的語(yǔ)法格式為:
PUT twitter/_mapping/_doc
{
"properties": {
"email": {
"type": "keyword"
}
}
}
?
?插入幾條信息后進(jìn)行查詢
IK分詞器?
內(nèi)置英文分詞器
IK中文分詞器?
從以下地址下載https://github.com/medcl/elasticsearch-analysis-ik/releases?page=4
下載6.8.23版本,與elasticsearch版本一致?
[root@VM-20-10-centos plugins]# pwd
/usr/share/elasticsearch/plugins
[root@VM-20-10-centos plugins]# ls -ld .
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Jan 7 2022 .
[root@VM-20-10-centos plugins]# mkdir ik
[root@VM-20-10-centos plugins]# cd ik
[root@VM-20-10-centos ik]# wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.23/elasticsearch-analysis-ik-6.8.23.zip
--2023-01-08 19:15:30-- https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.23/elasticsearch-analysis-ik-6.8.23.zip
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/2993595/f25df4d6-dd2d-447b-acd5-dd5811e28d8d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230108%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230108T111531Z&X-Amz-Expires=300&X-Amz-Signature=e413b87851160f50cd1165d3002b647ed0b0e2deabb60b958f2107fc828cba4d&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2993595&response-content-disposition=attachment%3B%20filename%3Delasticsearch-analysis-ik-6.8.23.zip&response-content-type=application%2Foctet-stream [following]
--2023-01-08 19:15:31-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/2993595/f25df4d6-dd2d-447b-acd5-dd5811e28d8d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230108%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230108T111531Z&X-Amz-Expires=300&X-Amz-Signature=e413b87851160f50cd1165d3002b647ed0b0e2deabb60b958f2107fc828cba4d&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=2993595&response-content-disposition=attachment%3B%20filename%3Delasticsearch-analysis-ik-6.8.23.zip&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.110.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4504589 (4.3M) [application/octet-stream]
Saving to: ‘elasticsearch-analysis-ik-6.8.23.zip’
elasticsearch-analysis-ik-6 100%[=========================================>] 4.30M 14.6KB/s in 5m 16s
2023-01-08 19:20:48 (13.9 KB/s) - ‘elasticsearch-analysis-ik-6.8.23.zip’ saved [4504589/4504589]
[root@VM-20-10-centos ik]# unzip elasticsearch-analysis-ik-6.8.23.zip
[root@VM-20-10-centos plugins]# chown -R elasticsearch:elasticsearch ik
[root@VM-20-10-centos plugins]# ll
total 4
drwxr-xr-x 3 elasticsearch elasticsearch 4096 Jan 8 19:21 ik
[root@VM-20-10-centos plugins]# ls -l ik
total 1432
-rw-r--r-- 1 elasticsearch elasticsearch 263965 Jan 18 2022 commons-codec-1.9.jar
-rw-r--r-- 1 elasticsearch elasticsearch 61829 Jan 18 2022 commons-logging-1.2.jar
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Jan 18 2022 config
-rw-r--r-- 1 elasticsearch elasticsearch 54739 Jan 18 2022 elasticsearch-analysis-ik-6.8.23.jar
-rw-r--r-- 1 elasticsearch elasticsearch 736658 Jan 18 2022 httpclient-4.5.2.jar
-rw-r--r-- 1 elasticsearch elasticsearch 326724 Jan 18 2022 httpcore-4.4.4.jar
-rw-r--r-- 1 elasticsearch elasticsearch 1807 Jan 18 2022 plugin-descriptor.properties
-rw-r--r-- 1 elasticsearch elasticsearch 125 Jan 18 2022 plugin-security.policy
#重啟elasticsearch
[root@VM-20-10-centos plugins]# systemctl restart elasticsearch.service
[root@VM-20-10-centos plugins]# systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2023-01-08 19:22:57 CST; 11s ago
Docs: http://www.elastic.co
Main PID: 2498058 (java)
Tasks: 21 (limit: 23722)
Memory: 1.2G
CGroup: /system.slice/elasticsearch.service
├─2498058 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -X>
└─2498122 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
Jan 08 19:22:57 VM-20-10-centos systemd[1]: Started Elasticsearch.
[root@VM-20-10-centos plugins]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 1024 0.0.0.0:3307 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 50 [::ffff:127.0.0.1]:9600 *:*
LISTEN 0 1024 [::]:3307 [::]:*
LISTEN 0 1024 *:9200 *:*
LISTEN 0 1024 *:9300 *:*
LISTEN 0 1024 *:5044 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
?可以看到使用ik中文分詞效果還是不錯(cuò)的
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-725255.html
?
到了這里,關(guān)于使用postman和es插件操作elasticsearch API的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!