Exemple de requete en elasticsearch

Elasticsearch

Explications

Il y a 2 types de recherche :

Type de recherche Score Cache
Query Variable Non
Filter Fixe Oui

Exemple :

 1GET /_search
 2{
 3  "query": {
 4    "bool": {
 5      "must": {
 6        "match": {
 7          "message": "this is a test"
 8        }
 9      },
10      "filter": [
11        {
12          "term": {
13            "user": "kimchy"
14          }
15        },
16        {
17          "term": {
18            "user": "herald"
19          }
20        }
21      ],
22      "must_not": {
23        "term": {
24          "user": "cassie"
25        }
26      },
27      "should": {
28        "term": {
29          "user": "johnny"
30        }
31      }
32    }
33  },
34  "aggs": {
35    "user_terms": {
36      "terms": {
37        "field": "user"
38      }
39    }
40  }
41}

Query String

Exemple :

1GET /_search
2{
3  "query": {
4    "query_string": {
5      "query": "(new york city) OR (big apple)",
6      "default_field": "content"
7    }
8  }
9}

les AND et les OR doivent être en majuscule

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

Simple Query

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

Requetes

Url Signification
GET _cat Information generale
GET /_cat/indices Liste tous les indexes
GET /_cat/health L'etat du cluster Elasticsearch
GET / La version d'elasticsearch
GET _cat/aliases La liste des alias
GET //_mapping Le mapping de l'index
POST //_search recherche sur l'index
POST //_search recherche sur l'index
POST //_count comptage sur l'index

les operateurs de recherche

https://coralogix.com/blog/42-elasticsearch-query-examples-hands-on-tutorial/

Exemples

Exemple de requete en ElasticSearch :

 1POST /my-index-000001/_search
 2{
 3  "query": {
 4    "match": {
 5      "[user.id](http://user.id/)": "kimchy"
 6    }
 7  },
 8  "fields": [
 9    "[user.id](http://user.id/)",
10    "http.response.*",
11    {
12      "field": "@timestamp",
13      "format": "epoch_millis"
14    }
15  ],
16  "_source": false
17}

Exemple trouvé ici

 1GET /_search
 2{
 3  "query": {
 4    "simple_query_string" : {
 5        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
 6        "fields": ["title^5", "body"],
 7        "default_operator": "and"
 8    }
 9  }
10}
 1{
 2  "query": {
 3    "match": {
 4      "my_field": "meaning"
 5    }
 6  },
 7  "fields": [
 8    "name",
 9    "surname",
10    "age"
11  ],
12  "from": 100,
13  "size": 20
14}
1curl "localhost:9200/_search?q=name:john~1 AND (age:[30 TO 40} OR surname:K*) AND -city"
 1% curl 'localhost:9200/_cat/indices?format=json&pretty'
 2[
 3  {
 4    "pri.store.size": "650b",
 5    "health": "yellow",
 6    "status": "open",
 7    "index": "my-index-000001",
 8    "pri": "5",
 9    "rep": "1",
10    "docs.count": "0",
11    "docs.deleted": "0",
12    "store.size": "650b"
13  }
14]
 1curl -X PUT 'http://localhost:9200/students' -d '{
 2   "mappings": {
 3     "student": { 
 4       "properties": { 
 5         "name":     { "type": "keyword"  },
 6         "degree"    { "type": "keyword" },
 7         "age":      { "type": "integer" }  
 8         },
 9       "properties": { 
10         "performance": { "type": "keyword"  } 
11         }
12     }
13   }
14 }'
1GET /my-index-000001/_search
2{
3  "timeout": "2s",
4  "query": {
5    "match": {
6      "user.id": "kimchy"
7    }
8  }
9}
1GET /my-index-000001/_search
2{
3  "track_total_hits": true,
4  "query": {
5    "match" : {
6      "user.id" : "elkbee"
7    }
8  }
9}

Des sites avec d'autres exemples: