0
点赞
收藏
分享

微信扫一扫

elasticsearch创建索引和mapping

90哦吼 2022-04-01 阅读 94
java后端

1. 创建索引的同时指定特殊字段的类型

PUT /gunspoc
{
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"keyword"
        },
        "age":{
          "type": "long"
        },
        "address":{
          "type":"text"
        },
        "birthday":{
           "type": "date",
           "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
        }
      }
    }
  }
}

这里建立了一个索引,且指定了name的类型为keyword,这样name字段就可以用term来精准搜索了,long类型的age就可以用来计算和比较大小了,birthday指定为日期类型,其可以是format中的格式化类型也可以是时间戳类型,其他的没有指定类型的字段将以默认的映射来在es中建立字段。

2. 查看mapping

GET /gunspoc/_mapping

结果:

{
  "gunspoc" : {
    "mappings" : {
      "doc" : {
        "properties" : {
          "address" : {
            "type" : "text"
          },
          "age" : {
            "type" : "long"
          },
          "birthday" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          },
          "name" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

3. 查看settings

GET /gunspoc/_settings?pretty

结果:

{
  "gunspoc" : {
    "settings" : {
      "index" : {
        "creation_date" : "1607391267314",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "Oljl4kqeSFiWCFz_1M5XvA",
        "version" : {
          "created" : "6050499"
        },
        "provided_name" : "gunspoc"
      }
    }
  }
}

4. 建立索引同时设置settings

PUT /gunspoc2
{
  "settings": {
    "number_of_shards": 6,
    "number_of_replicas": 1,
    "refresh_interval": "10s",
    "translog":{
      "flush_threshold_size":"1gb",
      "sync_interval":"30s",
      "durability":"async"
    }
  },
  "mappings": {
    "doc":{
      "properties":{
        "name":{
          "type":"keyword"
        },
        "age":{
          "type": "long"
        },
        "address":{
          "type":"text"
        },
        "birthday":{
           "type": "date",
           "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
        }
      }
    }
  }
}
举报

相关推荐

0 条评论