Back to Nightingale

Elasticsearch Log Queries

aiagent/skill/embedded/builtin/query-datasource/datasources/elasticsearch.md

9.1.13.3 KB
Original Source

Elasticsearch Log Queries

  • plugin_type: elasticsearch
  • Query language: Elasticsearch DSL / Lucene
  • Use case: Log queries

Get Index List

POST /api/n9e/indices
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "elasticsearch",
  "datasource_id": 1
}

Also available through the proxy:

GET /api/n9e/proxy/<datasource_id>/_cat/indices?format=json&s=index
Authorization: Bearer <token>

Get Index Fields

POST /api/n9e/fields
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "elasticsearch",
  "datasource_id": 1,
  "index": "logs-*"
}

Get Field Values (for filtering)

POST /api/n9e/es-variable
Authorization: Bearer <token>
Content-Type: application/json
json
{
  "cate": "elasticsearch",
  "datasource_id": 1,
  "index": "logs-*",
  "query": {
    "find": "terms",
    "field": "service",
    "query": ""
  }
}

Search Logs (execute _msearch through the proxy)

POST /api/n9e/proxy/<datasource_id>/_msearch
Authorization: Bearer <token>
Content-Type: application/x-ndjson

The request body is in NDJSON format (one JSON object per line, with header and body alternating):

{"search_type":"query_then_fetch","ignore_unavailable":true,"index":"logs-*"}
{"size":50,"query":{"bool":{"filter":[{"range":{"@timestamp":{"gte":"2024-04-01T00:00:00.000Z","lte":"2024-04-02T00:00:00.000Z","format":"strict_date_optional_time"}}},{"query_string":{"query":"level:ERROR AND service:api"}}]}},"sort":[{"@timestamp":{"order":"desc"}}]}

Request body field descriptions:

FieldDescription
sizeNumber of documents to return
query.bool.filterArray of filter conditions
rangeTime-range filter; the field name is usually @timestamp
query_string.querySearch using Lucene query syntax
sortSorting, usually in descending time order

Response format:

json
{
  "responses": [
    {
      "hits": {
        "total": {"value": 1234},
        "hits": [
          {
            "_source": {
              "@timestamp": "2024-04-01T12:00:00.000Z",
              "level": "ERROR",
              "service": "api",
              "message": "Connection timeout"
            }
          }
        ]
      }
    }
  ]
}

Aggregation Query (log count trend statistics)

{"search_type":"query_then_fetch","ignore_unavailable":true,"index":"logs-*"}
{"size":0,"query":{"bool":{"filter":[{"range":{"@timestamp":{"gte":"now-1h","lte":"now"}}}]}},"aggs":{"date_histogram":{"date_histogram":{"field":"@timestamp","fixed_interval":"1m"},"aggs":{"count":{"value_count":{"field":"_index"}}}}}}

Common Lucene Query Syntax

RequirementQuery
Exact matchlevel:ERROR
AND combinationlevel:ERROR AND service:api
OR combinationlevel:ERROR OR level:WARN
Wildcardmessage:timeout*
Rangestatus:[400 TO 599]
ExcludeNOT level:DEBUG
Phrase matchmessage:"connection refused"

Considerations

  • NDJSON format: The _msearch request body is in NDJSON format, one JSON per line, with header and body alternating, and a trailing newline is required
  • Time field: Usually @timestamp, in ISO 8601 format
  • Index pattern: Wildcards are supported, e.g. logs-*, logs-2024.04.*