ElasticSearch基本指令

https://www.elastic.co/cn/
在postman中,向ES服务器发put请求(增加索引): http://127.0.0.1:9200/shopping
在postman中,向ES服务器发get请求(查询索引): http://127.0.0.1:9200/shopping
在postman中,向ES服务器发get请求(查询所有索引): http://127.0.0.1:9200/_cat/indices?v
在postman中,向ES服务器发delete请求(删除索引): http://127.0.0.1:9200/shopping
在postman中,向ES服务器发post请求(索引增加数据): http://127.0.0.1:9200/shopping/_doc
在postman中,向ES服务器发post请求(索引增加数据:指定生成ID): http://127.0.0.1:9200/shopping/_doc/1001
在postman中,向ES服务器发put请求(索引增加数据:指定生成ID): http://127.0.0.1:9200/shopping/_doc/1001
在postman中,向ES服务器发put请求(索引增加数据:指定生成ID): http://127.0.0.1:9200/shopping/_create/1001
body请求体中的内容如下:
{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":"3999.00"
}

在postman中,向ES服务器发get请求(按ID查询索引): http://127.0.0.1:9200/shopping/_doc/1001
在postman中,向ES服务器发get请求(按查询索引下的所有内容): http://127.0.0.1:9200/shopping/_search

在postman中,向ES服务器发put请求(全量修改索引内容): http://127.0.0.1:9200/shopping/_doc/1001
body请求体中的内容如下:
{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":"5999.00"
}
在postman中,向ES服务器发post请求(部分修改索引内容): http://127.0.0.1:9200/shopping/_update/1001
{
    "doc":{
        "title":"华为手机"
    }
}
在postman中,向ES服务器发delete请求(删除索引ID): http://127.0.0.1:9200/shopping/_doc/1001

-----------------------------------------------------------------------------------------------
在postman中,向ES服务器发get请求(按请求路径:查询品牌小米):http://127.0.0.1:9200/shopping/_search?q=category:小米
在postman中,向ES服务器发get请求(按请求body:查询品牌小米):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match":{
            "category":"小米"
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:查询该索引下全部):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_all":{
            
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:查询该索引下分页):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 5
}

在postman中,向ES服务器发get请求(按请求body:查询该索引下分页,指定显示项):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 5,
    "_source":["title"]
}

在postman中,向ES服务器发get请求(按请求body:查询该索引下分页,指定显示项,排序):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_all":{
            
        }
    },
    "from": 0,
    "size": 5,
    "_source":["title"],
    "sort":{
        "price":{
            "order":"desc"
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:组合查询:两者都要):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "category":"小米"
                    }
                },
                {
                    "match":{
                        "price":1999.00
                    }
                }
            ]
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:组合查询:满足其中一个):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "category":"小米"
                    }
                },
                {
                    "match":{
                        "category":"华为"
                    }
                }
            ]
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:组合查询:满足其中一个,范围查询):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "category":"小米"
                    }
                },
                {
                    "match":{
                        "category":"华为"
                    }
                }
            ],
            "filter":{
                "range":{
                    "price":{
                        "gt": 5000
                    }
                }
            }
        }
    }
}


在postman中,向ES服务器发get请求(按请求body:组合查询:满足其中一个,范围查询):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "category":"小米"
                    }
                },
                {
                    "match":{
                        "category":"华为"
                    }
                }
            ],
            "filter":{
                "range":{
                    "price":{
                        "gt": 5000
                    }
                }
            }
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:组合查询:完全匹配查询):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_phrase":{
            "category":"小华"
        }
    }
}


在postman中,向ES服务器发get请求(按请求body:组合查询:完全匹配查询,高亮显示字段):http://127.0.0.1:9200/shopping/_search
{
    "query":{
        "match_phrase":{
            "category":"小华"
        }
    },
    "highlight":{
        "fields":{
            "category": {}    
        }
    }
}

在postman中,向ES服务器发get请求(按请求body:聚合操作:分组):http://127.0.0.1:9200/shopping/_search
{
    "aggs":{//聚合操作
        "price_group":{//名称,随便起名
            "terms":{//分组
                "field": "price"//分组字段
            }
        }
    },
    "size":0 //不要显示原始数据
}

在postman中,向ES服务器发get请求(按请求body:聚合操作:平均):http://127.0.0.1:9200/shopping/_search
{
    "aggs":{//聚合操作
        "price_avg":{//名称,随便起名
            "avg":{//平均值
                "field": "price"//分组字段
            }
        }
    },
    "size":0 //不要显示原始数据
}
-----------------------------------------------------------------------------------------------
在postman中,向ES服务器发put请求(增加索引): http://127.0.0.1:9200/user
在postman中,向ES服务器发put请求(索引增加字段): http://127.0.0.1:9200/user/_mapping
body请求体中的内容如下:
{
    "properties":{
        "name":{
            "type": "text",//可以模糊查询
            "index": true
        },
        "sex":{
            "type": "keyword",//只能全部匹配查询
            "index": true
        },
        "tel":{
            "type": "keyword",
            "index": false  //不能被查询
        }
    }
}
在postman中,向ES服务器发put请求(索引增加数据): http://127.0.0.1:9200/user/_create/1001
body请求体中的内容如下:
{
    "name":"小米",
    "sex":"男的",
    "tel":"123456"
}

在postman中,向ES服务器发get请求(索引查询数据): http://127.0.0.1:9200/user/_search
body请求体中的内容如下:
{
    "query":{
        "match": {
            "name": "小"
        }
    }
}
{
    "query":{
        "match": {
            "sex": "男"//查不到
        }
    }
}

 

-----------------------------------------------------------------------------------------------
elasticsearch-7.8.1集群配置
修改config/elasticsearch.yml文件

cluster.name: my-application
node.name: node-1001
node.master: true
node.data: true
network.host: 192.168.0.102
http.port: 1001
transport.tcp.port: 9301
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

查看配置是否生效
http://localhost:1001/_cluster/health

第二个节点的配置
cluster.name: my-application
node.name: node-1002
node.master: true
node.data: true
network.host: 192.168.0.103
http.port: 1002
transport.tcp.port: 9302

discovery.seed_hosts: ["192.168.0.102:9301"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

第三个节点的配置
cluster.name: my-application
node.name: node-1003
node.master: true
node.data: true
network.host: 192.168.0.104
http.port: 1003
transport.tcp.port: 9303

discovery.seed_hosts: ["192.168.0.102:9301","192.168.0.103:9302"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

-----------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------
linux环境下单机运行elasticsearch-7.8.1
解压
 tar -zxvf elasticsearch-7.8.1-linux-x86_64.tar.gz
创建es用户
useradd es
设置密码
passwd es
文件夹所有者
chown -R es:es  /opt/apps/es

修改/opt/apps/es/config/elasticsearch.yml文件
#加入如下配置
cluster.name: elasticsearch
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

修改/etc/security/limits.conf
#在文件末尾中增加下面内容
#每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536

修改/etc/security/limits.d/20-nproc.conf
#在文件末尾中增加下面内容
#每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536

修改/etc/sysctl.conf
#在文件末尾中增加下面内容
#一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为65536
vm.max_map_count=655360

重新加载
sysctl -p

切换用户
su es

-----------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------
linux环境下集群运行elasticsearch-7.8.1
解压
 tar -zxvf elasticsearch-7.8.1-linux-x86_64.tar.gz
修改文件名    
mv elasticsearch-7.8.1 es-cluster
创建es用户
useradd es
设置密码
passwd es
文件夹所有者
chown -R es:es  /opt/apps/es-cluster

修改配置文件
修改/opt/apps/es/config/elasticsearch.yml文件
#加入如下配置
#集群名称
cluster.name: cluster-es
#节点名称,每个节点的名称不能重复
node.name: node-1
#ip地址,每个节点的地址不能重复
network.host: linux1
#是不是有资格主节点
node.master: true
node.data: true
http.port: 9200
#head 插件需要打开两个配置
http.cors.allow-origin: "*"
http.cors.enabled: true
http.max_map_length: 200mb
#es7.x之后新增的配置,初始化一个新的集群时需要此配置来选举master
cluster.initial_master_nodes: ["node-1"]
#es7.x之后新增的配置,节点发现
discovery.seed_hosts: ["linux1:9300","linux2:9300","linux3.9300"]
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是2个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认4个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认4个
cluster.routing.allocation.node_initial_primaries_recoveries: 16


修改/etc/security/limits.conf,分发文件
#在文件末尾中增加下面内容
#每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536


修改/etc/security/limits.d/20-nproc.conf,分发文件
#在文件末尾中增加下面内容
#每个进程可以打开的文件数的限制
es soft nofile 65536
es hard nofile 65536
* hard nproc 4096
#注: *带表linux所有用户名称


修改/etc/sysctl.conf
#在文件末尾中增加下面内容
#一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为65536
vm.max_map_count=655360

重新加载
sysctl -p

启动软件
分别在不同节点上启动ES软件
cd /opt/es/es-cluster
启动
bin/elasticsearch
后台启动
bin/elasticsearch -d

查看elasticsearch集群状态
http://linux1:9200/_cat/nodes
-----------------------------------------------------------------------------------------------

 

 

posted on 2025-10-28 10:49  yebinghuai-qq-com  阅读(2)  评论(0)    收藏  举报

导航