Elasticsearch - Indexation et recherches simples

Mais où manger ce soir ?

Le but de cet exercice est de manipuler un type plus complet, les geo_points. Vous avez précédemment chargé une liste de 500 restaurant de New-York, votre bt est de trouver quel restaurant correspondra le plus a vos critères : un restaurant Japonais (Japanese) du quartier Manhattan le plus proche de chez vous.

Le nom de l'index est boutiques, le nom du type est restaurant. Bonne chance !

Question

Combien y a t il de restaurant japonais dans votre liste ?

Indice

Vous pouvez consulter le mapping pour savoir sur quel champ requêter

1
GET http://localhost:9200/boutiques/restaurant/_mapping 
Solution

Il y en a 12

1
POST boutiques/restaurant/_search
2
{
3
  "query": {"match": {"cuisine":"Japanese"}  }
4
}

Question

Combien y en a t il dans le quartier Manhattan ?

Indice

Il faut combiner deux critères avec une requête bool

1
 "query": {
2
      "bool": {
3
        "must": [
4
          { "match": { "<field>": <value> }},
5
          { "match": { "<field>": <value> }}
6
        ]
7
      }
8
  }
Solution

Il y en a 10

1
POST http://localhost:9200/boutiques/restaurant/_search
2
{
3
    "query": {
4
      "bool": {
5
        "must": [
6
          { "match": { "borough": "Manhattan" }},
7
          { "match": { "cuisine": "japanese" }}
8
        ]
9
      }
10
  }
11
}

Question

Quel est le plus proche ? Vous habitez à l'adresse suivante : [-73.87, 40.65]

Indice

Vous pouvez trier par distance par rapport à un point donné, en indiquant le champ de comparaison

1
POST http://localhost:9200/boutiques/restaurant/_search
2
{
3
    "sort" : [
4
        {
5
            "_geo_distance" : {
6
                "address.coord" : [-73.87, 40.65],
7
                "order" : "asc",
8
                "unit" : "km",
9
                "mode" : "min"
10
            }
11
        }
12
    ]
13
}
Solution

Il s'agit du Hasaki Restaurant

1
POST http://localhost:9200/boutiques/restaurant/_search
2
{
3
    "sort" : [
4
        {
5
            "_geo_distance" : {
6
                "address.coord" : [-73.87, 40.65],
7
                "order" : "asc",
8
                "unit" : "km",
9
                "mode" : "min"
10
            }
11
        }
12
    ],
13
    "query": {
14
      "bool": {
15
        "must": [
16
          { "match": { "borough": "Manhattan" }},
17
          { "match": { "cuisine": "japanese" }}
18
        ]
19
      }
20
  },
21
  "_source": ["borough","cuisine", "name" ],
22
  "size": 1
23
}
PrécédentPrécédentFin
AccueilAccueilImprimerImprimer Robin Jolliet, 2016 (Contributions : Stéphane Crozat, les étudiants de l'UTC) Paternité - Partage des Conditions Initiales à l'IdentiqueRéalisé avec Scenari (nouvelle fenêtre)