Exemple de requete en elasticsearch

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 :

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

Query String

Exemple :

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

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 :

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

Exemple trouvé ici

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

Des sites avec d’autres exemples:

Généré avec Hugo
Thème Stack conçu par Jimmy