下载链接ES:
https://www.elastic.co/cn/downloads/elasticsearch
立即访问最最重要的一点:
Starting in version 7.14, Beats central management has been removed. If you’re currently using Beats central management, we recommend that you start using Fleet instead. For more information, refer to the Fleet documentation.
注意
Documents sent to a data stream must have a @timestamp field 发送给数据流的文档必须具有@timestamp字段
POST logs-my_app-default/_doc
{
"@timestamp":"2021-10-20T14:13:10.000Z",
"event":{
"original":"192.0.2.42 --[10/Sep/2021:14:13 +0000] " GET /images/bg.jpg HTTP/1.0" 200 24736 "
}
}
Use the _bulk 进行新增 Each line must end in a newline character (\n), including the last line. - {"create":{}}
PUT logs-my_app-default/_bulk
{"create":{}}
{ "@timestamp":"2021-09-20T14:24:10.000Z","event":{ "original": "192.0.2.242 - - [07/May/2020:16:24:32 -0500] "GET /images/hm_nbg.jpg HTTP/1.0" 304 0" } }
{"create":{}}
{ "@timestamp": "2099-05-08T16:25:42.000Z", "event": { "original": "192.0.2.255 - - [08/May/2099:16:25:42+0000] "GET /favicon.ico HTTP/1.0" 200 3638" } }
GET logs-my_app-default/_search
{
"query": {
"match_all": { }
},
"sort": [
{
"@timestamp": "desc"
}
]
}
通过fileds指定字段
GET logs-my_app-default/_search
{
"query": {
"match_all": {}
},
"fields": [
"@timestamp"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
GET logs-my_app-default/_search
{
"query": {
"range": {
"@timestamp": {
"gte": "2021-05-05",
"lte": "2099-05-08"
}
}
},
"fields": [
"@timestamp"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
通过使用 now/d 的方式
GET logs-my_app-default/_search
{
"query": {
"range": {
"@timestamp": {
"gte": "now-1d/d",
"lte": "now/d"
}
}
},
"fields": [
"@timestamp"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
您可以在搜索期间从非结构化内容中提取来自非结构化内容的运行时字段,例如日志消息例如:获取从非结构化的内容中获取ip
"script": """ String sourceip=grok('%{IPORHOST:sourceip}.*').extract(doc["event.original"].value).sourceip; if(sourceip != null) emit(sourceip) """
GET logs-my_app-default/_search
{
"runtime_mappings": {
"source.ip": {
"type": "ip",
"script": """
String sourceip=grok('%{IPORHOST:sourceip}.*').extract(doc["event.original"].value)?.sourceip;
if(sourceip != null) emit(sourceip)
"""
}
},
"query": {
"range": {
"@timestamp": {
"gte": "now",
"lte": "2099-05-08"
}
}
},
"fields": [
"@timestamp",
"source.ip"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
使用bool 进行组合查询
GET logs-my_app-default/_search
{
"runtime_mappings": {
"source.ip": {
"type": "ip",
"script": """
String sourceip = grok('%{IPORHOST:sourceip}.*').extract(doc[ "event.original" ].value)?.sourceip;
if (sourceip != null) emit(sourceip);
"""
}
},
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "now",
"lte": "2099-05-08"
}
}
},
{
"range": {
"source.ip": {
"gte": "192.0.2.0",
"lte": "192.0.2.240"
}
}
}
]
}
},
"fields": [
"@timestamp",
"source.ip"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
注意:
The aggregation only runs on documents that match the query 聚合仅在与查询匹配的文档上运行
下面使用聚合来计算运行时的http.response.body.bytes字段的 average_response_siz
GET logs-my_app-default/_search
{
"runtime_mappings": {
"http.response.body.bytes": {
"type": "long",
"script": """
String bytes = grok('%{COMMONAPACHELOG}').extract(doc[ "event.original" ].value)?.bytes;
if (bytes != null) emit(Integer.parseInt(bytes));
"""
}
},
"aggs": {
"average_response_size": {
"avg": {
"field": "http.response.body.bytes"
}
}
},
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"gte": "now",
"lte": "2099-05-09"
}
}
}
]
}
},
"fields": [
"@timestamp",
"http.response.bytes"
],
"_source": false,
"sort": [
{
"@timestamp":"desc"
}
]
}
结果:

DELETE _data_stream/logs-my_app-default
更多的search操作
Common search options
ES的Java API官方文档
官网地址:Elasticsearch
ES Java API地址:ES Java API


