前言
Elasticsearch一共有两种主键生成策略,一个是手动指定和一个是自动生成!
1. JAVA操作Elasticsearch手动指定ID
IndexRequest indexRequest = new IndexRequest(indexName);
// 手动指定id
indexRequest.id(obj.getId());
效果如图
2. JAVA操作Elasticsearch自动生成ID
无须指定ID,es会自动生成id规则
3. DSL方式手动指定ID
示例
PUT /index/type/id
{
"xxx":"xxx"
}
真实请求
PUT /cs_index/product/2
{
"name":"xingdailu",
"desc":"nvmingxing",
"price":100
}
执行结果
{
"_index": "cs_index",
"_type": "product",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
4. DSL方式自动生成ID
示例(请求中不带/id)
POST /index/type
{
"xxx":"xxx"
}
真实请求文章来源:https://www.toymoban.com/news/detail-401988.html
POST /test_index/order/
{
"user":"3"
}
执行结果文章来源地址https://www.toymoban.com/news/detail-401988.html
{
"_index": "test_index",
"_type": "order",
"_id": "op33M2jBk5d8CGC4Toxn",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
到了这里,关于JAVA操作Elasticsearch指定主键_id生成策略的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!