使用postman和es插件操作elasticsearch API

本文介绍了使用postman和es浏览器插件操作elasticsearch API的常用方法

本文使用的es浏览器插件时edge下的elasticvue,可以在edge的应用商店直接搜索安装,相较于es-head,这个插件一直在维护更新,使用还是很方便的

 

 

索引管理

查看索引

查看索引主要使用get方法,可以查看单个or多个索引,也可以查看索引的详细信息

 

创建索引

创建索引主要使用put方法,可以指定创建索引的主分片数和副本数,如果不指定参数,则默认创建5个分片及1个副本

创建好后,可以通过elasticvue插件查看

可以看到一个指定参数的索引,只有主分片没有副本

进一步点击查看索引详细信息

可以通过uuid在elasticsearch部署主机找到相应的数据存放目录

 修改索引

 通过put方法修改索引副本数

 

删除索引

使用delete方法即可删除索引

可以看到linux-beretxj-02索引已经被删除

索引别名 

使用post方法,可同时创建多个索引的别名

 可以通过get方法or浏览器插件查看别名

 

可以通过post方法删除索引别名

在删除前再给linux-bertxj-01创建一个别名

 

 

 

如果要修改索引别名,可以通过先删除,再新增的方法来实现

比如将linux-beretxj-03的bieming-03修改为aaaaa

 

打开关闭索引

关闭索引以后,索引将不可用,但数据还在,在生产中,可以先关闭,待确认清楚后再删除,以确保安全,如果确认后索引还有用,可以再打开,恢复索引的使用

 

可以看到索引状态已显示为close

使用post方法调用_open接口就可以打开

此外还可以使用通配符的方法同时对多个索引进行打开or关闭

 

管理文档 

创建文档 

以上为不指定id创建文档,也可以指定id创建文档

 

 查看文档

也可以通过指定id,查看单条文档

修改文档

覆盖更新一条文档

 局部更新

 

可以看到增加了一个字段 

删除文档 

使用delete方法,通过指定id删除

批量操作 

批量创建文档 

 

批量创建时,发现有报错,查阅文档后发现,es7以后的版本批量创建时,不用指定文档类型,但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"} }

 按照官方案例进行修改

{ "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} }

 

批量查看 

通过查看官方文档,批量查看需要使用_mget

 

如下方式进行查询

{
    "docs": [
        {
            "_index": "linux-beretxj-01",
            "_type": "_doc",
            "_id": "OQlfkIUBwAMQjjM14rzC"
        },
        {
            "_index": "linux-beretxj-01",
            "_type": "_doc",
            "_id": "1001"
        }
    ]
}

 

映射 

创建索引时指定映射关系及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"
    }
  }
}

 创建索引时指定映射关系

造测试数据

指定ip网段进行查询

 

为已创建的索引修改映射关系 

 根据官网,6.8版本的语法格式为:

PUT twitter/_mapping/_doc 
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}

 

 插入几条信息后进行查询

IK分词器 

内置英文分词器

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中文分词效果还是不错的

 

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值