Pengenalan Singkat Elasticsearch

Muhammad Arslan 23 Desember 2016

Pengenalan Singkat Elasticsearch

Elasticsearch adalah salah satu database yang masuk ke dunia NoSQL dengan fokus di search engine database. Elasticsearch ditenagai oleh Apache Lucene yang juga merupakan search engine database yang memiliki query low level. Elasticsearch memiliki query yang lebih mudah untuk digunakan karena berbasis RESTful.

Elasticsearh memiliki konsep yang cukup unik. Dimana kita dapat mengasumsikan indeks sebagai "database", types sebagai "tabel" dan dokumen sebagai record atau row. Sedangkan mapping dapat diasumsikan sebagai "skema tabel". Di Elasticsearch tidak ada transaction dan dapat membuat struktur indeks tergantung dengan kebutuhan kita. Selain itu dapat diatur untuk menjadi sebuah sistem terdistribusi terhadap sejumlah server.

Lebih lengkapnya kamu dapat melihat penjelasan "Elasticsearch as NoSQL" untuk mengetahui kenapa Elasticsearch diklaim sebagai NoSQL. Sekarang mari kita fokus mencoba Elasticsearch terlebih dahulu.

1. Instalasi

Silahkan unduh Elastichsearch dari tautan https://www.elastic.co/downloads/elasticsearch. Kemudian lakukan perintah berikut di dalam terminal:
$ tar -xzvf elasticsearch-5.0.0.tar.gz
$ mv elasticsearch-5.0.0 elasticsearch
$ mv elasticsearch /opt
$ /opt/elasticsearch/bin/elasticsearch
[2016-12-18T16:05:45,265][INFO ][o.e.n.Node               ] [] initializing ...
[2016-12-18T16:05:45,377][INFO ][o.e.e.NodeEnvironment    ] [dXvLN44] using [1] data paths, mounts [[/ (/dev/sda2)]], net usable_space [58.7gb], net total_space [103.6gb], spins? [no], types [ext4]
[2016-12-18T16:05:45,378][INFO ][o.e.e.NodeEnvironment    ] [dXvLN44] heap size [1.9gb], compressed ordinary object pointers [true]
[2016-12-18T16:05:45,433][INFO ][o.e.n.Node               ] [dXvLN44] node name [dXvLN44] derived from node ID; set [node.name] to override
[2016-12-18T16:05:45,438][INFO ][o.e.n.Node               ] [dXvLN44] version[5.0.0], pid[11683], build[253032b/2016-10-26T04:37:51.531Z], OS[Linux/4.4.0-21-generic/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/1.8.0_111/25.111-b14]
[2016-12-18T16:05:46,785][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [aggs-matrix-stats]
[2016-12-18T16:05:46,786][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [ingest-common]
[2016-12-18T16:05:46,786][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [lang-expression]
[2016-12-18T16:05:46,786][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [lang-groovy]
[2016-12-18T16:05:46,787][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [lang-mustache]
[2016-12-18T16:05:46,787][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [lang-painless]
[2016-12-18T16:05:46,787][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [percolator]
[2016-12-18T16:05:46,788][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [reindex]
[2016-12-18T16:05:46,788][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [transport-netty3]
[2016-12-18T16:05:46,788][INFO ][o.e.p.PluginsService     ] [dXvLN44] loaded module [transport-netty4]
[2016-12-18T16:05:46,789][INFO ][o.e.p.PluginsService     ] [dXvLN44] no plugins loaded
[2016-12-18T16:05:50,767][INFO ][o.e.n.Node               ] [dXvLN44] initialized
[2016-12-18T16:05:50,768][INFO ][o.e.n.Node               ] [dXvLN44] starting ...
[2016-12-18T16:05:50,980][INFO ][o.e.t.TransportService   ] [dXvLN44] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2016-12-18T16:05:50,989][WARN ][o.e.b.BootstrapCheck     ] [dXvLN44] max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
[2016-12-18T16:05:54,128][INFO ][o.e.c.s.ClusterService   ] [dXvLN44] new_master {dXvLN44}{dXvLN442RHqsd3WXSmZI5Q}{Y9t-5aucQTOHgEVgO5bV9Q}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2016-12-18T16:05:54,159][INFO ][o.e.h.HttpServer         ] [dXvLN44] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2016-12-18T16:05:54,160][INFO ][o.e.n.Node               ] [dXvLN44] started
[2016-12-18T16:05:54,641][INFO ][o.e.g.GatewayService     ] [dXvLN44] recovered [7] indices into cluster_state
[2016-12-18T16:05:55,917][INFO ][o.e.c.r.a.AllocationService] [dXvLN44] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[my_index][0]] ...]).
Sekarang, kamu dapat menggunakan Curl untuk mengakses http://localhost:9200/ seperti pada gambar berikut:
$ curl -XGET http://localhost:9200/?pretty=true
{
  "name" : "dXvLN44",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "YZuWH53RQZifeETAZ4PGbQ",
  "version" : {
    "number" : "5.0.0",
    "build_hash" : "253032b",
    "build_date" : "2016-10-26T04:37:51.531Z",
    "build_snapshot" : false,
    "lucene_version" : "6.2.0"
  },
  "tagline" : "You Know, for Search"
}

2. Membuat indeks baru

Untuk membuat sebuah indeks baru dapat menggunakan format seperti berikut:
PUT localhost:9200/nama_indeks
Maka sekarang kita dapat membuat sebuah indeks store dengan menggunakan Postman seperti pada gambar berikut:

-- gambar membuat indeks

Kemudian kita dapat melihat informasi indeks dan mapping-nya dengan menggunakan Curl di terminal:

$ curl -XGET http://localhost:9200/store?pretty=true
{
  "store" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1482057154729",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "bwpm0ie5Q52-Hz4EM4T0-g",
        "version" : {
          "created" : "5000099"
        },
        "provided_name" : "store"
      }
    }
  }
}

Bila kamu membuat indeks yang sama maka akan muncul error seperti berikut:

curl -XPUT http://localhost:9200/store?pretty=true
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_already_exists_exception",
        "reason" : "index [store/bwpm0ie5Q52-Hz4EM4T0-g] already exists",
        "index_uuid" : "bwpm0ie5Q52-Hz4EM4T0-g",
        "index" : "store"
      }
    ],
    "type" : "index_already_exists_exception",
    "reason" : "index [store/bwpm0ie5Q52-Hz4EM4T0-g] already exists",
    "index_uuid" : "bwpm0ie5Q52-Hz4EM4T0-g",
    "index" : "store"
  },
  "status" : 400
}

Untuk menghapus sebuah indeks dapat dilakukan dengan cara berikut:

$ curl -XDELETE http://localhost:9200/store?pretty=true
{
  "acknowledged" : true
}

Silahkan buat kembali indeks store dan seperti yang kamu lihat, disana mappings-nya masih kosong karena kita belum membuat types. Sekarang mari kita pasang mapping berikut dengan menggunakan Curl:

$ curl -XPUT http://localhost:9200/store?pretty=true -d '{"mappings":{"games":{"properties":{"title":{"type":"text"},"description":{"type":"text"},"rating":{"type":"float"},"published_at":{"type":"date"}}}}}'
{"acknowledged":true,"shards_acknowledged":true}

Sekarang kamu dapat memeriksa mapping yang sudah baru dibuat terhadap indeks store dimana kita menambahkan types games:

$ curl -XGET http://localhost:9200/store?pretty=true
{
  "store" : {
    "aliases" : { },
    "mappings" : {
      "games" : {
        "properties" : {
          "description" : {
            "type" : "text"
          },
          "published_at" : {
            "type" : "date"
          },
          "rating" : {
            "type" : "float"
          },
          "title" : {
            "type" : "text"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1482058002611",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "yNXRlV1wQHuuZ3f05AJgaw",
        "version" : {
          "created" : "5000099"
        },
        "provided_name" : "store"
      }
    }
  }
}

$ curl -XGET http://localhost:9200/store/_mappings?pretty=true
{
  "store" : {
    "mappings" : {
      "games" : {
        "properties" : {
          "description" : {
            "type" : "text"
          },
          "published_at" : {
            "type" : "date"
          },
          "rating" : {
            "type" : "float"
          },
          "title" : {
            "type" : "text"
          }
        }
      }
    }
  }
}

Menarik bukan? sekarang kita akan mencoba membuat beberapa dokumen untuk kita coba lakukan pencarian dengan menggunakan Elasticsearch.

4. Menambahkan dokumen baru

Membuat dokumen satu per satu:
$ curl -XPOST http://localhost:9200/store/games/1?pretty=true -d '{"title":"Frozen Free Fall", "description":"sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozen", "rating":4.8, "published_at":"2016-01-05"}'
{
  "_index" : "store",
  "_type" : "games",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
Untuk memeriksanya silahkan eksekusi Curl berikut:
$ curl -XGET http://localhost:9200/store/games/_search?pretty=true
{
  "took" : 50,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Frozen Free Fall",
          "description" : "sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozen",
          "rating" : 4.8,
          "published_at" : "2016-01-05"
        }
      }
    ]
  }
}
Bila kita menimpanya, maka akan membuat dokumen tersebut menjadi versi terbaru:
$ curl -XPOST http://localhost:9200/store/games/1?pretty=true -d '{"title":"Frozen Free Fall", "description":"sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozens", "rating":4.8, "published_at":"2016-01-05"}'
{
  "_index" : "store",
  "_type" : "games",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}

$ curl -XGET http://localhost:9200/store/games/1?pretty=true { "_index" : "store", "_type" : "games", "_id" : "1", "_version" : 3, "found" : true, "_source" : { "title" : "Frozen Free Fall", "description" : "sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozens", "rating" : 4.8, "published_at" : "2016-01-05" } }

Kamu dapat menggunakan bulk insert yang datanya berada di dalam file JSON:

$ curl -XPOST 'http://localhost:9200/store/games/_bulk?pretty&refresh' --data-binary "@multi_doc.json"
{
  "took" : 53,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "store",
        "_type" : "games",
        "_id" : "1",
        "_version" : 7,
        "result" : "updated",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : false,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_version" : 4,
        "result" : "updated",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : false,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_version" : 4,
        "result" : "updated",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : false,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_version" : 4,
        "result" : "updated",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : false,
        "status" : 200
      }
    },
    {
      "index" : {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_version" : 1,
        "result" : "created",
        "forced_refresh" : true,
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    }
  ]
}

Isi dari file multi_doc.json diatas adalah sebagai berikut:

{"index":{"_id":"1"}}
{"title":"Frozen Free Fall", "description":"sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozen", "rating":4.8, "published_at":"2016-01-05"}
{"index":{"_id":"2"}}
{"title":"Inside Out", "description":"sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out", "rating":4.5, "published_at":"2016-01-10"}
{"index":{"_id":"3"}}
{"title":"Spiderman", "description":"Running game dengan berbagai karakter spiderman dari berbagai dimensi", "rating":4.9, "published_at":"2015-12-05"}
{"index":{"_id":"4"}}
{"title":"Minion Rush", "description":"Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya", "rating":4.4, "published_at":"2015-05-05"}
{"index":{"_id":"5"}}
{"title":"Mini Racing Team", "description":"Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu", "rating":4.5, "published_at":"2015-02-05"}

Sekarang mari kita lihat hasil bulk insert diatas:

$ curl -XGET http://localhost:9200/store/games/_search?pretty=true
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "Mini Racing Team",
          "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu",
          "rating" : 4.5,
          "published_at" : "2015-02-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Inside Out",
          "description" : "sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out",
          "rating" : 4.5,
          "published_at" : "2016-01-10"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "Minion Rush",
          "description" : "Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya",
          "rating" : 4.4,
          "published_at" : "2015-05-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Frozen Free Fall",
          "description" : "sebuah game mencocokkan yang dibuat dengan mengadaptasi Disney Frozen",
          "rating" : 4.8,
          "published_at" : "2016-01-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "Spiderman",
          "description" : "Running game dengan berbagai karakter spiderman dari berbagai dimensi",
          "rating" : 4.9,
          "published_at" : "2015-12-05"
        }
      }
    ]
  }
}

5. Menghapus dokumen

Untuk menghapus suatu dokumen, dapat tunjuk langsung dokumen dengan id yang dituju:
$ curl -XDELETE http://localhost:9200/store/games/1?pretty=true
{
  "found" : true,
  "_index" : "store",
  "_type" : "games",
  "_id" : "1",
  "_version" : 8,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
Bila kita akses dokumen tersebut maka tidak akan dapat ditemukan:
$ curl -XGET http://localhost:9200/store/games/1?pretty=true
{
  "_index" : "store",
  "_type" : "games",
  "_id" : "1",
  "found" : false
}

6. Melakukan pencarian

Kamu dapat melakukan pencarian pada semua dokumen seperti berikut:
$ curl -XGET http://localhost:9200/store/games/_search?pretty=true
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "Mini Racing Team",
          "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu",
          "rating" : 4.5,
          "published_at" : "2015-02-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Inside Out",
          "description" : "sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out",
          "rating" : 4.5,
          "published_at" : "2016-01-10"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "Minion Rush",
          "description" : "Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya",
          "rating" : 4.4,
          "published_at" : "2015-05-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "Spiderman",
          "description" : "Running game dengan berbagai karakter spiderman dari berbagai dimensi",
          "rating" : 4.9,
          "published_at" : "2015-12-05"
        }
      }
    ]
  }
}
Dapat juga dengan menyaring pada field title atau description seperti berikut:
$ curl -XGET http://localhost:9200/store/games/_search?pretty=true -d '{"query":{"match":{"description":"Running"}}}'
{
  "took" : 50,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5974999,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_score" : 0.5974999,
        "_source" : {
          "title" : "Minion Rush",
          "description" : "Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya",
          "rating" : 4.4,
          "published_at" : "2015-05-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_score" : 0.27233246,
        "_source" : {
          "title" : "Spiderman",
          "description" : "Running game dengan berbagai karakter spiderman dari berbagai dimensi",
          "rating" : 4.9,
          "published_at" : "2015-12-05"
        }
      }
    ]
  }
}

$ curl -XGET http://localhost:9200/store/games/_search?pretty=true -d '{"query":{"match":{"title":"Mini"}}}' { "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.25316024, "hits" : [ { "_index" : "store", "_type" : "games", "_id" : "5", "_score" : 0.25316024, "_source" : { "title" : "Mini Racing Team", "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu", "rating" : 4.5, "published_at" : "2015-02-05" } } ] } }

Kamu juga dapat melakukan pengurutan pada hasil pencarian seperti berikut:

$ curl -XGET 'http://localhost:9200/store/_search?pretty=true&sort=rating:asc'
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "title" : "Minion Rush",
          "description" : "Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya",
          "rating" : 4.4,
          "published_at" : "2015-05-05"
        },
        "sort" : [
          4.4
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "title" : "Mini Racing Team",
          "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu",
          "rating" : 4.5,
          "published_at" : "2015-02-05"
        },
        "sort" : [
          4.5
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "title" : "Inside Out",
          "description" : "sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out",
          "rating" : 4.5,
          "published_at" : "2016-01-10"
        },
        "sort" : [
          4.5
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "title" : "Spiderman",
          "description" : "Running game dengan berbagai karakter spiderman dari berbagai dimensi",
          "rating" : 4.9,
          "published_at" : "2015-12-05"
        },
        "sort" : [
          4.9
        ]
      }
    ]
  }
}

$ curl -XGET http://localhost:9200/store/games/_search?pretty=true -d '{"query":{"match_all":{}}, "sort":{"rating":"desc"}}'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "title" : "Spiderman",
          "description" : "Running game dengan berbagai karakter spiderman dari berbagai dimensi",
          "rating" : 4.9,
          "published_at" : "2015-12-05"
        },
        "sort" : [
          4.9
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "title" : "Mini Racing Team",
          "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu",
          "rating" : 4.5,
          "published_at" : "2015-02-05"
        },
        "sort" : [
          4.5
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "title" : "Inside Out",
          "description" : "sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out",
          "rating" : 4.5,
          "published_at" : "2016-01-10"
        },
        "sort" : [
          4.5
        ]
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "title" : "Minion Rush",
          "description" : "Running game yang bisa kamu mainkan bersama minion dan berbagai quest lainnya",
          "rating" : 4.4,
          "published_at" : "2015-05-05"
        },
        "sort" : [
          4.4
        ]
      }
    ]
  }
}

Sebagai penutup, kamu juga dapat melakukan "query between" di dalam Elasticsearch:

$ curl -XGET http://localhost:9200/store/games/_search?pretty=true -d '{"query":{"bool":{"must":{"match_all":{}},"filter":{"range":{"rating":{"gt":4.4,"lt":4.9}}}}}}'
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "Mini Racing Team",
          "description" : "Side scrolling racing yang bisa kamu mainkan dengan berbagai kendaraan unik dan karakter yang lucu",
          "rating" : 4.5,
          "published_at" : "2015-02-05"
        }
      },
      {
        "_index" : "store",
        "_type" : "games",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Inside Out",
          "description" : "sebuah game bubble pop yang dibuat dengan mengadaptasi Disney Inside Out",
          "rating" : 4.5,
          "published_at" : "2016-01-10"
        }
      }
    ]
  }
}

7. Penutup

Setelah melalui tutorial ini, kamu dapat mengenal Elasticsearch walaupun kulit kacangnya saja. Kamu juga sudah dapat membuat indeks sendiri serta types sendiri. Selain itu di tutorial ini, kamu sudah mengetahui untuk membuat dokumen baru baik satu per satu maupun bulk insert. Dan terakhir kamu dapat melakukan penghapusan suatu dokumen ataupun pencarian di Elasticsearch.

Selanjutnya, kamu dapat membaca Elasticsearch Official Documentation untuk menelusuri lebih dalam ada hal - hal menarik apa sajakah yang ada di dalam Elasticsearch.

8. Referensi

  • Elasticsearch Official Documentation
  • Tutorialspoint - Elasticsearch
(arslan/elasticsearch)