Elasticsearch 使用命令操作索引

索引相当于数据库 类型相当于表 文档相当于表中的每行数据

1、新增索引
执行下面的命令,则创建一个名称为 twitter 的索引,它拥有3个分片 2个副本

curl -XPUT '192.168.254.128:9200/product?pretty' -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
'

2、删除索引
执行下面的命令 则删除twitter索引

curl -XDELETE '192.168.254.128:9200/twitter'

3、新增mapping
在索引test 下增加一个叫test_mapping的类型 该类型拥有一个name的属性 类型为text

curl -XPUT '192.168.254.128:9200/test/_mapping/test_mapping?pretty' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}
'

新增一个使用分词器的mapping
下面的代码创建了一个product类型 除了id、name等属性外,还有一个属性 类型是nested 即集合类型 每个集合元素包含 productPlanId、id、name、parentId属性
analyzer 代表使用的分词器类型
ik_max_word 代表使用ik分词器的最大化分词
store 代表是否存储,true代表该属性存储false代表改睡醒不存储

curl -XPUT '192.168.254.128:9200/product/_mapping/product_plan?pretty' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "id":{"type":"keyword"},
    "name":{"type":"text","analyzer":"ik_max_word"},
    "desc":{"type":"text","analyzer":"ik_max_word","store":false},
    "price":{"type":"float"},
    "productId":{"type":"keyword"},
    "productName":{"type":"text","analyzer":"ik_max_word"},
    "categorys":{
        "type":"nested",
        "properties":{
            "productPlanId":{"type":"keyword"},
            "id":{"type":"keyword"},
            "name":{"type":"text","analyzer":"ik_max_word"},
            "parentId":{"type":"keyword"}
        }
    }

  }
}
'

4、添加数据

curl -XPUT '192.168.254.128:9200/product/product_plan/2?pretty' -H 'Content-Type: application/json' -d'
{
    "id":"1",
    "name":"儿童保险计划",
    "desc":"专业儿童保险产品计划",
    "price":200,
    "productId":"1",
    "productName":"保险产品",
    "categorys":[
        {
            "productPlanId":"1",
            "id":"10",
            "name":"保护儿童安全,是大家共同的责任",
            "parentId":"4"
        },
        {
            "productPlanId":"1",
            "id":"10",
            "name":"保护儿童安全,是全宇宙的共同责任",
            "parentId":"4"
        }
    ]
}
'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值