elastic search记录

安装与启动

插件安装
中文分词器 https://github.com/medcl/elasticsearch-analysis-ik

elastic api

GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_cat/indices


## 查询所有索引中的字段映射
GET _mapping

## 查询index中的type, 所有文档
GET logstash-2019.11.10-000001/_search


## 根据id查询单个详情,  上面结果中返回的type=_doc
GET logstash-2019.11.10-000001/_doc/VL1cU24BpEn5ymJfSBRH?pretty


## 根据message字段全匹配查询, 提供type (已过期)
GET logstash-2019.11.10-000001/_doc/_search?q=message:ffffff

## 根据message字段模糊查询, 提供type (已过期)
GET logstash-2019.11.10-000001/_doc/_search?q=message:*f

## 根据message字段全匹配查询, 不提供type
POST logstash-2019.11.10-000001/_search 
{"query":{"match":{"message":"ffffff"}}}

## 根据message字段模糊查询, 使用的是wildcard
POST logstash-2019.11.10-000001/_search 
{"query":{"wildcard":{"message":"ff*"}}}



## 查询之后计算权重, 在查询表达式中定义函数function_score
# https://www.elastic.co/guide/cn/elasticsearch/guide/current/function-score-query.html
## todo 

使用kibana查询

bin/kibana
http://localhost:5601
页面和阿里云的日志中心非常相似

中文页面配置

kibana-7.4.2-linux-x86_64configkibana.yml最后一行

# Specifies locale to be used for all localizable strings, dates and number formats.
# Supported languages are the following: English - en , by default , Chinese - zh-CN .
i18n.locale: "zh-CN"

运维

集群中节点数 设置成奇数,防止脑裂

开启数据压缩

spring data elasticsearch

https://spring.io/projects/spring-data-elasticsearch#learn

https://docs.spring.io/spring-data/elasticsearch/docs/3.2.1.RELEASE/reference/html/#elasticsearch.query-methods.criterions

根据BookRepository 方法名直接生成es查询api

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>

spring-web默认没加上,启动报错
jpa接口比较重,spring cloud中可能有很多冲突问题, 使用原生的接口开发也许更快,

索引重建

java使用索引的别名查询, ES别名可以引用新的索引, java应用可以不重启

问题列表

磁盘空间不足时 不能重建索引
java api : upsert的操作 doc_as_update 设置为true,默认是false, update不成功可能是这个原因

Filebeat

window版本坑太多了,运行不起来
linux版本 filebeat.yml文件权限必须是rwxr-xr-x, 777的权限反而运行不起来, 简直了wsl chmod如何生效

./filebeat --strict.perms=false 

修改filebeat.yml enabled=true, 默认竟然是false, 运行半天没收集到日志,突然看到这个坑


配置多行日志合并, 比如java exception stack log.

filebeat原理
监控日志文件的inode, 文件写入或者切片时,inode会变化,老的inode数据读完后 再到新的inode读数据,这样数据就不会丢失。

ES文档:

API ==> https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.3/java-rest-high-query-builders.html
book ==> https://www.elastic.co/guide/cn/elasticsearch/guide/current/icu-tokenizer.html

深入相似度算法 https://www.elastic.co/guide/cn/elasticsearch/guide/current/decay-functions.html#decay-functions

原文地址:https://www.cnblogs.com/yszzu/p/11830402.html