ポンコツエンジニアのごじゃっぺ開発日記。

いろいろポンコツだけど、気にするな。エンジニアの日々の開発などの記録を残していきます。 自動で収入を得られるサービスやシステムを作ることが目標!!

まずはmac上でElasticsearchを使ってみる

高速で全文検索ができるという素晴らしい検索エンジンのElasticsearch。このElasticsearchを使いこなせたらきっと役に立ちそう!

ということもありまして、早めに使ってみたいなと思いましてとりあえずまずはmac上でElasticsearchを使ってみました。

その手順を記録していきます。

 

環境確認

まずはJavaが入っていることを確認します。

$ java -version
java version "1.8.0_131"

自分のPCには1.8.0_131が入っていたのでこのまま進めます。1.8以上が入っていないといけないっぽいです。

 

Elasticsearchのインストール

次にElasticsearchをインストールします。こちらのサイト 書いてありますので、こちらを見ながらやります。

https://www.elastic.co/downloads/elasticsearch

この記事を書いているときのElasticsearchのバージョンは6.2.3だったので、そのバージョンで進めていきます。

$ wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-kuromoji/analysis-kuromoji-6.2.3.zip
$ tar -xzf elasticsearch-6.2.3.tar.gz
$ cd elasticsearch-6.2.3

Elasticsearchの日本語プラグインのkuromojiもインストールしていきます。

$ bin/elasticsearch-plugin install analysis-kuromoji

インストールはこれで終わりです。

さて、実際にElasticsearchを起動させてみましょう。

$ bin/elasticsearch

1分弱程度で起動が終わると思うので、そしたらhttp://localhost:9200/にアクセスしてみましょう。

以下のようなJSONが出力されると思います。これで完了です。

$ curl http://localhost:9200/
{
  "name" : "btA8FIj",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "qtGrfELfTPqvMT2G3zym-g",
  "version" : {
    "number" : "6.2.3",
    "build_hash" : "c59ff00",
    "build_date" : "2018-03-13T10:06:29.741383Z",
    "build_snapshot" : false,
    "lucene_version" : "7.2.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

データの投入

さっそくデータを投入してみたいと思います!

$ curl -X POST http://localhost:9200/doraemon/character -d '{"name":"doraemon","voice":"mizuta wasabi"}'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

エラーが出てしまいました。。どうやらヘッダーにapplication/jsonをつけてあげないといけないみたい。。

$ curl -H "Content-Type: application/json" -X POST http://localhost:9200/doraemon/character -d '{"name":"doraemon","voice":"mizuta wasabi"}'
{"_index":"doraemon","_type":"character","_id":"CajGyWIBhdSaYVlB6suk","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

データの投入に成功しました!この調子でどんどんデータを入れていきたいと思います。

$ curl -H "Content-Type: application/json" -X POST http://localhost:9200/doraemon/character -d '{"name":"nobi nobita","voice":"ohara megumi"}'
{"_index":"doraemon","_type":"character","_id":"CqjKyWIBhdSaYVlBG8si","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

$ curl -H "Content-Type: application/json" -X POST http://localhost:9200/doraemon/character -d '{"name":"nobi tamako","voice":"mitsuishi kotono"}'
{"_index":"doraemon","_type":"character","_id":"C6jKyWIBhdSaYVlBU8u8","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

$ curl -H "Content-Type: application/json" -X POST http://localhost:9200/doraemon/character -d '{"name":"nobi nobisuke","voice":"tahara aruno"}'
{"_index":"doraemon","_type":"character","_id":"DKjKyWIBhdSaYVlBbcsG","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

ドラえもんが住んでいる家の住人を登録してみました。

データの検索

さて、ここから検索してみます。

nameがnobiという文字列に一致するものを持ってきたいと思います。

$ curl -H "Content-Type: application/json" -X GET http://localhost:9200/doraemon/character/_search -d '{"query":{"match":{"name":"nobi"}}}' |jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 100 582 100 35 24798 1491 --:--:-- --:--:-- --:--:-- 25304
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.6099695,
"hits": [
{
"_index": "doraemon",
"_type": "character",
"_id": "CqjKyWIBhdSaYVlBG8si",
"_score": 0.6099695,
"_source": {
"name": "nobi nobita",
"voice": "ohara megumi"
}
},
{
"_index": "doraemon",
"_type": "character",
"_id": "C6jKyWIBhdSaYVlBU8u8",
"_score": 0.2876821,
"_source": {
"name": "nobi tamako",
"voice": "mitsuishi kotono"
}
},
{
"_index": "doraemon",
"_type": "character",
"_id": "DKjKyWIBhdSaYVlBbcsG",
"_score": 0.2876821,
"_source": {
"name": "nobi nobisuke",
"voice": "tahara aruno"
}
}
]
}
}

野比家の3人が引っかかりました。

せっかくなのでvoiceがwasabiのものも検索したいと思います。

$ curl -H "Content-Type: application/json" -X GET http://localhost:9200/doraemon/character/_search -d '{"query":{"match":{"voice":"wasabi"}}}'
| jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 320 100 282 100 38 12483 1682 --:--:-- --:--:-- --:--:-- 12818
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "doraemon",
"_type": "character",
"_id": "CajGyWIBhdSaYVlB6suk",
"_score": 0.6931472,
"_source": {
"name": "doraemon",
"voice": "mizuta wasabi"
}
}
]
}
}

しっかりと声優が水田さわびさんのドラえもんが引っかかってくれました。

こんな感じで簡単にデータを登録して検索することができました!

こんなにも簡単に全文検索ができると夢が広がりますね!

お問い合わせプライバシーポリシー制作物