第 8 章 关于 Mapping

查看 Mapping

之前说 type 可以理解为table,那每个字段的数据类型是如何定义的呢

默认情况下, 是由插入的第一条数据的类型来自动推断来设定的!

可以通过 Mapping 来设置和查看每个字段的数据类型.

GET movie_index/movie/_mapping

true/false → boolean
1020  →  long
20.1 → double
“2018-02-01” → date
“hello world” → text +keyword

手动指定 mapping

搭建索引

PUT movie_chn
{
  "mappings": {
    "movie":{
      "properties": {
        "id":{
          "type": "long"
        },
        "name":{
          "type": "text"
          , "analyzer": "ik_smart"
        },
        "doubanScore":{
          "type": "double"
        },
        "actorList":{
          "properties": {
            "id":{
              "type":"long"
            },
            "name":{
              "type":"keyword"
            }
          }
        }
      }
    }
  }
}

插入数据

PUT /movie_chn/movie/1
{ "id":1,
  "name":"红海行动",
  "doubanScore":8.5,
  "actorList":[  
    {"id":1,"name":"张译"},
    {"id":2,"name":"海清"},
    {"id":3,"name":"张涵予"}
   ]
}
PUT /movie_chn/movie/2
{
  "id":2,
  "name":"湄公河行动",
  "doubanScore":8.0,
  "actorList":[  
    {"id":3,"name":"张涵予"}
  ]
}

PUT /movie_chn/movie/3
{
  "id":3,
  "name":"红海事件",
  "doubanScore":5.0,
  "actorList":[  
    {"id":4,"name":"张晨"}
  ]
}

查询

GET /movie_chn/movie/_search
{
  "query": {
    "match": {
      "name": "红海"
    }
  }
}

GET /movie_chn/movie/_search
{
  "query": {
    "term": {
      "actorList.name": "张"
    }
  }
}

results matching ""

    No results matching ""