Elasticsearch マッピング

Elasticsearch Mapping — ドキュメントスキーマと検索精度を最適化するためのマッピング定義

Kunihiko Kido
Apr 10, 2014 · 13 min read

$ curl -XPUT 'localhost:9200/blog/story/1' -d '{
"title" : "Elasticsearch 特徴まとめ",
"subtitle" : "Elasticsearch Features — 主にシステムを中心とした特徴まとめ","contents": "Elasticsearch とは?簡単に説明すると、クラウド向けに構築された、RESTful な APIを提供する分散型のサーチエンジンアプリケーション。オープンソースで提供されています。",
"tags" : ["Elasticsearch", "Search-Engine"],
"pub_date" : "2014-04-10T01:40:00",
"author" : "Kunihiko Kido",
"views" : 82,
"reads": 60,
"read_ratio": 0.73,
"enabled": true
}'
$ curl -XGET 'localhost:9200/blog/_mapping/story?pretty=true'
{
"blog" : {
"mappings" : {
"story" : {
"properties" : {
"author" : {
"type" : "string"
},
"contents" : {
"type" : "string"
},
"enabled" : {
"type" : "boolean"
},
"pub_date" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"read_ratio" : {
"type" : "double"
},
"reads" : {
"type" : "long"
},
"subtitle" : {
"type" : "string"
},
"tags" : {
"type" : "string"
},
"title" : {
"type" : "string"
},
"views" : {
"type" : "long"
}
}
}
}
}
}
1) 基本的なフィールド型
String: string
Whole number: byte, short, integer, long
Floating point: float, double
Boolean: boolean
Date: date
2) 基本的な型マッピングルール
JSON type: | Field type:
-------------------------------------|---------------
Boolean: true or false | “boolean”
Whole number: 123 | “long”
Floating point: 123.45 | “double”
String, valid date: “2014-09-15" | “date”
String: “foo bar” | “string”
{
"tags": {
"type": "string",
"index": "not_analyzed"
}
}
{
"title": {
"type": "string",
"analyzer": "japanese‎"
},
"subtile": {
"type": "string",
"analyzer": "japanese‎"
},
"contents": {
"type": "string",
"analyzer": "japanese‎"
}
}
$ curl -XDELETE 'localhost:9200/blog'
$ curl -XPUT 'localhost:9200/blog' -d '
{
"mappings" : {
"story" : {
"properties" : {
"author" : {
"type" : "string"
},
"contents" : {
"type" : "string",
"analyzer": "japanese‎"
},
"enabled" : {
"type" : "boolean"
},
"pub_date" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"read_ratio" : {
"type" : "double"
},
"reads" : {
"type" : "long"
},
"subtitle" : {
"type" : "string",
"analyzer": "japanese‎"
},
"title" : {
"type" : "string",
"analyzer": "japanese‎"
},
"views" : {
"type" : "long"
}
}
}
}
}
}'
$ curl -XGET 'localhost:9200/blog/_mapping/story?pretty=true'
{
"blog" : {
"mappings" : {
"story" : {
"properties" : {
"author" : {
"type" : "string"
},
"contents" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"enabled" : {
"type" : "boolean"
},
"pub_date" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"read_ratio" : {
"type" : "double"
},
"reads" : {
"type" : "long"
},
"subtitle" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"title" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"views" : {
"type" : "long"
}
}
}
}
}
}
$ curl -XPUT 'localhost:9200/blog/_mapping/story' -d '
{
"properties": {
"tags": {
"type": "string",
"index": "not_analyzed"
}
}
}'
$ curl -XGET 'localhost:9200/blog/_mapping/story?pretty=true'
{
"blog" : {
"mappings" : {
"story" : {
"properties" : {
"author" : {
"type" : "string"
},
"contents" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"enabled" : {
"type" : "boolean"
},
"pub_date" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"read_ratio" : {
"type" : "double"
},
"reads" : {
"type" : "long"
},
"subtitle" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"tags" : {
"type" : "string",
"index" : "not_analyzed"
},

"title" : {
"type" : "string",
"analyzer" : "japanese‎"
},
"views" : {
"type" : "long"
}
}
}
}
}
}
① author フィールド:analyzed
$ curl -XGET 'localhost:9200/blog/_analyze?field=author&pretty=true' -d "Search-Engine"
② tags フィールド:not_analyzed
$ curl -XGET 'localhost:9200/blog/_analyze?field=tags&pretty=true' -d "Search-Engine"

Hello! Elasticsearch.

オープンソースのサーチエンジン Elasticsearch に関連する技術ノート

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store