Skip to content

Instantly share code, notes, and snippets.

@k0f1sh
Created October 8, 2018 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0f1sh/3f318cfaaae7411c38f7fee95fe22a31 to your computer and use it in GitHub Desktop.
Save k0f1sh/3f318cfaaae7411c38f7fee95fe22a31 to your computer and use it in GitHub Desktop.
spandexでelasticsearch api叩いてみる
(ns hoge.core
(:require [qbits.spandex :as s]))
;; 参考: https://dev.classmethod.jp/server-side/elasticsearch-getting-started-07/
(def conn (s/client {:hosts ["http://localhost:9200"]}))
;; clusterの状態確認
(s/request conn {:url "/_cat/health?v"})
;; すべてのIndexの情報一覧を確認
(s/request conn {:url "/_cat/indices?v"})
;; customerという名前のインデックスを作成
(s/request conn {:url [:customer]
:method :put})
;; 確認
(s/request conn {:url "/_cat/indices?v&index=customer"})
;; shardsの状態を確認a
(s/request conn {:url "/_cat/shards?v&index=customer"})
;; customerにデータ登録
(s/request conn {:url [:customer :external 1]
:method :put
:body {:name "John Doe"}})
;; 取得
(s/request conn {:url [:customer :external 1]})
;; id自動生成
(s/request conn {:url [:customer :external]
:method :post
:body {:name "John Doe"}})
;; 更新
(s/request conn {:url [:customer :external 1 :_update]
:method :post
:body {:doc {:name "Jane Doe"}}})
(s/request conn {:url [:customer :external 1 :_update]
:method :post
:body {:doc {:name "Jane Doe" :age 20}}})
;; scriptを使ったドキュメントの更新
(s/request conn {:url [:customer :external 1 :_update]
:method :post
:body {:script "ctx._source.age += 5"}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment