
Ich habe unten ähnliche Protokolle.
Ich habe Dummy erstellt index
und mapping
wie unten indev-tools
PUT new
{
"mappings": {
"properties": {
"@timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
}
}
}
}
und indexed
Daten wie unten,
PUT /new/_doc/1
{
"@timestamp": "2021-11-05 08:12:14.534",
"level": "INFO",
"id": "1",
"text": "website is accessed",
"status": "clicked"
}
PUT /new/_doc/2
{
"@timestamp": "2021-10-14 09:11:14.534",
"level": "INFO",
"id": "3",
"text": "website is accessed",
"status": "clicked"
}
PUT /new/_doc/3
{
"@timestamp": "2021-09-09 02:08:20.534",
"level": "INFO",
"id": "4",
"text": "website is accessed",
"status": "clicked"
}
Ich kann die Gesamtzahlen mit der folgenden request
Abfrage abrufen:
GET new/_search
{
"aggs": {},
"size": 0,
"fields": [],
"query": {
"bool": {
"must": [],
"filter": [
{
"bool": {
"should": [
{
"match_phrase": {
"text": "website is accessed"
}
}
],
"minimum_should_match": 1
}
},
{
"range": {
"@timestamp": {
"gte": "2021-10-01",
"lte": "2021-10-30"
}
}
}
],
"should": [],
"must_not": []
}
}
}
So erhalten Sie response
es:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
Wie Sie sehen, muss ich hardcode
den date
Wert für einen bestimmten Wert abrufen, month
d. h. um die gleichen Informationen für einen Monat abzurufen sept
, muss ich die date time range
folgende Curl-Anfrage ändern:
"range": {
"@timestamp": {
"gte": "2021-09-01",
"lte": "2021-09-30"
}
}
Unten ist die curl call request
.
curl -u elastic:xxx -XGET "http://10.10.10.10:9200/new/_search?pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {},
"size": 0,
"fields": [],
"query": {
"bool": {
"must": [],
"filter": [
{
"bool": {
"should": [
{
"match_phrase": {
"text": "website is accessed"
}
}
],
"minimum_should_match": 1
}
},
{
"range": {
"@timestamp": {
"gte": "2021-10-01",
"lte": "2021-10-30"
}
}
}
],
"should": [],
"must_not": []
}
}
}'
Wie kann ich dynamisch (also ohne die Anfrage selbst fest zu codieren) an die Curl-Anfrage übergeben year
, die month
die Informationen für dieses bestimmte Element abruft month
?year
aktualisieren -
Ich kann die Ergebnisse für den letzten Monat (November) oder die letzten beiden Monate (Oktober) usw. abrufen, indem ich Folgendes verwende:
Im vergangenen Monat - Nov
-
"gte": "now-M",
"lt": "now/M"
2 Monate -Oct
"gte": "now-2M/M",
"lte": "now-2M/M"
Aber gibt es eine Möglichkeit, das Gewünschte bereitzustellen year
und month
die Ergebnisse abzurufen?
Danke,
Antwort1
Sie könnenDatum Mathematikmit vollständig definierten Terminen:
"range": {
"@timestamp": {
"gte": "2021-10-01",
"lte": "2021-10-01||+1M/d"
}
}