【ES】给索引指定Mapping并创建内容并查询
【Mapping的用处】
mapping会把JSON文档文档映射成Lucene所需要的扁平格式
一个mapping属于一个索引的type
每个文档都属于一个Type
一个type又一个mapping定义
7.0开始,不需要在mapping定义中指定type信息,因为默认每个索引只有一个type叫"_doc"
【创建索引member并定义两个字段的mapping】
命令:
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/member' -d' {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",//text类型全文搜索
"fields" : {
"keyword" : {
"type" : "keyword",//keyword支持聚合查询
"ignore_above" : 256
}
}
},
"age":{
"type":"text",
"index_options":"offsets"//控制倒排索引记录的内容。offsets最多,记录四个
}
}
}
}'
反馈:
{"acknowledged":true,"shards_acknowledged":true,"index":"member"}
【以Post方式创建一个成员andy】
命令:
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/member/_doc' -d' {
"name":"andy",
"age":"11"
}'
反馈:
{"_index":"member","_type":"_doc","_id":"GPYzH4EB81RixwMxUXbz","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
【以PUT方式创建一个成员bill】
命令:
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/member/_doc/2' -d' {
"name":"bill",
"age":"21"
}'
反馈:
{"_index":"member","_type":"_doc","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
【查询名称为andy的memebr】
命令:
curl -H "Content-Type: application/json" -XGET 'localhost:9200/member/_search' -d' {
"query":{
"match":{
"name":{
"query":"andy"
}
}
}
}'
反馈:
{"took":1272,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.6931471,"hits":[{"_index":"member","_type":"_doc","_id":"GPYzH4EB81RixwMxUXbz","_score":0.6931471,"_source": { "name":"andy", "age":"11" }}]}}
【查询名称为andy的memebr(漂亮模式)】
命令:
curl -H "Content-Type: application/json" -XGET 'localhost:9200/member/_search?pretty' -d' {
"query":{
"match":{
"name":{
"query":"andy"
}
}
}
}'
反馈:
{ "took" : 28, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.6931471, "hits" : [ { "_index" : "member", "_type" : "_doc", "_id" : "GPYzH4EB81RixwMxUXbz", "_score" : 0.6931471, "_source" : { "name" : "andy", "age" : "11" } } ] } }
【参考网页】
1. https://www.jianshu.com/p/7950d6cd78e5
2. https://blog.csdn.net/fanrenxiang/article/details/85317344
END