2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ElasticSearchで検索のプロになる!04.自動インデックス作成の秘密

Last updated at Posted at 2016-02-18

先ほど、RESTのPUT、POSTでデータを作成しました。
ElasticSearchはスキマーレスということもあり、スキーマを設定しなくとも、自動で設定してくれます。

その辺りの仕組みを少々記載します。

ElasticSearchはインデックスのドキュメントのスキーマを送られてきたJsonから推測します。

たとえば。

{
"field1":10,
"field2":"10"
}

とあった場合、field1は数値型、field2は文字列型と推定するわけです。

ちょっと登録してみます。
タイプ:testはすでにあるのでtest2で

curl -X POST http://192.168.33.10:9200/blog/test2/ -d '{"field1":10,"field2":"10"}'

Response

{
  "_index" : "blog",
  "_type" : "test2",
  "_id" : "AU4BKkpR8Y0sBqrTHRLZ",
  "_version" : 1,
  "created" : true
}

では、index:blog, type:test2のスキーマを見てみます。

curl -x GET http://192.168.33.10:9200/blog/test2/_mapping?pretty

{
  "blog" : {
    "mappings" : {
      "test2" : {
        "properties" : {
          "field1" : {
            "type" : "long"
          },
          "field2" : {
            "type" : "string"
          }
        }
      },
      "test" : {
        "properties" : {
          "title" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

test,test2のスキーマが出力されます。
見てみればわかるように
field1:long
field2:string
になっています。
数値型は、longなので、ここに小数点を含んだ数値型をPOSTすると
小数点は切り捨てられます。
なので注意が必要。

自動はすごく便利なのですが、それでは実際に運用する場合に困ることも多いですな。
そんな時は、スキーマ(ElasticSearchではマッピングといいます)を指定してindexを作らないといけない。

また、文字列検索するときは検索するためにN-Gramやら形態素で解析できるフィールドにしないといけない。

次はその辺りを記載します。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?