Elasticsearch ãããã³ã°
Elasticsearch Mappingâââããã¥ã¡ã³ãã¹ãã¼ãã¨æ¤ç´¢ç²¾åº¦ãæé©åããããã®ãããã³ã°å®ç¾©
Elasticsearch ã«ããããããã³ã°ã¨ã¯ããªã¬ã¼ã·ã§ãã«DBã§ããã¨ããã®ãã¼ãã«å®ç¾©ã«ç¸å½ãã¾ããããããåã«ãã¼ã¿ãæ ¼ç´ããçºã®ãã£ã¼ã«ããç¨æãã¦åãè¨å®ããã ãã§ã¯ããã¾ãããElasticsearch ã§ã¯ããã£ã¼ã«ãã®åã®ä»ã«è¨èªè§£æå¦çãªã©ã®ããã¥ã¡ã³ããæ¤ç´¢å¯è½ã«ããçºã®å種è¨å®ãå¯è½ã§ãã
ã¹ãã¼ãã¼ã¬ã¹ãä¸ã¤ã®ç¹å¾´ã® Elasticsearch ã§ã¯ãããã¥ã¡ã³ããã¤ã³ããã¯ã¹ããã¨èªåçã«åãã£ã¼ã«ãæ¯ã«ãã£ã¼ã«ãã¿ã¤ããªã©ã®ãããã³ã°ãèªåã§è¨å®ããã¤ã³ããã¯ã¹ãä½æããã¾ããã¾ããäºåã«ãããã³ã°ãè¨å®å¯è½ãªä»çµã¿ã¨ãªã£ã¦ãã¾ãã
èªåãããã³ã°
ã¾ãã¯ä»¥ä¸ã®å 容ã§ãã¤ã³ããã¯ã¹ãä½æãèªåãããã³ã°ã«ã¤ãã¦è¦ã¦è¡ãã¾ãã
- ããã°è¨äºæ¯ã«ï¼ã¤ã®ããã¥ã¡ã³ããã¤ã³ããã¯ã¹
- åããã¥ã¡ã³ãã¯ããã¥ã¡ã³ãã¿ã¤ã story ã«ã¤ã³ããã¯ã¹
- ããã¥ã¡ã³ãã¿ã¤ã story ã¯ãblog ã¤ã³ããã¯ã¹ã«å«ã¾ãã
- ã¤ã³ããã¯ã¹ã¯ ãã¼ã«ã«ã®Elasticsearch ã¯ã©ã¹ã¿ã¼å ã«åå¨
ã¾ãã¯ãããã¥ã¡ã³ããç»é²ãã¾ãã次ã®ã³ãã³ãã§ã¯ãblog ã¤ã³ããã¯ã¹é ä¸ã« story ããã¥ã¡ã³ãã¿ã¤ããä½æãããã®ããã¥ã¡ã³ãã¿ã¤ãå ã«ï¼ã¤ã®ããã¥ã¡ã³ããä½æãã¦ãã¾ãã
$ 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"
}
}
}
}
}
}
以ä¸ã®åºæ¬çãªåãã«ã¼ã«ã«å¾ã£ã¦ãèªåãããã³ã°ããããã¨ãããããã¨æãã¾ãã
ï¼ï¼ åºæ¬çãªãã£ã¼ã«ãå
String: string
Whole number: byte, short, integer, long
Floating point: float, double
Boolean: boolean
Date: dateï¼ï¼ åºæ¬çãªåãããã³ã°ã«ã¼ã«
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â
ã»ã¨ãã©ã®åã§ãåãã£ã¼ã«ãã®å±æ§ãªãã·ã§ã³ã¯ãtype ããæå®ããã¦ãã¾ããããæå®ããã¦ããªãå±æ§ãªãã·ã§ã³ã¯ãããã©ã«ãã使ç¨ããã¾ããä¾ãã°ãã¿ã¤ãã âstringâ ã®ãã£ã¼ã«ãã§ã¯ããã£ã¼ã«ãå±æ§ã®indexãªãã·ã§ã³ã¯âanalyzedâ ãé©ç¨ããã¦ãã¾ããããã¯ããã£ã¼ã«ããªãªã¸ãã«ã®å¤ã解æå¦çãããã®çµæã®å¤ã使ã£ã¦ãã¤ã³ããã¯ã¹ãä½æãã¾ãã¯æ¤ç´¢ã¯ã¨ãªãçºè¡ãããã¨ãæå³ãã¾ãã
NOTEï¼
ãã£ã¼ã«ãã¿ã¤ãã«ãã£ã¦æå®ã§ããå±æ§ãªãã·ã§ã³ãç°ãªãã¾ããèå³ã®ããæ¹ã¯ãCore Types ã®ããã¥ã¡ã³ããåç §ãã¦ã¿ã¦ãã ããã
æåãããã³ã°å®ç¾©ãå¿ è¦ãªã±ã¼ã¹
ãã¼ã¿ã®å 容ã«ãã£ã¦ãèªåã§ãããã³ã°ãããåã解æå¦çä»æ§ãªã©ã§æ¤ç´¢è¦ä»¶ãæºããã¦ããã°ãã®èªåãããã³ã°ã®æ©æµãåãããã¨ãã§ãã¾ãããçµé¨ä¸ã»ã¨ãã©ã®å ´åããã¥ã¢ã«ã§ã®ãããã³ã°å®ç¾©ãå¿ è¦ã«ãªãã®ã§ã¯ãªããã¨æãã¾ãã
ä¾ãã°ãtags ãã£ã¼ã«ãã®ããã«ãªãªã¸ãã«ãã¼ã¿ã®å 容ã§æ£ç¢ºã«æ¤ç´¢ã§ããããã«ããçºã«ã¯ãindex ãªãã·ã§ã³ã«ânot_analyzedâãè¨å®ããå¿ è¦ãããã¾ãã
{
"tags": {
"type": "string",
"index": "not_analyzed"
}
}
ã¾ããtitle ã subtitle ãcontents ãã£ã¼ã«ããªã©ã¯æ¥æ¬èªã§ã®å ¨ææ¤ç´¢ãå¿ è¦ã¨ãã¾ãã®ã§ãanalyzer ãªãã·ã§ã³ã«æ¥æ¬èªè§£æç¨ã® analyzer ãè¨å®ããå¿ è¦ãããã¾ãã
{
"title": {
"type": "string",
"analyzer": "japaneseâ"
},
"subtile": {
"type": "string",
"analyzer": "japaneseâ"
},
"contents": {
"type": "string",
"analyzer": "japaneseâ"
}
}
NOTEï¼
âJapaneseâ ã¨ããã¢ãã©ã¤ã¶ã¼ã¯ããã©ã«ãã§ã¯åå¨ãã¾ããã®ã§ããã³ãã«ããã¦ãã NGram Tokenizer ã Japanese (kuromoji) Analysis for Elasticsearch ãã©ã°ã¤ã³ãªã©ããã¼ã¹ã«æ¥æ¬èªç¨ã®ã¢ãã©ã¤ã¶ã¼ãå®ç¾©ãã¦è¨å®ãã¾ãã
ããã°ã®è¨äºãã¤ã³ããã¯ã¹ããä¾ãããã¾ãããããã®ããã«ã·ã³ãã«ãªä¾ã§ãã¡ããã¨æ¤ç´¢ã§ããããã«ããçºã«ã¯ããããã³ã°å®ç¾©ãéè¦ã§ããã¨ãããã¨ãç解ã§ãããã¨æãã¾ãã
æåãããã³ã°
ãããã³ã°ãæåã§è¨å®ããæ¹æ³ã¯ãã¤ã³ããã¯ã¹ãæ°è¦ã§ä½æããã¨ãã«ä¸ç·ã«è¨å®ããæ¹æ³ã¨ãä½ææ¸ã¿ã®ã¤ã³ããã¯ã¹ã« /_mapping ã¨ã³ããã¤ã³ãã使ã£ã¦ãæ°ããã¿ã¤ãã®è¿½å ã»æ´æ°ããæ¹æ³ãããã¾ãã
IMPORTANTï¼ï¼
æ¢åã®ã¤ã³ããã¯ã¹ã«ãã¼ã¿ãæ¢ã«ã¤ã³ããã¯ã¹ããã¦ããå ´åã/_mapping ã¨ã³ããã¤ã³ãã使ã£ã¦ãæ°ãããã£ã¼ã«ãã¿ã¤ãã追å ãããã¨ã¯ã§ãã¾ãããæ´æ°ã¯ã§ãã¾ããã
ã¾ãã¯ãæ°è¦ä½æãã
èªåãããã³ã°ã§ä½æãã blog ã¤ã³ããã¯ã¹ã次ã®ã³ãã³ãã§åé¤ãã¦ãã ããã
$ curl -XDELETE 'localhost:9200/blog'
次ã«ãblog ã¤ã³ããã¯ã¹ã®ä½æã¨ãããã³ã°å®ç¾©ãè¡ãã¾ããtitleãsubtitleãcontents ã® analyzer ãªãã·ã§ã³ã«ã«ã¹ã¿ã ã§ç¨æãã japanese ã¢ãã©ã¤ã¶ã¼ãè¨å®ãã¦ãã¾ããï¼â» ããã¦ãtags ãã£ã¼ã«ãã¯å®ç¾©ãã¦ã¾ããï¼
$ 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"
}
}
}
}
}
}
ã¡ããã¨åæ ããã¦ããã®ã確èªã§ãã¾ãã
次ã«ãä»ä½æããã¤ã³ããã¯ã¹ã®ãããã³ã°å®ç¾©ã«æ°ãã tags ãã£ã¼ã«ãç¨ã®å®ç¾©ã /_mapping ã¨ã³ããã¤ã³ãã使ã£ã¦è¿½å ãã¾ããæ£ç¢ºãªå¤ã§æ¤ç´¢ã§ããããã« âindexâ å±æ§ã« ânot_analyzedâ ãè¨å®ãã¦ãã¾ãã
$ 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"
}
}
}
}
}
}
ã¡ããã¨åæ ããã¦ããã®ã確èªã§ãã¾ãã
ãããã³ã°ã®ãã¹ã
analyze API ã使ã£ã¦ãè¨å®ãããããã³ã°ã®åä½ã確èªãããã¨ãã§ãã¾ããåã âstringâ å ã® author ãã£ã¼ã«ã 㨠tags ãã£ã¼ã«ããæ¯è¼ããã®ãåãããããã§ãã次ã®ï¼ã¤ã®ã³ãã³ãã®åºåçµæã確èªãã¦ã¿ã¦ãã ããã
â 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"
ã³ãã³ãã®å®è¡çµæã¯ã次ã®ããã«ãªãã¯ãã§ãã
- â author ãã£ã¼ã«ãï¼analyzed
âsearchâ 㨠âengineâ ã®ï¼ã¤ã®åèªã¨ãã¦è§£æçµæ表示 - â¡ tags ãã£ã¼ã«ãï¼not_analyzed
âSerach-Engineâ ã®ï¼ã¤ã®åèªã¨ãã¦è§£æçµæ表示
ãã®çµæã¯ãauthor ãã£ã¼ã«ãã«ä»®ã« âSearch-Engineâ ã¨ããæååãã¤ã³ããã¯ã¹ããå ´åã«ã¯ãâsearchâãâSearch EngineâãâEngineâããªã©ãããããªããã¿ã¼ã³ã§æ¤ç´¢ã«ããããããã¨ã表ããtags ãã£ã¼ã«ãã«å¯¾ããæ¤ç´¢ã¯ãâSearch-Engineâ ã¨ããæ£ç¢ºãªåèªã§ãããããããªããã¨ã表ãã¦ãã¾ãã
ä»åã¯ãElasticsearch ã®ãããã³ã°ã«ã¤ãã¦ã¾ã¨ãã¦ã¿ã¾ãããããã¾ã§èª¬æãã¦ããããã«ããããã³ã°ã¯æ¤ç´¢ç²¾åº¦æé©åããçºã«éè¦ãªå½¹ç®ãæã£ã¦ããã¨ç解ãã¦ããã ãããã¨æãã¾ããæ¤ç´¢ã¨ã³ã¸ã³ã®è¨è¨ã®é£ããã¨ããã¯ããã¼ã¿ã®å 容ã¨ããã®ãã¼ã¿ãã©ã®ããã«æ¤ç´¢ããã®ãã®ä¸¡æ¹ãèãã¦è¨è¨ããå¿ è¦ãããã¨ããã ã¨æãã¾ãã説æãããã¦ãã¾ããããElasticsearch ã®ãã£ã¼ã«ãã¯ããã©ãããªãã£ã¼ã«ãå®ç¾©ã®ä»ã«ãã«ãã¬ãã«ã®ãªãã¸ã§ã¯ããå®ç¾©ããããããªãè¤éãªå®ç¾©ãå¯è½ã§ãã®ã§ããã®è¾ºãã¯å¥éã¾ã¨ãããã¨æãã¾ãã