
Tengo a continuación registros similares.
He creado un muñeco index
y lo he creado mapping
como se muestra a continuación endev-tools
PUT new
{
"mappings": {
"properties": {
"@timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss.SSS"
}
}
}
}
y indexed
datos como se muestra a continuación,
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"
}
Puedo recuperar los recuentos totales utilizando la siguiente request
consulta,
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": []
}
}
}
Obteniendo response
lo siguiente,
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
Como puede ver, necesito hardcode
obtener date
el valor de un determinado month
, es decir, para obtener la misma información durante el sept
mes, necesito modificar lo date time range
siguiente en la solicitud curl,
"range": {
"@timestamp": {
"gte": "2021-09-01",
"lte": "2021-09-30"
}
}
Abajo esta el 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": []
}
}
}'
¿Cómo puedo pasar year
dinámicamente (es decir , month
sin codificar la solicitud) a la solicitud curl que recuperará la información para ese archivo en particular month
?year
actualizar -
Puedo obtener los resultados del último mes (noviembre) o de los últimos 2 meses (octubre), y así sucesivamente usando a continuación,
el mes pasado - Nov
-
"gte": "now-M",
"lt": "now/M"
2 meses -Oct
"gte": "now-2M/M",
"lte": "now-2M/M"
Pero, ¿hay alguna manera de proporcionar los resultados deseados year
y month
recuperarlos?
Gracias,
Respuesta1
Puedes usarmatemáticas de fechacon fechas totalmente definidas:
"range": {
"@timestamp": {
"gte": "2021-10-01",
"lte": "2021-10-01||+1M/d"
}
}