您的位置:首页 > Web前端 > HTML5

ElasticSearch5:Document id的手动指定和自动生成两种解析

2017-12-28 09:43 549 查看
1.document的id

1)根据应用情况来说,是否满足是否指定document id的手动指定,一般情况,是从某些其他的系统中,导入一些数据到es时,会采用这种方式,就是使用系统中已有数据的唯一标识,作为es中document的id,举个例子,比如说我们现在在开发一个电商网站,做搜索功能,或者oa系统的员工查询,这个时候,数据首先会在网站的系统中的数据库中,会先有一份,此时就肯定会有一个数据库的primary key(自增长,uuid,业务id),此时就比较适合采用数据在数据库中已有的primary key

语法:

PUT /index/type/id

{

    "xxx":"xxx"

}

例子:

PUT /ecommerce/product/4

{

  "name":"yayale yagao",

  "desc":"gaoxiao meibai",

  "price":100,

  "producer":"yayale producer",

  "tags":[

    "fangzhu","meibai","qingxin"

    ]

}

执行结果:

{

  "_index": "ecommerce",

  "_type": "product",
  "_id": "4",

  "_version": 2,

  "result": "updated",

  "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

  },

  "_seq_no": 2,

  "_primary_term": 3

}

2)如果数据主要存储到es中,那么就可以使用document中的自动生成的id

语法:不写id

POST /index/type

{

    "xxx":"xxx"

}
例子:

POST /test_index/salesOrder/

{

  "OrderProductIds":"5"

}

执行结果:生成的自动id是20的guid

{

  "_index": "test_index",

  "_type": "salesOrder",

  "_id": "1l3zmmABEP2XuYqp4PmX",

  "_version": 1,

  "result": "created",

  "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

  },

  "_seq_no": 0,

  "_primary_term": 1

}

例子2:

POST /ecommerce/product

{

  "name":"heiren yagao",

  "desc":"gaoxiao meibai",

  "price":100,

  "producer":"heiren producer",

  "tags":[

    "fangzhu","meibai","qingxin"

    ]

}

执行结果:

{

  "_index": "ecommerce",

  "_type": "product",

  "_id": "1131mmABEP2XuYqpoflk",

  "_version": 1,

  "result": "created",

  "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

  },

  "_seq_no": 3,

  "_primary_term": 3

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch