Linux安装Elasticsearch配置及注意事项
Linux安装Elasticsearch配置及注意事项
1、下载Linux版的Elasticsearch压缩包
自行去官网下载对应的Elasticsearch版本(https://www.elastic.co/cn/downloads/elasticsearch#preview-release)
另外JDK环境可自行安装。
2、上传包并解压
tar -zxvf elasticsearch-7.15.2-linux-x86_64.tar.gz
1
3、进入解压目录
cd elasticsearch-7.15.2
1
目录结构:
drwxr-xr-x. 2 elastic elastic 4096 Nov 4 22:08 bin
drwxr-xr-x. 3 elastic elastic 199 Dec 2 15:34 config
drwxr-xr-x. 9 elastic elastic 121 Nov 4 22:08 jdk
drwxr-xr-x. 3 elastic elastic 4096 Nov 4 22:08 lib
-rw-r--r--. 1 elastic elastic 3860 Nov 4 22:02 LICENSE.txt
drwxr-xr-x. 2 elastic elastic 30 Dec 2 15:08 logs
drwxr-xr-x. 60 elastic elastic 4096 Nov 4 22:08 modules
-rw-r--r--. 1 elastic elastic 628969 Nov 4 22:06 NOTICE.txt
drwxr-xr-x. 2 elastic elastic 6 Nov 4 22:06 plugins
-rw-r--r--. 1 elastic elastic 2710 Nov 4 22:02 README.asciidoc
1
2
3
4
5
6
7
8
9
10
目录介绍:
bin: 二进制脚本文件包括elasticsearch启动节点、elasticsearch-plugin安装插件
config: 配置文件目录
jdk: java环境
lib: 依赖的jar,类库
logs: 日志文件
modules: es相关模块
plugins: 插件
4、创建elasticsearch用户并授权
#创建elastic用户
useradd elastic
#授权 【/home/elasticsearch】为es存放目录
chown -R elastic:elastic /home/elasticsearch
1
2
3
4
5、调制系统参数
5.1、修改最大文件数和锁内存限制
vi /etc/security/limits.conf
1
在末尾处增加:
elastic hard nproc unlimited
elastic soft nproc unlimited
elastic hard nofile 262144
elastic soft nofile 262144
elastic memlock unlimited
elastic fsize unlimited
elastic as unlimited
1
2
3
4
5
6
7
elastic:代码启动Elasticsearch集群的用户
5.2、更改一个进程能拥有的最大内存区域限制
vi /etc/sysctl.conf
1
新增或编辑以下内容:
vm.max_map_count=262144
vm.swappiness=1
1
2
保存后执行 sysctl -p
5.3、修改用户最大线程数
vi /etc/security/limits.d/20-nproc.conf
1
注意:centos7是20-nproc.conf,而 centos6是90-nproc.conf
新增或修改以下内容:
* soft nproc unlimited
root soft nproc unlimited
elastic hard nofile 262144
elastic soft nofile 262144
1
2
3
4
6、 elasticsearch配置
进入config目录
cd config/
[elastic@localhost config]$ ll
total 40
-rw-rw----. 1 elastic elastic 199 Dec 2 15:08 elasticsearch.keystore
-rw-rw----. 1 elastic elastic 2732 Dec 2 15:34 elasticsearch.yml
-rw-rw----. 1 elastic elastic 3117 Dec 2 15:08 jvm.options
drwxr-x---. 2 elastic elastic 6 Nov 4 22:06 jvm.options.d
-rw-rw----. 1 elastic elastic 19093 Nov 4 22:06 log4j2.properties
-rw-rw----. 1 elastic elastic 473 Nov 4 22:06 role_mapping.yml
-rw-rw----. 1 elastic elastic 197 Nov 4 22:06 roles.yml
-rw-rw----. 1 elastic elastic 0 Nov 4 22:06 users
-rw-rw----. 1 elastic elastic 0 Nov 4 22:06 users_roles
1
2
3
4
5
6
7
8
9
10
11
12
其中核心的配置文件有:
elasticsearch.yml是用于配置Elasticsearch的最主要的配置文件。
jvm.options用于配置Elasticsearch JVM设置。
log4j2.properties用于配置Elasticsearch日志记录的属性
6.1 配置elasticsearch.yml
#集群名称
cluster.name: my-application
#节点名称
node.name: node-1
#数据目录(提前创建好)
path.data: /home/elasticsearch/data
#日志目录(提前创建好)
path.logs: /home/elasticsearch/logs
#主机
network.host: 0.0.0.0
#端口
http.port: 9200
#主节点选举列表
cluster.initial_master_nodes: ["node-1"]
#跨域配置 是否支持跨域,默认为false
http.cors.enabled: true
#当设置允许跨域,默认为*,表示支持所有域名
http.cors.allow-origin: "*"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
6.2 配置jvm.options
该配置占物理内存一半,32G最佳。
-Xms1g
-Xmx1g
1
2
后续章节介绍配置详解,快速创建忽略6.3 、6.4、6.5、6.6、6.7
6.3 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: 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: /home/elasticsearch/data
#
# Path to log files:
# 日志目录
path.logs: /home/elasticsearch/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
# 锁定物理内存地址,防止elasticsearch内存被交换出去,也就是避免es使用swap交换分区
#bootstrap.memory_lock: true
# 确保ES_HEAP_SIZE参数设置为系统可用内存的一半左右
# 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.
# 当系统进行内存交换的时候,es的性能很差
# 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:
# 网络主机 host 为 0.0.0.0 ,即可启用该物理机器所有网卡网络访问 (设置后,es将从开发模式变成生成模式)
network.host: 0.0.0.0
#
# 为es设置自定义端口,默认是9200
# 注意:在同一个服务器中启动多个es节点的话,默认监听的端口号会自动加1:例如:9200,9201,9202
# 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]"]
# 当与其他主机上的节点组成集群,需要用discover.seed_hosts提供集群其他节点列表
#discovery.seed_hosts: ["node-1"]
#
# 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 -----------------------------------
# 设置是否可以通过正则或者_all删除或者关闭索引库,默认true表示必须需要显式指定索引库名称
# 生产环境建议设置为true,删除索引库的时候必须显式指定,否则可能会误删索引库中的索引库。
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
#跨域配置 是否支持跨域,默认为false
http.cors.enabled: true
#当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。比如只允许本地地#址。 /https?:\/\/localhost(:[0-9]+)?/
http.cors.allow-origin: "*"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
6.4 log4j2.properties详解
status = error
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
######## Server JSON ############################
#配置 RollingFile appender
appender.rolling.type = RollingFile
appender.rolling.name = rolling
#输出目录日志
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.json
#使用json输出
appender.rolling.layout.type = ESJsonLayout
#用来解析日志时更容易地区分不同类型的日志
appender.rolling.layout.type_name = server
#日志压缩
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.json.gz
appender.rolling.policies.type = Policies
#使用基于时间的回滚策略
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#每天回滚一个日志文件
appender.rolling.policies.time.interval = 1
#在日边界上对齐(而不是每隔24小时滚动一次)
appender.rolling.policies.time.modulate = true
#使用基于大小的回滚策略
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
#大于256MB时进行回滚
appender.rolling.policies.size.size = 128MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
#当回滚完成时,删除当前日志文件
appender.rolling.strategy.action.type = Delete
#设置日志路径
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
#处理滚动时应用的条件
appender.rolling.strategy.action.condition.type = IfFileName
#只删除匹配对应模式的文件
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
#使用只删除主日志文件的模式
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
#大于2GB时进行压缩
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB
##以下两行为保留日志7天配置
#appender.rolling.strategy.action.condition.nested_condition.type=IfLastModified
#appender.rolling.strategy.action.condition.nested_condition.age=7D
################################################
######## Server - old style pattern ###########
appender.rolling_old.type = RollingFile
appender.rolling_old.name = rolling_old
appender.rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
appender.rolling_old.layout.type = PatternLayout
appender.rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
appender.rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
appender.rolling_old.policies.type = Policies
appender.rolling_old.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling_old.policies.time.interval = 1
appender.rolling_old.policies.time.modulate = true
appender.rolling_old.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling_old.policies.size.size = 128MB
appender.rolling_old.strategy.type = DefaultRolloverStrategy
appender.rolling_old.strategy.fileIndex = nomax
appender.rolling_old.strategy.action.type = Delete
appender.rolling_old.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling_old.strategy.action.condition.type = IfFileName
appender.rolling_old.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
appender.rolling_old.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
appender.rolling_old.strategy.action.condition.nested_condition.exceeds = 2GB
################################################
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.rolling.ref = rolling
rootLogger.appenderRef.rolling_old.ref = rolling_old
######## Deprecation JSON #######################
appender.deprecation_rolling.type = RollingFile
appender.deprecation_rolling.name = deprecation_rolling
appender.deprecation_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_deprecation.json
appender.deprecation_rolling.layout.type = ESJsonLayout
appender.deprecation_rolling.layout.type_name = deprecation.elasticsearch
appender.deprecation_rolling.layout.esmessagefields=x-opaque-id,key
appender.deprecation_rolling.filter.rate_limit.type = RateLimitingFilter
appender.deprecation_rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_deprecation-%i.json.gz
appender.deprecation_rolling.policies.type = Policies
appender.deprecation_rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.deprecation_rolling.policies.size.size = 1GB
appender.deprecation_rolling.strategy.type = DefaultRolloverStrategy
appender.deprecation_rolling.strategy.max = 4
appender.header_warning.type = HeaderWarningAppender
appender.header_warning.name = header_warning
#################################################
######## Deprecation - old style pattern #######
appender.deprecation_rolling_old.type = RollingFile
appender.deprecation_rolling_old.name = deprecation_rolling_old
appender.deprecation_rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_deprecation.log
appender.deprecation_rolling_old.layout.type = PatternLayout
appender.deprecation_rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
appender.deprecation_rolling_old.filter.rate_limit.type = RateLimitingFilter
appender.deprecation_rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_deprecation-%i.log.gz
appender.deprecation_rolling_old.policies.type = Policies
appender.deprecation_rolling_old.policies.size.type = SizeBasedTriggeringPolicy
appender.deprecation_rolling_old.policies.size.size = 1GB
appender.deprecation_rolling_old.strategy.type = DefaultRolloverStrategy
appender.deprecation_rolling_old.strategy.max = 4
#################################################
logger.deprecation.name = org.elasticsearch.deprecation
logger.deprecation.level = deprecation
logger.deprecation.appenderRef.deprecation_rolling.ref = deprecation_rolling
logger.deprecation.appenderRef.deprecation_rolling_old.ref = deprecation_rolling_old
logger.deprecation.appenderRef.header_warning.ref = header_warning
logger.deprecation.additivity = false
######## Search slowlog JSON ####################
appender.index_search_slowlog_rolling.type = RollingFile
appender.index_search_slowlog_rolling.name = index_search_slowlog_rolling
appender.index_search_slowlog_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs\
.cluster_name}_index_search_slowlog.json
appender.index_search_slowlog_rolling.layout.type = ESJsonLayout
appender.index_search_slowlog_rolling.layout.type_name = index_search_slowlog
appender.index_search_slowlog_rolling.layout.esmessagefields=message,took,took_millis,total_hits,types,stats,search_type,total_shards,source,id
appender.index_search_slowlog_rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs\
.cluster_name}_index_search_slowlog-%i.json.gz
appender.index_search_slowlog_rolling.policies.type = Policies
appender.index_search_slowlog_rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.index_search_slowlog_rolling.policies.size.size = 1GB
appender.index_search_slowlog_rolling.strategy.type = DefaultRolloverStrategy
appender.index_search_slowlog_rolling.strategy.max = 4
#################################################
######## Search slowlog - old style pattern ####
appender.index_search_slowlog_rolling_old.type = RollingFile
appender.index_search_slowlog_rolling_old.name = index_search_slowlog_rolling_old
appender.index_search_slowlog_rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_search_slowlog.log
appender.index_search_slowlog_rolling_old.layout.type = PatternLayout
appender.index_search_slowlog_rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
appender.index_search_slowlog_rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_search_slowlog-%i.log.gz
appender.index_search_slowlog_rolling_old.policies.type = Policies
appender.index_search_slowlog_rolling_old.policies.size.type = SizeBasedTriggeringPolicy
appender.index_search_slowlog_rolling_old.policies.size.size = 1GB
appender.index_search_slowlog_rolling_old.strategy.type = DefaultRolloverStrategy
appender.index_search_slowlog_rolling_old.strategy.max = 4
#################################################
logger.index_search_slowlog_rolling.name = index.search.slowlog
logger.index_search_slowlog_rolling.level = trace
logger.index_search_slowlog_rolling.appenderRef.index_search_slowlog_rolling.ref = index_search_slowlog_rolling
logger.index_search_slowlog_rolling.appenderRef.index_search_slowlog_rolling_old.ref = index_search_slowlog_rolling_old
logger.index_search_slowlog_rolling.additivity = false
######## Indexing slowlog JSON ##################
appender.index_indexing_slowlog_rolling.type = RollingFile
appender.index_indexing_slowlog_rolling.name = index_indexing_slowlog_rolling
appender.index_indexing_slowlog_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_indexing_slowlog.json
appender.index_indexing_slowlog_rolling.layout.type = ESJsonLayout
appender.index_indexing_slowlog_rolling.layout.type_name = index_indexing_slowlog
appender.index_indexing_slowlog_rolling.layout.esmessagefields=message,took,took_millis,doc_type,id,routing,source
appender.index_indexing_slowlog_rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_indexing_slowlog-%i.json.gz
appender.index_indexing_slowlog_rolling.policies.type = Policies
appender.index_indexing_slowlog_rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.index_indexing_slowlog_rolling.policies.size.size = 1GB
appender.index_indexing_slowlog_rolling.strategy.type = DefaultRolloverStrategy
appender.index_indexing_slowlog_rolling.strategy.max = 4
#################################################
######## Indexing slowlog - old style pattern ##
appender.index_indexing_slowlog_rolling_old.type = RollingFile
appender.index_indexing_slowlog_rolling_old.name = index_indexing_slowlog_rolling_old
appender.index_indexing_slowlog_rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_indexing_slowlog.log
appender.index_indexing_slowlog_rolling_old.layout.type = PatternLayout
appender.index_indexing_slowlog_rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
appender.index_indexing_slowlog_rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}\
_index_indexing_slowlog-%i.log.gz
appender.index_indexing_slowlog_rolling_old.policies.type = Policies
appender.index_indexing_slowlog_rolling_old.policies.size.type = SizeBasedTriggeringPolicy
appender.index_indexing_slowlog_rolling_old.policies.size.size = 1GB
appender.index_indexing_slowlog_rolling_old.strategy.type = DefaultRolloverStrategy
appender.index_indexing_slowlog_rolling_old.strategy.max = 4
#################################################
logger.index_indexing_slowlog.name = index.indexing.slowlog.index
logger.index_indexing_slowlog.level = trace
logger.index_indexing_slowlog.appenderRef.index_indexing_slowlog_rolling.ref = index_indexing_slowlog_rolling
logger.index_indexing_slowlog.appenderRef.index_indexing_slowlog_rolling_old.ref = index_indexing_slowlog_rolling_old
logger.index_indexing_slowlog.additivity = false
appender.audit_rolling.type = RollingFile
appender.audit_rolling.name = audit_rolling
appender.audit_rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_audit.json
appender.audit_rolling.layout.type = PatternLayout
appender.audit_rolling.layout.pattern = {\
"type":"audit", \
"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZ}"\
%varsNotEmpty{, "node.name":"%enc{%map{node.name}}{JSON}"}\
%varsNotEmpty{, "node.id":"%enc{%map{node.id}}{JSON}"}\
%varsNotEmpty{, "host.name":"%enc{%map{host.name}}{JSON}"}\
%varsNotEmpty{, "host.ip":"%enc{%map{host.ip}}{JSON}"}\
%varsNotEmpty{, "event.type":"%enc{%map{event.type}}{JSON}"}\
%varsNotEmpty{, "event.action":"%enc{%map{event.action}}{JSON}"}\
%varsNotEmpty{, "authentication.type":"%enc{%map{authentication.type}}{JSON}"}\
%varsNotEmpty{, "user.name":"%enc{%map{user.name}}{JSON}"}\
%varsNotEmpty{, "user.run_by.name":"%enc{%map{user.run_by.name}}{JSON}"}\
%varsNotEmpty{, "user.run_as.name":"%enc{%map{user.run_as.name}}{JSON}"}\
%varsNotEmpty{, "user.realm":"%enc{%map{user.realm}}{JSON}"}\
%varsNotEmpty{, "user.run_by.realm":"%enc{%map{user.run_by.realm}}{JSON}"}\
%varsNotEmpty{, "user.run_as.realm":"%enc{%map{user.run_as.realm}}{JSON}"}\
%varsNotEmpty{, "user.roles":%map{user.roles}}\
%varsNotEmpty{, "apikey.id":"%enc{%map{apikey.id}}{JSON}"}\
%varsNotEmpty{, "apikey.name":"%enc{%map{apikey.name}}{JSON}"}\
%varsNotEmpty{, "authentication.token.name":"%enc{%map{authentication.token.name}}{JSON}"}\
%varsNotEmpty{, "authentication.token.type":"%enc{%map{authentication.token.type}}{JSON}"}\
%varsNotEmpty{, "origin.type":"%enc{%map{origin.type}}{JSON}"}\
%varsNotEmpty{, "origin.address":"%enc{%map{origin.address}}{JSON}"}\
%varsNotEmpty{, "realm":"%enc{%map{realm}}{JSON}"}\
%varsNotEmpty{, "url.path":"%enc{%map{url.path}}{JSON}"}\
%varsNotEmpty{, "url.query":"%enc{%map{url.query}}{JSON}"}\
%varsNotEmpty{, "request.method":"%enc{%map{request.method}}{JSON}"}\
%varsNotEmpty{, "request.body":"%enc{%map{request.body}}{JSON}"}\
%varsNotEmpty{, "request.id":"%enc{%map{request.id}}{JSON}"}\
%varsNotEmpty{, "action":"%enc{%map{action}}{JSON}"}\
%varsNotEmpty{, "request.name":"%enc{%map{request.name}}{JSON}"}\
%varsNotEmpty{, "indices":%map{indices}}\
%varsNotEmpty{, "opaque_id":"%enc{%map{opaque_id}}{JSON}"}\
%varsNotEmpty{, "x_forwarded_for":"%enc{%map{x_forwarded_for}}{JSON}"}\
%varsNotEmpty{, "transport.profile":"%enc{%map{transport.profile}}{JSON}"}\
%varsNotEmpty{, "rule":"%enc{%map{rule}}{JSON}"}\
%varsNotEmpty{, "put":%map{put}}\
%varsNotEmpty{, "delete":%map{delete}}\
%varsNotEmpty{, "change":%map{change}}\
%varsNotEmpty{, "create":%map{create}}\
%varsNotEmpty{, "invalidate":%map{invalidate}}\
}%n
# "node.name" node name from the `elasticsearch.yml` settings
# "node.id" node id which should not change between cluster restarts
# "host.name" unresolved hostname of the local node
# "host.ip" the local bound ip (i.e. the ip listening for connections)
# "origin.type" a received REST request is translated into one or more transport requests. This indicates which processing layer generated the event "rest" or "transport" (internal)
# "event.action" the name of the audited event, eg. "authentication_failed", "access_granted", "run_as_granted", etc.
# "authentication.type" one of "realm", "api_key", "token", "anonymous" or "internal"
# "user.name" the subject name as authenticated by a realm
# "user.run_by.name" the original authenticated subject name that is impersonating another one.
# "user.run_as.name" if this "event.action" is of a run_as type, this is the subject name to be impersonated as.
# "user.realm" the name of the realm that authenticated "user.name"
# "user.run_by.realm" the realm name of the impersonating subject ("user.run_by.name")
# "user.run_as.realm" if this "event.action" is of a run_as type, this is the realm name the impersonated user is looked up from
# "user.roles" the roles array of the user; these are the roles that are granting privileges
# "apikey.id" this field is present if and only if the "authentication.type" is "api_key"
# "apikey.name" this field is present if and only if the "authentication.type" is "api_key"
# "authentication.token.name" this field is present if and only if the authenticating credential is a service account token
# "authentication.token.type" this field is present if and only if the authenticating credential is a service account token
# "event.type" informs about what internal system generated the event; possible values are "rest", "transport", "ip_filter" and "security_config_change"
# "origin.address" the remote address and port of the first network hop, i.e. a REST proxy or another cluster node
# "realm" name of a realm that has generated an "authentication_failed" or an "authentication_successful"; the subject is not yet authenticated
# "url.path" the URI component between the port and the query string; it is percent (URL) encoded
# "url.query" the URI component after the path and before the fragment; it is percent (URL) encoded
# "request.method" the method of the HTTP request, i.e. one of GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH, TRACE, CONNECT
# "request.body" the content of the request body entity, JSON escaped
# "request.id" a synthetic identifier for the incoming request, this is unique per incoming request, and consistent across all audit events generated by that request
# "action" an action is the most granular operation that is authorized and this identifies it in a namespaced way (internal)
# "request.name" if the event is in connection to a transport message this is the name of the request class, similar to how rest requests are identified by the url path (internal)
# "indices" the array of indices that the "action" is acting upon
# "opaque_id" opaque value conveyed by the "X-Opaque-Id" request header
# "x_forwarded_for" the addresses from the "X-Forwarded-For" request header, as a verbatim string value (not an array)
# "transport.profile" name of the transport profile in case this is a "connection_granted" or "connection_denied" event
# "rule" name of the applied rule if the "origin.type" is "ip_filter"
# the "put", "delete", "change", "create", "invalidate" fields are only present
# when the "event.type" is "security_config_change" and contain the security config change (as an object) taking effect
appender.audit_rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_audit-%d{yyyy-MM-dd}.json
appender.audit_rolling.policies.type = Policies
appender.audit_rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.audit_rolling.policies.time.interval = 1
appender.audit_rolling.policies.time.modulate = true
logger.xpack_security_audit_logfile.name = org.elasticsearch.xpack.security.audit.logfile.LoggingAuditTrail
logger.xpack_security_audit_logfile.level = info
logger.xpack_security_audit_logfile.appenderRef.audit_rolling.ref = audit_rolling
logger.xpack_security_audit_logfile.additivity = false
logger.xmlsig.name = org.apache.xml.security.signature.XMLSignature
logger.xmlsig.level = error
logger.samlxml_decrypt.name = org.opensaml.xmlsec.encryption.support.Decrypter
logger.samlxml_decrypt.level = fatal
logger.saml2_decrypt.name = org.opensaml.saml.saml2.encryption.Decrypter
logger.saml2_decrypt.level = fatal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
6.5 配置日志级别
通过elasticsearch.yml文件
:
例如:
logger.org.elasticsearch.transport: trace
1
当临时调试一个问题,希望在更持久的基础上调整日志级别时,这很适合。
通过集群更新Api设置
PUT /_cluster/settings
{
"transient":{
"<name of logging hierarchy>":"<level>"
}
}
1
2
3
4
5
6
7
8
例如:
PUT /_cluster/settings
{
"transient":{
"logger.org.elasticsearch.transport": "trace"
}
}
1
2
3
4
5
6
7
8
当需要动态地调整活动运行的集群上的日志级别时,这很适合。
通过log4j2.properties
配置如下:
logger.<unique_identifier>.name=<name of logging hierarchy>
logger.<unique_identifier>.level=<level>
1
2
例如:
logger.transport.name=org.elasticsearch.transport
logger.transport.level=trace
1
2
6.6 重要的elasticsearch一些配置
6.6.1 数据和日志存放目录
如果安装文件使用的是.zip或.tar.gz结构的文件,那么data和logs目录在${es_home}的子文件夹中,如果将这些重要文件夹保留在其默认位置,则当将Elasticsearch升级到新版本时,这些文件夹被删除的风险很高。
#自定义数据目录(提前创建好)
path.data: /home/elasticsearch/data
#自定义日志目录(提前创建好)
path.logs: /home/elasticsearch/logs
1
2
3
4
6.6.2 集群名称
节点只能在与集群中的所有其他节点共享cluster.name时加入集群。默认集群名称是【my-application】,但是应该将其改成合适名称。
#集群名称
cluster.name: my-application
1
2
注意:集群名称是组建集群的唯一标识。请确保不要在不同环境中重用相同的集群名称,否则最终可能会导致节点加入错误的集群。
6.6.3 节点名称
Elasticsearch使用node.name作为特点Elasticsearch实例的可读标识符,包含在很多Api的响应中,它默认为启动Elasticsearch时计算机拥有的主机名。
#节点名称
node.name: node-1
1
2
6.6.4 网络设置
默认情况下,ELasticsearch仅绑定到环回地址,例如127.0.0.1,这足以在服务器上运行单个开发节点。
但是为了与其他服务器上的节点组成集群,节点需要绑定到非环回地址,虽然有许多网络设置,但通常只需配置network.host:
# 网络主机 host 为 0.0.0.0 ,即可启用该物理机器所有网卡网络访问 (设置后,es将从开发模式变成生成模式)
network.host: 0.0.0.0
1
2
注意:一旦为network.host设置了自定义设置,Elasticsearch将假定从开发模式切换到生产模式,并将许多系统启动检查从【警告】升到到【异常】。
6.6.5 重要节点发现和集群初始化设置
1、discovery.seed_hosts
在没有任何网络配置的情况下,Elasticsearch将绑定到可用的环回地址,并扫描本地的端口9300到9305,以尝试连接到同一服务器上运行的其他节点,这提供了一种自动组建集群的功能,而无须进行任何配置。
而discovery.seed_hosts通过设置提供集群中其他节点的列表,与其他主机上的可通信的节点进行组成集群。
cluster.initial_master_nodes: ["192.168.1.10:9300","192.168.1.11","seeds.mydomain.com","[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9300"]
1
其中四种设置规则:
【192.168.1.10:9300】:直接指定主机ip和端口;
【192.168.1.11】: 指定了主机ip,使用默认的端口。默认端口的设置由transport.profiles.default.port和transport.port端口设置,前者优先级高于后者;
【seeds.mydomain.com】: 指定了主机hostname,需要使用dns解析成ip地址,并使用默认端口;
【[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9300】: ip v6地址形式
2、cluster.initial_master_nodes
当第一次启动一个全新的Elasticsearch集群时,有一个集群引导过程,在开发模式下,在没有配置发现设置的情况下,该节点本身将自动选举自身为主节点,这种自动引导具有不安全性,生产环境不推荐使用。而在生产模式下,需要启动一个全新的集群时,必须明确列出合格主节点的名称或者IP地址,而cluster.initial_master_nodes则是设置合格的节点或IP地址。
cluster.initial_master_nodes:准备让哪些节点参与主节点的选举。
cluster.initial_master_nodes: ["node-1","192.168.1.11","192.168.1.10:9300"]
1
其中设置规则:
【192.168.1.10:9300】:直接指定主机ip和端口;
【192.168.1.11】: 指定了主机ip,使用默认的端口。默认端口的设置由transport.profiles.default.port和transport.port端口设置,前者优先级高于后者;
【node-1】: 节点名称
6.6.6 Heap设置
默认情况下,Elasticsearch告诉JVM使用最小和最大大小为1 GB的堆。 迁移到生产环境时,配置堆大小以确保Elasticsearch有足够的可用堆是很重要的。
Elasticsearch将通过Xms(最小堆大小)和Xmx(最大堆大小)设置分配jvm.options中指定的整个堆。
设置建议:
1、将最小堆大小(Xms)和最大堆大小(Xmx)设置为彼此相等。
2、Elasticsearch可用的堆越多,它可用于缓存的内存就越多。 但请注意,过多的堆可能会使您陷入长时间的垃圾收集暂停。
3、将Xmx设置为不超过物理RAM的50%,以确保有足够的物理RAM留给内核文件系统缓存。
4、不要将Xmx设置为JVM用于压缩对象指针(压缩oops)的截止值以上; 确切的截止值变化但接近32 GB。
6.6.7 JVM Jeap dump目录设置
默认情况下,Elasticsearch将Jvm内存不足异常上的堆转储到默认数据目录,即data目录。如果此路径不适合接收堆转储,则应该修改 jvm.options中的配置
# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data
1
2
3
如果指定目录,则JVM将根据正在运行的实例的PID为堆转储生成一个文件。如果指定的是文件而不是目录,则当JVM需要对内存不足异常执行堆转储时,该文件不能不存在,否则堆转储将失败。
6.6.8 GC日志设置
默认情况下,Elasticsearch启用GC日志。它们在jvm.options中配置,并默认为与Elasticsearch日志相同的默认位置。默认位置为每64MB回滚一次日志,最多可占用2GB的磁盘空间。
## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m
1
2
3
4
5
6
7
8
9
6.6.9 临时文件存储目录
默认情况下,Elasticsearch使用启动脚本在系统临时目录下创建的专用临时目录。
在某些Linux发行版上,系统实用程序将清除文件和目录(/tmp如果它们最近未被访问过)。如果长时间不使用需要临时目录的功能,则可能导致在Elasticsearch运行时删除专用临时目录。如果随后使用需要临时目录的功能,则会导致问题。
如果使用.deb或.rpm包安装Elasticsearch 并在其下运行,systemd那么Elasticsearch使用的专用临时目录将从定期清理中排除。
但是,如果您打算.tar.gz在Linux 上运行分发一段时间,那么您应该考虑为Elasticsearch创建一个专用的临时目录,该目录不在将从中清除旧文件和目录的路径下。此目录应具有权限集,以便只有运行Elasticsearch的用户才能访问它。然后$ES_TMPDIR在启动Elasticsearch之前将环境变量设置 为指向它。
## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}
1
2
也可以在jvm.options文件中直接配置绝对路径。
6.6.10 JVM致命错误日志设置
默认情况下,Elasticsearch将JVM配置为将致命错误日志写入默认日志记录目录(这/var/log/elasticsearch适用于RPM和Debian软件包发行版,以及logs 针对tar和zip归档文件分发的Elasticsearch安装根目录下的目录 )。这些是JVM在遇到致命错误(例如,分段错误)时生成的日志。如果该路径不适合于接收的日志,则应修改条目-XX:ErrorFile=…中 jvm.options到备用路径。
# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log
1
2
6.7 重要的系统参数一些设置
6.7.1 开发模式与生产模式
默认情况下,Elasticsearch假定您正在开发模式下工作。如果未正确配置上述任何设置,则会向日志文件写入警告,但您将能够启动并运行Elasticsearch节点。
一旦配置了类似的网络设置network.host,Elasticsearch就会假定您正在转向生产并将上述警告升级为异常。这些异常将阻止您的Elasticsearch节点启动。这是一项重要的安全措施,可确保您不会因服务器配置错误而丢失数据。
6.7.2 配置系统设置limit.conf
1、临时设置
直接使用ulimit命令进行设置
如:将文件打开数设置为65535
ulimit 65535
1
2、修改/etc/security/limit.conf文件(永久模式)
如:为elastic用户特定的系统参数属性
elastic hard nproc unlimited
elastic soft nproc unlimited
elastic hard nofile 262144
elastic soft nofile 262144
elastic memlock unlimited
elastic fsize unlimited
elastic as unlimited
1
2
3
4
5
6
7
6.7.3 禁用交换区
大多数操作系统尝试尽可能多地将内存用于文件系统缓存,因此应用程序的数据可能会被移到swap区,这可能导致部分JVM堆,甚至其可执行页面被交换到磁盘。
上述交换对性能、节点稳定性都非常不利,它可能会导致垃圾收集持续几分钟而不是几毫秒,并可能导致节点响应缓慢,甚至断开与集群的连接。因此,应不惜一切代价予以避免。在弹性分布式系统中,让操作系统杀死节点比启用交换区更有效。
禁用交换区有三种方式:
1、完全禁用交换区
临时禁用可通过以下命令
swapoff -a
1
永久禁用可注释掉在/etc/fstab中的包含swap的任何行
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Nov 26 21:50:46 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=70b8f2c6-cac6-4a6d-aa77-78766edb2dd5 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap
1
2
3
4
5
6
7
8
9
10
11
12
该方式可能会导致OOM,慎用。
2、最小化使用交换区
Linux系统上可用的另一个方式是确保sysctl值vm.swappiness设置为1.这降低了内核交换的可能性,在正常情况下不会导致交换,同时仍然允许整个系统在紧急情况下交换。
vi /etc/sysctl.conf
1
新增或编辑以下内容:
vm.max_map_count=262144
vm.swappiness=1
1
2
保存后执行 sysctl -p
3、内存锁定
该方式是尝试将进程地址空间锁定到RAM中,防止任何Elasticsearch内存被交换出去.
通过修改elasticsearch.yml中的bootstrap.memory_lock
bootstrap.memory_lock: true
1
注意:防止运行Elasticsearch的用户没有锁定内存的权限。建议在启动Elasticsearch之前,以root权限在/etc/security/limit.conf中将memlock设置为unlimited。
6.7.4 文件描述符
Elasticsearch使用许多文件描述符或文件句柄。文件描述符用尽可能是灾难性的,很可能导致数据丢失。确保将运行Elasticsearch的用户的打开文件描述符的数量限制增加到65,536或更高。
vi /etc/security/limits.conf
1
在末尾处增加:
elastic hard nofile 262144
elastic soft nofile 262144
1
2
其中elastic:代码启动Elasticsearch集群的用户
也可用API检查为每个节点配置的max_file_描述符,方法是:
GET _nodes/stats/process?filter_path=**.max_file_descriptors
1
6.7.5 虚拟内存
默认情况下,Elasticsearch使用mmapfs目录存储其索引。mmap计数的默认操作系统限制可能太低,这可能会导致内存不足异常。
在Linux上,可以通过以根root用户身份运行以下命令来增加限制:
vi /etc/sysctl.conf
1
新增或编辑以下内容:
vm.max_map_count=262144
vm.swappiness=1
1
2
保存后执行 sysctl -p
6.7.6 线程数量限制
Elasticsearch为不同类型的操作使用许多线程池。重要的是,它能够在需要时创建新线程。确保Elasticsearch用户可以创建的线程数至少为4096。
这可以通过在启动Elasticsearch之前将ulimit -u 4096设置为root来完成,也可以通过在/etc/security/limits.conf中将nproc设置为4096来完成。
vi /etc/security/limits.conf
1
在末尾处增加:
elastic hard nproc unlimited
elastic soft nproc unlimited
1
2
elastic:代码启动Elasticsearch集群的用户
6.7.6 DNS缓存设置
Elasticsearch运行时有一个安全管理器。有了安全管理器,JVM默认无限期地缓存正向主机名解析,默认情况下缓存负向主机名解析10秒。Elasticsearch使用默认值覆盖此行为,以缓存正向查找60秒,缓存负向查找10秒。这些值应该适用于大多数环境,包括DNS分辨率随时间变化的环境。否则,可以在JVM options中编辑值es.networkaddress.cache.ttl和es.networkaddress.cache.negative.ttl。请注意,除非删除networkaddress.cache.ttl= 和networkaddress.cache.negative.ttl=的设置,否则Elasticsearch将忽略Java安全策略中的es.networkaddress.cache.ttl 和es.networkaddress.cache.negative.ttl值。
默认情况下:DNS缓存的默认设置已经满足绝大数情况,无需修改。
6.7.7 JNA临时目录挂载位置
Elasticsearch使用Java Native Access(JNA)库执行一些依赖于平台的本机代码。在Linux上,支持这个库的本机代码在运行时从JNA存档中提取。默认情况下,此代码提取到Elasticsearch临时目录,该目录默认为/tmp的子目录。或者,可以使用JVM标志-Djna.tmpdir= 来控制这个位置。由于本机库作为可执行文件映射到JVM虚拟地址空间,因此此代码提取到的位置的底层装入点不能使用noexec装入,因为这会阻止JVM进程将此代码映射为可执行文件。在某些强化的Linux安装中,/tmp是默认装载选项。使用noexec挂载底层挂载的一个迹象是,在启动时,JNA将无法使用 java.lang.UnsatisfiedLinkerError异常进行加载,并显示一条消息,“ failed to map segment from shared object”。注意,异常消息在不同的JVM版本中可能有所不同。此外,Elasticsearch中依赖于通过JNA执行本机代码的组件将失败,并显示消息, because JNA is not available。如果您看到这样的错误消息,则必须重新装载用于JNA的临时目录,以使其不与noexec一起装载。
7、启动elasticsearch
1、直接启动elasticsearch,窗口关闭即关闭服务
./elasticsearch
1
2、后台运行elasticsearch
./elasticsearch -d
1
8、开发端口
firewall-cmd --add-port=9200/tcp --permanent
firewall-cmd --reload
1
2
9、测试效果
在浏览器中输入
http://ip:9200
1
页面展示结果:
{
"name" : "node-1",
"cluster_name" : "my-application",
"cluster_uuid" : "BG1X1nxpQGa9l6lqoDPlpg",
"version" : {
"number" : "7.15.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "93d5a7f6192e8a1a12e154a2b81bf6fa7309da0c",
"build_date" : "2021-11-04T14:04:42.515624022Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10、IK中文分词器
10.1 IK中文分词器简介
IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始,IKAnalyzer已经推出了3个大版本。最初,它是以开源项目Lucene为应用主体的,结合词典分词和文法分析算法的中文分词组件。新版本的IKAnalyzer3.0则发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。
IK分词器3.0的特性如下:
采用了特有的“正向迭代最细粒度切分算法“,具有60万字/秒的高速处理能力。
采用了多子处理器分析模式,支持:英文字母(IP地址、Email、URL)、数字(日期,常用中文数量词,罗马数字,科学计数法),中文词汇(姓名、地名处理)等分词处理。
对中英联合支持不是很好,在这方面的处理比较麻烦.需再做一次查询,同时是支持个人词条的优化的词典存储,更小的内存占用。
支持用户词典扩展定义。
针对Lucene全文检索优化的查询分析器IKQueryParser;采用歧义分析算法优化查询关键字的搜索排列组合,能极大的提高Lucene检索的命中率。
10.2 安装IK中文分词器
10.2.1 下载IK中文分词器
可在官网https://github.com/medcl/elasticsearch-analysis-ik/releases 自行下载对应版本的IK中文分词器
10.2.2 解压elasticsearch-analysis-ik-7.15.2.zip
在ES中plugins中创建ik目录
cd plugins
mkdir ik
cd ik
unzip elasticsearch-analysis-ik-7.15.2.zip
1
2
3
4
如果unzip命令没有可通过【yum -y install unzip zip】命令进行安装
10.2.3 将解压的目录移到ES中plugins中的ik目录
查看elasticsearch的plugins
[root@localhost elasticsearch-7.15.2]# cd plugins/ik/
[root@localhost ik]# ll
total 1432
-rw-r--r--. 1 elastic elastic 263965 Apr 25 2021 commons-codec-1.9.jar
-rw-r--r--. 1 elastic elastic 61829 Apr 25 2021 commons-logging-1.2.jar
drwxr-xr-x. 2 elastic elastic 4096 Apr 25 2021 config
-rw-r--r--. 1 elastic elastic 54634 Nov 15 16:44 elasticsearch-analysis-ik-7.15.2.jar
-rw-r--r--. 1 elastic elastic 736658 Apr 25 2021 httpclient-4.5.2.jar
-rw-r--r--. 1 elastic elastic 326724 Apr 25 2021 httpcore-4.4.4.jar
-rw-r--r--. 1 elastic elastic 1807 Nov 15 16:44 plugin-descriptor.properties
-rw-r--r--. 1 elastic elastic 125 Nov 15 16:44 plugin-security.policy
1
2
3
4
5
6
7
8
9
10
11
10.2.4 重启elasticsearch
./elasticsearch -d
1
通过启动日志可看到已加载了IK中文分词器
[2021-12-03T11:49:45,198][INFO ][o.e.p.PluginsService ] [node-1] loaded plugin [analysis-ik]
1
10.3 测试IK在中文分词器
IK提供了两个分词算法ik_smart 和ik_max_word。其中ik_smart 为最少切分,ik_max_word为最细粒度划分
10.3.1 最小切分(ik_smart)
http://localhost:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员
1
结果:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "程序员",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
10.3.2 最细粒度划分(ik_max_word)
http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员
1
结果为:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "程序员",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
},
{
"token": "程序",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "员",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 4
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
10.4 自定义词库
1、在elasticsearch的plugins文件夹下的ik文件夹下的config文件夹中
cd /home/elasticsearch/elasticsearch-7.15.2/plugins/ik/config
1
2、创建mydiv.dic文件输入单词并以utf-8格式编码 (mydiv :自定义的名称)
[root@localhost config]# touch mydiv.dic
[root@localhost config]# ll
total 8260
-rw-r--r--. 1 elastic elastic 5225922 Apr 25 2021 extra_main.dic
-rw-r--r--. 1 elastic elastic 63188 Apr 25 2021 extra_single_word.dic
-rw-r--r--. 1 elastic elastic 63188 Apr 25 2021 extra_single_word_full.dic
-rw-r--r--. 1 elastic elastic 10855 Apr 25 2021 extra_single_word_low_freq.dic
-rw-r--r--. 1 elastic elastic 156 Apr 25 2021 extra_stopword.dic
-rw-r--r--. 1 elastic elastic 625 Apr 25 2021 IKAnalyzer.cfg.xml
-rw-r--r--. 1 elastic elastic 3058510 Apr 25 2021 main.dic
-rw-r--r--. 1 root root 0 Dec 7 16:27 mydiv.dic
-rw-r--r--. 1 elastic elastic 123 Apr 25 2021 preposition.dic
-rw-r--r--. 1 elastic elastic 1824 Apr 25 2021 quantifier.dic
-rw-r--r--. 1 elastic elastic 164 Apr 25 2021 stopword.dic
-rw-r--r--. 1 elastic elastic 192 Apr 25 2021 suffix.dic
-rw-r--r--. 1 elastic elastic 752 Apr 25 2021 surname.dic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3、在IKAnalyzer.cfg.xml文件中配置:
[root@localhost config]# cat IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
增加以下配置:
<entry key="ext_dict">xxx.dic</entry>
浙公网安备 33010602011771号