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 可以看到版本号
- 添加环境变量: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
Elasticsearch-head
- 下载地址:https://github.com/mobz/elasticsearch-head
- 安装:npm install
- 启动:npm run start
创建文档
- 用 Postman 发送 PUT 请求到 ES:/索引/类型/ID
- 在 Elasticsearch-head 中查看数据
修改数据
- 发送 POST 请求到 ES:/索引/类型/ID/_update
- 在 Elasticsearch-head 中查看数据
查询数据
- 用 Postman 发送 GET 请求到 ES:/索引/类型/ID
条件查询
- 用 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
}
}
]
}
}
}
- filter:gt(>)、gte(≥)、lt(<)、lte(≤)
{
"query": {
"bool": {
"filter": [
{
"range": {
"age": {
"gt": 10
}
}
}
]
}
}
}