Elastic Searchの検索エンジンを操作するまとめ①

はじめに

全文検索機能を実装するにあたり、Elastic Search検索エンジンを活用しようと思い、その操作方法を学んだことをまとめました!

目次

目的

Elastic SearchのKibanaのクエリの送り方

(前提、環境)

データベース(index)の作り方

settingsを決める

PUT /products
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

POST(データを保存する)

クエリを送る

POST /products/_doc/101
{
  "name": "tea pot",
  "price": 100,
  "in_stock": 12
}

実行結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "101",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 1
}

GET(取得する)

クエリを送る

GET /products/_doc/100

実行結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 3,
  "_seq_no" : 8,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "tea pot",
    "price" : 100,
    "in_stock" : 10
  }
}

UPDATE(更新する)

クエリを送る

POST /products/_doc/100/_update
{
  "doc": {
    "in_stock": 10001
  }
}

実行結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 9,
  "_primary_term" : 1
}
GET /products/_doc/100
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 7,
  "_seq_no" : 12,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "in_stock" : 10001
  }
}

更新用のスクリプト①

クエリを送る

POST /products/_doc/100/_update
{
  "script": {
    "source": "ctx._source.in_stock--"
  }
}

ポイント

①"ctx"は変数であり、"context"の略です。

②"_source"プロパティをチェーンさせることで保存してあるデータにアクセスすることができます。

③"in_stock"プロパティは保存してあるデータの中のプロパティにアクセスすると言うことです。

④減らしたい場合は–、増やしたい場合は++をチェーンさせます

結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 8,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 13,
  "_primary_term" : 1
}
GET /products/_doc/100
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 8,
  "_seq_no" : 13,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "in_stock" : 10000
  }
}

更新用のスクリプト②

クエリを送る

POST /products/_doc/100/_update
{
  "script": {
    "source": "ctx._source.in_stock = 10"
  }
}

ポイント

①今回は指定した値に更新しています!
= 10で書くことで上書きしている訳ですね。

結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 9,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 14,
  "_primary_term" : 1
}
GET /products/_doc/100
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 9,
  "_seq_no" : 14,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "in_stock" : 10
  }
}

更新用のスクリプト③

クエリを送る

POST /products/_doc/100/_update
{
  "script": {
    "source": "ctx._source.in_stock -= params.quantity"
    , "params": {
      "quantity": 4
    }
  }
}

ポイント

①今回はリクエストを送るときにparamsをセットして送っています。
②paramsの中身に数字の4をセット。
③sourceにparams.quantity分だけ引くように指定しています。

結果

{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 13,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 18,
  "_primary_term" : 1
}
GET /products/_doc/100
{
  "_index" : "products",
  "_type" : "_doc",
  "_id" : "100",
  "_version" : 13,
  "_seq_no" : 18,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "in_stock" : 6
  }
}

終わりに

ざっとですが、indexの作成やらの操作方法をudemyで学習中した内容をアウトプットしました!
まだまだやれることはたくさんあるので、シリーズ化したい・・・

よかったらシェアしてね!
  • URL Copied!
  • URL Copied!
目次
閉じる