在elasticsearch中建立index,并添加type定义mapping

对于初次接触elasticsearch的童鞋们,为了便于理解,在此拿elasticsearch与mysql作对比进行说明:

  ES中的index相当于mysql的db,一个mysql可以有多个db,类似的,一个ES集群可以有多个index。

  ES中的type相当于mysql中的某个表,mysql中的某个db可以有多个表,在某个表中存储我们的某一类数据。

  ES中的type对应的mapping,相当于mysql中的表结构,定义了不同字段的数据类型。

即,总结一下:

  当我们想要在ES中存储我们的某类业务数据时,需要1、先建立一个index;2、在这个index中建立一个对应的type,并定义它的数据格式mapping;3、前两步操作成功之后即可以在ES中存储我们的数据。

1、建立index

curl -XPUT "localhost:9200/hot_rank_201802?pretty"    #hot_rank_201802是索引名称

2、建立type,并构建mapping

# hot_rank_201802是index名称 baidu_hot_search_rank是type名称 -d后面是对应的mysql的表结构
curl -XPUT 'localhost:9200/hot_rank_201802/_mapping/baidu_hot_search_rank?pretty' -H 'Content-Type: application/json' -d '{
  "properties": {
    "long_rank": {
        "type": "long"
    },
    "kw_topic": {
        "type": "keyword"
    },
    "kw_url": {
        "type": "keyword"
    },
    "kw_mark": {
        "type": "keyword"
    },
    "long_searchIndex": {
        "type": "long"
    },
    "date_grabAt": {
        "type": "date"
    },
    "kw_relatedNewsUrl": {
        "type": "keyword"
    },
    "kw_relatedVideoUrl": {
        "type": "keyword"
    },
    "kw_relatedImgUrl": {
        "type": "keyword"
    },
  "kw_searchTrend": {
        "type": "keyword"
    }
} }'

经过这两步,我们已经成功在ES中建立起我们的数据表。

原文地址:https://www.cnblogs.com/hcy-fly/p/8423185.html