« ^ »

Elasticsearchを使う

所要時間: 約 2分

今回は検索エンジンであるElasticsearchを使う。

簡単な操作

docker pull elasticsearch:8.10.2
コンテナイメージを取得する。
docker run --rm --name elasticsearch \
    --publish 9200:9200 \
    --publish 9300:9300 \
    --env="xpack.security.enabled=false" \
    --env="discovery.type=single-node" \
    elasticsearch:8.10.2
コンテナを起動する。

外部からの通信は基本的に9200ポートで受ける。内部的な通信は9300ポートで受ける。

:ORIGIN = http://localhost:9200

PUT :ORIGIN/book
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "book"
}
// PUT http://localhost:9200/book
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// content-length: 63
// Request duration: 0.143103s
インデックスを作成する。
:ORIGIN = http://localhost:9200

GET :ORIGIN/_cat/indices?v
health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   aaa   mpfUHiC5TCOZVW67_VKcYQ   1   1          0            0       248b           248b
yellow open   book  YtQbUM0MTSO9Fa7quNUErw   1   1          0            0       226b           226b

GET http://localhost:9200/_cat/indices?v
HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
content-type: text/plain; charset=UTF-8
Transfer-Encoding: chunked
Request duration: 0.017379s
インデックスの一覧を取得する。
:ORIGIN = http://localhost:9200

GET :ORIGIN/book/_mapping
{
  "book": {
    "mappings": {}
  }
}
// GET http://localhost:9200/book/_mapping
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// Transfer-Encoding: chunked
// Request duration: 0.017041s
マッピングを確認する。
:ORIGIN = http://localhost:9200

POST :ORIGIN/book/_doc/
Content-Type: application/json

{
  "title": "yay"
}
{
  "_index": "book",
  "_id": "WJunRosBJwkhSkuXi4bn",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
// POST http://localhost:9200/book/_doc/
// HTTP/1.1 201 Created
// Location: /book/_doc/WJunRosBJwkhSkuXi4bn
// X-elastic-product: Elasticsearch
// content-type: application/json
// content-length: 156
// Request duration: 0.277306s
ドキュメントを登録する。
:ORIGIN = http://localhost:9200

POST :ORIGIN/_search/
Content-Type: application/json

{
   "query": {
     "bool": {
       "must": [
          {
            "match": {
              "title": "yay"
            }
          }
       ]
     }
   }
}
{
  "took": 192,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "book",
        "_id": "WJunRosBJwkhSkuXi4bn",
        "_score": 0.2876821,
        "_source": {
          "title": "yay"
        }
      }
    ]
  }
}
// POST http://localhost:9200/_search/
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// Transfer-Encoding: chunked
// Request duration: 0.238364s
ドキュメントの検索
:ORIGIN = http://localhost:9200

PUT :ORIGIN/book/_doc/WJunRosBJwkhSkuXi4bn
Content-Type: application/json

{
  "title": "YAY"
}
{
  "_index": "book",
  "_id": "WJunRosBJwkhSkuXi4bn",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}
// PUT http://localhost:9200/book/_doc/WJunRosBJwkhSkuXi4bn
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// content-length: 156
// Request duration: 0.058960s
ドキュメントを更新する。
:ORIGIN = http://localhost:9200

DELETE :ORIGIN/book/_doc/WJunRosBJwkhSkuXi4bn
{
  "_index": "book",
  "_id": "WJunRosBJwkhSkuXi4bn",
  "_version": 3,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}
// DELETE http://localhost:9200/book/_doc/WJunRosBJwkhSkuXi4bn
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// content-length: 156
// Request duration: 0.020091s
ドキュメントを削除する。
:ORIGIN = http://localhost:9200

DELETE :ORIGIN/book
{
  "acknowledged": true
}
// DELETE http://localhost:9200/book
// HTTP/1.1 200 OK
// X-elastic-product: Elasticsearch
// content-type: application/json
// content-length: 21
// Request duration: 0.102885s
インデックスを削除する。