elastic search中的配置和mappings定义了索引的基本配置和数据结构及数据结构的基本配置。其中字段的类型类似于关系型数据库里面的字段映射。
Settings
查看所有索引
curl ‘localhost:9200/_cat/indices?v&pretty’
shengl-pro:tmp shengl$ curl 'localhost:9200/_cat/indices?v&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open address EvIdMEAdRd2fSNPK2GXOfw 5 1 11 0 45.3kb 45.3kb yellow open book N4ifaCnKSZ-25XQSY-eVYA 5 1 7 1 27.1kb 27.1kb yellow open customer FTwjUka-TxeD_U7YAm40hA 5 1 2 0 7.2kb 7.2kb yellow open addressfile LHFYHXa9R1iBx6-LMy3aSQ 5 1 2 0 4.6kb 4.6kb
查询索引settings
curl -XGET ‘localhost:9200/addressfile/_settings?pretty’
shengl-pro:tmp shengl$ curl -XGET 'localhost:9200/addressfile/_settings?pretty'
{
"addressfile" : { # index Name
"settings" : { # settings info
"index" : {
"creation_date" : "1539444600576",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "LHFYHXa9R1iBx6-LMy3aSQ",
"version" : {
"created" : "5050399"
},
"provided_name" : "addressfile"
}
}
}
}
新建索引及其配置
- 创建一个只有主分片没有副本的索引
curl -X PUT "localhost:9200/my_temp_index" -H 'Content-Type: application/json' -d' { "settings": { "number_of_shards" : 1, "number_of_replicas" : 0 } } '
修改索引Settings
- 通过update修改副本数
curl -X PUT "localhost:9200/my_temp_index/_settings" -H 'Content-Type: application/json' -d' { "number_of_replicas": 1 } '
配置分析器
- standard,通过单词边界分割切词
- standard词汇但愿过滤器,整理分词器触发的语汇但愿
- lowercase,转换为小写
stop删除停用词 ,默认不会被打开
创建一个新的分析器
curl -X PUT "localhost:9200/spanish_docs" -H 'Content-Type: application/json' -d' { "settings": { "analysis": { "analyzer": { "es_std": { "type": "standard", "stopwords": "_spanish_" } } } } } '
自定义分析器
一个分析器组合了三种函数,顺序执行
- 字符过滤器,整理尚未被分词的字符串,比如HTML格式的网页中的
,
,且可以吧Á转为对应的真实字符。一个分析器可能有0个或者多个字符过滤器。 - 分词器,一个分析器必须有一个唯一的分词器。用来分词。
- 词单元过滤器,可以修改,过滤,添加或者移除单元。
template: { "settings": { "analysis": { "char_filter": { ... custom character filters ... }, "tokenizer" : { ... custom tokenizer ... }, "filter": { ... custom filter ... }, "analyzer": { ... custom analyzers ... }, }
- 字符过滤器,整理尚未被分词的字符串,比如HTML格式的网页中的
示范: 一个HTML分析器
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d' { "settings": { "analysis": { "char_filter": { "&_to_and": { "type": "mapping", "mappings": [ "&=> and "] }}, "filter": { "my_stopwords": { "type": "stop", "stopwords": [ "the", "a" ] }}, "analyzer": { "my_analyzer": { "type": "custom", "char_filter": [ "html_strip", "&_to_and" ], "tokenizer": "standard", "filter": [ "lowercase", "my_stopwords" ] }} }}} '追加应用分析器
curl -X PUT "localhost:9200/my_index/_mapping/my_type" -H 'Content-Type: application/json' -d' { "properties": { "title": { "type": "string", "analyzer": "my_analyzer" } } } '
索引模板
创建一个索引模板
curl -XPUT 'localhost:9200/_template/my_logs' -d '{ #1 创建一个名为my_logs的模板
"template": "logstash-*', #2 应用于所有以logstash-开头的索引
"order": 1, #3 将会覆盖默认的logstash模板,因为默认模板的order更低
"settings": {
"number_of_shards":1 #4 限制主分片数量为1
},
"mappings": {
"_default_": { #5 为所有类型禁用_all
"_all": {
"enabled": false
}
}
},
"aliases": {
"last_3_months": {} #6 添加到last_3_month别名中,别名是最佳实践
}
}
shengl-pro:tmp shengl$ curl -XPUT 'localhost:9200/_template/my_logs' -d '{
"template": "logstash-*",
"order": 1,
"settings": {
"number_of_shards":1
},
"mappings": {
"_default_": {
"_all": {
"enabled": false
}
}
},
"aliases": {
"last_3_months": {}
}
}'
{"acknowledged":true}
Mappings
ES的Mapping比较灵活(相对于关系型数据库中的字段设置+索引还要多),默认会识别类型(当然可能会出错),也可以自己定义(使用分词器,是否分词,是否存储等等)
查询索引的信息
- 下列命令会展示索引的所有信息,包括settings和mappings
curl -XGET ‘localhost:9200/address/?pretty’
shengl-pro:tmp shengl$ curl -XGET 'localhost:9200/address/?pretty' { "address" : { "aliases" : { }, "mappings" : { "tiny" : { "properties" : { "city" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "country" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "region" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "normal" : { "properties" : { "base" : { "properties" : { "country" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "city" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "country" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "detail" : { "properties" : { "city" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "region" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "rank" : { "type" : "long" }, "region" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } } }, "settings" : { "index" : { "creation_date" : "1539435278771", "number_of_shards" : "5", "number_of_replicas" : "1", "uuid" : "EvIdMEAdRd2fSNPK2GXOfw", "version" : { "created" : "5050399" }, "provided_name" : "address" } } } }
创建索引时指定映射
curl -XPUT ‘localhost:9200/blog_post?pretty’ -H ‘Content-Type: application/json’ -d ‘{
“mappings”: {
“user”: {
“_all”: {“enabled”: false},
“properties”: {
“title”: {“type”: “text”},
"name": {"type": "text"},
"age": {"type": "integer"}
}
},
"blogpost":{
"_all": {"enabled": false},
"properties": {
"title": {"type": "text" },
"body": {"type": "text" },
"summary": {"type": "text" },
"user_id": {"type": "keyword"},
"created": {
"type": "date",
"format": "strict_date_optional_time|| epoch_millis"
}
}
}
}
}’
版权声明
本文标题:75-elastic-search-settings-and-mappings
文章作者:盛领
发布时间:2018年10月22日 - 03:29:13
原始链接:http://blog.xiaoyuyu.net/post/b33f37.html
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。
如您有任何商业合作或者授权方面的协商,请给我留言:sunsetxiao@126.com
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!