1. Déclarer ses index #
Modèle et mapping.
Un modèle d'index #
Déclarez les types avant la première écriture : le type d'un champ existant ne se change pas sans réindexer.
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X PUT https://localhost:9200/_index_template/logs_app -d '{
"index_patterns": ["logs-app-*"],
"template": {
"settings": {"number_of_shards": 1, "number_of_replicas": 0},
"mappings": {"properties": {
"@timestamp": {"type": "date"},
"level": {"type": "keyword"},
"service": {"type": "keyword"},
"message": {"type": "text"},
"duree_ms": {"type": "integer"}
}}
}
}'| Type | Pour | Recherche |
|---|---|---|
keyword | Valeur exacte : niveau, service | Filtre, agrégation, tri |
text | Texte libre : message | Plein texte, analysé |
date, integer, ip | Valeurs typées | Plages, agrégations, CIDR |
2. Indexer #
L'API bulk.
Écrire par lots #
cat > lot.ndjson <<'EOF'
{"index":{"_index":"logs-app-000001"}}
{"@timestamp":"2026-11-04T10:00:00Z","level":"ERROR","service":"paiement","message":"timeout vers la banque utilisateur=alice","duree_ms":3100}
{"index":{"_index":"logs-app-000001"}}
{"@timestamp":"2026-11-04T10:00:01Z","level":"INFO","service":"paiement","message":"paiement accepté","duree_ms":120}
{"index":{"_index":"logs-app-000001"}}
{"@timestamp":"2026-11-04T10:00:02Z","level":"INFO","service":"panier","message":"article ajouté","duree_ms":40}
EOF
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/x-ndjson' \
-X POST 'https://localhost:9200/_bulk?refresh=true' --data-binary @lot.ndjsonBonne pratique : utilisez toujours_bulkpour de gros volumes, et lisez"errors"dans la réponse : le code HTTP reste 200 même si des documents sont refusés. Évitezrefresh=trueen production ; il sert ici à lire aussitôt le résultat.
3. Chercher #
Query DSL, agrégations, PPL et SQL.
La recherche #
Dans un bool, must compte dans le score, tandis que filter dit seulement oui ou non, et se met en cache. Mettez dans filter tout ce qui n'a pas besoin de pertinence.
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST 'https://localhost:9200/logs-app-*/_search' -d '{
"query": {"bool": {
"must": [{"match": {"message": "banque"}}],
"filter": [{"term": {"service": "paiement"}}]
}}
}'Les agrégations #
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST 'https://localhost:9200/logs-app-*/_search' -d '{
"size": 0,
"aggs": {"par_service": {
"terms": {"field": "service"},
"aggs": {"p95": {"percentiles": {"field": "duree_ms", "percents": [95]}}}
}}
}'Résultat de notre essai : paiement a 2 documents (p95 de 3100 ms) et panier 1 (p95 de 40 ms).
PPL et SQL #
Pour explorer des logs, le PPL enchaîne les opérations avec des |, à la manière d'un shell.
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST https://localhost:9200/_plugins/_ppl \
-d '{"query":"source=logs-app-* | where service=\"paiement\" | stats count() by level"}'
curl -sk -u 'admin:Str0ng!Passw0rd-2026' -H 'Content-Type: application/json' -X POST https://localhost:9200/_plugins/_sql \
-d '{"query":"SELECT service, COUNT(*) n FROM logs-app-000001 GROUP BY service"}'