ES 增删改查

ES

  • 下载地址:https://www.elastic.co/cn/downloads/elasticsearch
  • 配置 elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: elasticsearch
# 最大分片数
cluster.max_shards_per_node: 5000
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /usr/local/elk/elasticsearch-7.12.1/data
#
# Path to log files:
#
path.logs: /usr/local/elk/elasticsearch-7.12.1/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"
  • 创建用户启动 ES(root 用户不能启动)
useradd es
# 授权
chown -R user-es:user-es /usr/local/elasticsearch-7.6.2
# 切换用户
su es
# 启动
./elasticsearch
  • 如果报错:Exception in thread “main” java.nio.file.AccessDeniedException
chown leyou /tmp -R

node.js

  • 下载地址:https://nodejs.org/en/(下载左边的稳定版)
  • 安装
tar -xJf node-v10.16.3-linux-x64.tar.xz 
# 配置环境变量
vi /etc/profile
# 加两行
export NODE_HOME=/usr/local/node-v10.16.3-linux-x64
export PATH=$NODE_HOME/bin:$PATH
# 重新加载
source /etc/profile
  • 安装完成后,用 node -v 可以看到版本号
    version
  • 添加环境变量:npm config set prefix “安装目录\node_global”、npm config set prefix “安装目录\node_cache”
    环境变量
  • 用户变量 Path 添加:安装目录\node_global、系统变量 NODE_PATH:安装目录\node_modules
  • 安装 express:npm install express -g、npm install -g express-generator
    express

Elasticsearch-head

  • 下载地址:https://github.com/mobz/elasticsearch-head
  • 安装:npm install
    安装
  • 启动:npm run start
    启动

创建文档

  • 用 Postman 发送 PUT 请求到 ES:/索引/类型/ID
    PUT
  • 在 Elasticsearch-head 中查看数据
    查看数据

修改数据

  • 发送 POST 请求到 ES:/索引/类型/ID/_update
    POST
  • 在 Elasticsearch-head 中查看数据
    查看数据

查询数据

  • 用 Postman 发送 GET 请求到 ES:/索引/类型/ID
    GET

条件查询

  • 用 Postman 发送 GET 请求到 ES:/索引/类型/_search?q=name:小明
    条件查询
  • 匹配度越高,_score 越高

构建查询(/索引/类型/_search)

  • 条件查询
{
    "query": {
        "match": {
            "name": "小二"
        }
    }
}

构建查询

  • 查询全部
{
    "query": {
        "match_all": { }
    }
}

查询全部

  • 指定查询的内容
{
    "query": {
        "match_all": { }
    }, 
    "_source": [
        "name"
    ]
}

指定查询内容

  • 排序
{
    "query": {
        "match_all": { }
    }, 
    "sort": {
        "age": {
            "order": "desc"
        }
    }
}

排序

  • 分页
{
    "query": {
        "match_all": { }
    }, 
    "from": 0, 
    "size": 1
}

分页

  • must_not:查询 age != 3 的数据
{
    "query": {
        "bool": {
            "must_not": [
                {
                    "match": {
                        "age": 3
                    }
                }
            ]
        }
    }
}

must_not

  • filter:gt(>)、gte(≥)、lt(<)、lte(≤)
{
    "query": {
        "bool": {
            "filter": [
                {
                    "range": {
                        "age": {
                            "gt": 10
                        }
                    }
                }
            ]
        }
    }
}

filter

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值