一、 linux安装elasticsearch,以下简称ES
1.1、到官网下载ES,建议下载7.X.X及以上版本
https://elasticsearch.cn/download/
1.2 在根目录创建一个文件夹存放ES
将下载的安装包放在elastic中
~ cd /
~ mkdir elastic
1.3 解压es包,这里以7.6.1为例
tar -zxvf elasticsearch-7.6.1-linux-x86_64.tar.gz
1.4 修改配置文件
vim elasticsearch-7.6.1/config/elasticsearch.yml
# 释放以下3个
network.host: 192.168.103.10
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
# 添加以下3个配置
http.cors.enabled: true
http.cors.allow-origin: "*"
bootstrap.system_call_filter: false
注:以下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: my-application
#
# ------------------------------------ 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: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/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 -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.103.10
#
# Set a custom port for HTTP:
#
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
bootstrap.system_call_filter: false
discovery.type: single-node
# xpack.security.enabled: true
xpack.security.enabled: true
# xpack.security.transport.ssl.enabled: true
xpack.security.authc.accept_default_password: true
#
# 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.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
1.5 创建新用户,因为es不能用root用户启动
# 创建用户
useradd elasticsearchuser
# 设置用户密码 wqs123456@
passwd elasticsearchuser
# 给新用户添加刚才自己创建的目录权限
chown -R elasticsearchuser:elasticsearchuser /elastic
# 切换用户
su elasticsearchuser
1.6 启动
cd /elastic/elasticsearch-7.6.1/bin
# 1直接启动
./elasticsearch
# 2后台启动
./elasticsearch -d
# 注意:如果遇到内存不足,百度设置一下,重新启动即可
1.7 设置访问密码(如果想裸奔,下一步就没有必要了)
1.7.1 修改配置文件
vi elasticsearch-7.6.1/config/elasticsearch.yml
# 添加以下几项配置
# 这里是单机启动,使用这个的话,就要将cluster.initial_master_nodes: ["node-1"]注释掉
discovery.type: single-node
# xpack.security.enabled: true
xpack.security.enabled: true
# xpack.security.transport.ssl.enabled: true
xpack.security.authc.accept_default_password: true
1.7.2 重启es
ps -ef | grep elastic
# 杀死进程后重新后台启动
./elasticsearch -d
1.7.3 配置密码,成功后重启即可(遇到问题可以自行百度)
注意:设置密码过程中可能会遇到一些问题,可参考:https://blog.csdn.net/tergou/article/details/131263893进行解决
# 根据提示,输入各个用户的密码
./bin/elasticsearch-setup-passwords interactive
# 也可以让es,生成随机密码
./bin/elasticsearch-setup-passwords auto
配置成功案例
elasticsearchuser@vm:/elastic/elasticsearch-7.6.1/bin$ ./elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Passwords do not match.
Try again.
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
二、ES使用过程中遇到的问题
2.1 查询时hits.hits.total数据最大只能显示10000的问题
查询时加入"track_total_hits":true
即可显示真实数据
// es查询的写法
GET patent_info/_search
{
"from":15000,
"size":10,
"track_total_hits":true
}
// java中的写法
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.trackTotalHits(true);
2.2 查询超过一万行以后的数据报错
设置最大显示行max_result_window的数量即可,设置多少,超过了此条数依然会报错
# 错误报错
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
// 解决方法,es中运行一遍即可
PUT patent_info/_settings
{
"max_result_window" :100000000
}