三、映射操作

三、映射操作

映射类似于数据库(database)中的表结构(table)。创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。

{
    "properties":{
        "name":{           //属性 :姓名
            "type":"text",   //类型为text   可以分词
            "index":true     //可以被索引
         },
         "sex":{
          "type": "keyword",   //表示不能被分词 需要完整匹配
          "index": true
       	 },
        "age":{
            "type": "keyword",
            "index": false
        }  
    }
}

1) 创建映射

在 Postman中,向ES服务器发PUT请求 :http://127.0.0.1:9200/user/_mapping

{
    "properties":{
        "name":{           
            "type":"text",   
            "index":true    
         },
         "sex":{
          "type": "keyword",   
          "index": true
       	 },
        "age":{
            "type": "keyword",
            "index": false
        }  
    }
}
2) 查看索引

在 Postman中,向ES服务器发get请求 :http://127.0.0.1:9200/user/_mapping

//响应结果
{
    "user": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "keyword",
                    "index": false
                },
                "name": {
                    "type": "text"
                },
                "sex": {
                    "type": "keyword"
                }
            }
        }
    }
}
3)放入部分文档数据

​ 1. put 方式 create http://127.0.0.1:9200/user/_create/1

{
    "name":"华为手机",
    "sex":"女",
    "age":"12"
}

2.put 方式 doc http://127.0.0.1:9200/user/_doc/2

{
    "name":"小米",
    "sex":"男",
    "age":"18"
}

3.先用 http://127.0.0.1:9200/user/_search 查看文档数据

{
    "took": 1197,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "华为手机",
                    "sex": "女",
                    "age": "12"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "小米",
                    "sex": "男",
                    "age": "18"
                }
            }
        ]
    }
}

4)在body中

{
	"query":{
		"match":{
			"age":"18"
		}
	}
}
//响应结果  age 在映射中是不能被索引的,无法查看
{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: Cannot search on field [age] since it is not indexed.",
                "index_uuid": "ax4BLR7hQx2OFEPmn11rHw",
                "index": "user"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "user",
                "node": "3FhxLS28TNqD0cRRYLH80g",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: Cannot search on field [age] since it is not indexed.",
                    "index_uuid": "ax4BLR7hQx2OFEPmn11rHw",
                    "index": "user",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Cannot search on field [age] since it is not indexed."
                    }
                }
            }
        ]
    },
    "status": 400
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值