ElasticSearch[01]ElasticSearch基础操作

ElasticSearch版本:7.8.1
测试工具:Postman、ElasticSearch Head
参考视频:【尚硅谷】ElasticSearch教程入门到精通(基于ELK技术栈elasticsearch 7.8.x版本).

基础操作

索引-创建

PUT  http://127.0.0.1:9200/shopping

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}

索引-删除

DELETE  http://127.0.0.1:9200/shopping

{
    "acknowledged": true
}

索引-查单个

GET  http://127.0.0.1:9200/shopping

{
    "shopping": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1638924461235",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "vKbqxu7sQfy5EUUMNUrCCQ",
                "version": {
                    "created": "7080199"
                },
                "provided_name": "shopping"
            }
        }
    }
}

索引-查所有(_cat)

GET  http://127.0.0.1:9200/_cat/indices?v

health status index                          uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green open   shopping                       vKbqxu7sQfy5EUUMNUrCCQ   1   1          0            0       208b           208b

文档-创建-POST

POST  http://127.0.0.1:9200/shopping/_doc

//Body-JSON
{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg", 
    "price":3999.00 
}
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "lGmdl30BIh2p6feOh7Q0",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

文档-全查询

GET  http://127.0.0.1:9200/shopping/_doc/_search

{
    "took": 1066,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}

文档-创建-PUT(指定ID)

PUT  http://127.0.0.1:9200/shopping/_create/1001

Body-JSON
{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg", 
    "price":3999.00 
 }
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

文档-主键查询

GET  http://127.0.0.1:9200/shopping/_doc/1001

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999.00
    }
}

文档-删除

DELETE  http://127.0.0.1:9200/shopping/_doc/1001

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

文档-全量修改

PUT  http://127.0.0.1:9200/shopping/_doc/1001

//Body-JSON
{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg", 
    "price":4999.00 
 }
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

文档-局部修改

POST  http://127.0.0.1:9200/shopping/_update/1001

//Body-JSON
{
    "doc":{
        "title":"华为手机"
    }
 }
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

条件查询

条件查询-URL传参

GET  http://127.0.0.1:9200/shopping/_search?q=category:小米

{
    "took": 211,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.17402273,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 0.17402273,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.17402273,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                }
            }
        ]
    }
}

条件查询-请求体传参

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match":{
            "category":"小米"
        }
    }
}
{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.17402273,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 0.17402273,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.17402273,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                }
            }
        ]
    }
}

全量查询

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match_all":{
            
        }
    }
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                }
            }
        ]
    }
}

条件查询-分页、排序

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match_all":{
            
        }
    },
    //分页
    "from":0,
    "size":2,
    //选择属性查询显示
    "_source":["title"],
    //排序
    "sort":{
        "price":{
            "order":"desc"
        }
    }
}
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": null,
                "_source": {
                    "title": "华为手机"
                },
                "sort": [
                    4999.0
                ]
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": null,
                "_source": {
                    "title": "小米手机"
                },
                "sort": [
                    3999.0
                ]
            }
        ]
    }
}

条件查询-多条件查询

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "bool":{
            "should":[//相当于或  must相当于且
                {
                    "match": {
                        "category":"华为"
                    }
                },
                {
                    "match": {
                        "price":3999.00
                    }
                }
            ],
            "filter":{
                "range":{
                    //price>3999
                    "price":{
                        "gt":3999	//gt(Greater than大于)丨gte(Greater than or equal to大于等于)
                            		//丨lt(Less than小于)丨lte(Less than or equal to小于等于)
                    }
                }
            }
        }
    }
}
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.0,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                }
            }
        ]
    }
}

条件查询-全文检索

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match":{//类似于模糊查询
            "title": "小米手机"
        }
    }
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.21072102,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 0.21072102,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.21072102,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                }
            }
        ]
    }
}

条件查询-完全匹配

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match_phrase":{//类似于like匹配
            "title": "小米手机"
        }
    }
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.92407095,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 0.92407095,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}

条件查询-高亮查询显示

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "query":{
        "match":{
            "title": "手机"
        }
    },
    "highlight":{
        "fields":{
            "title":{}
        }
    }
}
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.21072102,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "lGmdl30BIh2p6feOh7Q0",
                "_score": 0.21072102,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                },
                "highlight": {
                    "title": [
                        "小米<em>手</em><em>机</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.21072102,
                "_source": {
                    "title": "华为手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 4999.0
                },
                "highlight": {
                    "title": [
                        "华为<em>手</em><em>机</em>"
                    ]
                }
            }
        ]
    }
}

条件查询-聚合查询(aggs)-分组(terms)

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "aggs":{//聚合操作
        "price_group":{//自定义以price分组的名称
            "terms":{//分组
                "field":"price"//分组字段
            }
        }
    },
    "size":0//不显示原始数据
}
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999.0,
                    "doc_count": 1
                },
                {
                    "key": 4999.0,
                    "doc_count": 1
                }
            ]
        }
    }
}

条件查询-聚合查询(aggs)-求平均(avg)

GET  http://127.0.0.1:9200/shopping/_search

//Body-JSON
{
    "aggs":{//聚合操作
        "price_avg":{//自定义以price求平均的名称
            "avg":{//平均值
                "field":"price"//求平均字段
            }
        }
    },
    "size":0//不显示原始数据
}
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 4499.0
        }
    }
}

映射关系

创建user索引

PUT  http://127.0.0.1:9200/user

创建user结构信息

PUT  http://127.0.0.1:9200/user/_mapping

//Body-JSON
{
    "properties":{
        "name":{
            "type":"text",//可被分词
            "index":true
        },
        "sex":{
            "type":"keyword",//完整匹配
            "index":true
        },
        "tel":{
            "type":"keyword",//完整匹配
            "index":false//不能以tel为索引查询
        }
    }
}
{
    "acknowledged": true
}

创建文档至user索引

PUT  http://127.0.0.1:9200/user/_create/1001

//Body-JSON
{
    "name":"小米",
    "sex":"男性",
    "tel":"1234"
 }
{
    "_index": "user",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

可多创建几个文档用于验证

全查询验证

GET  http://127.0.0.1:9200/user/_search

//Body-JSON
{
    "query":{
        "match":{
            "name":"小"
        }
    }
}
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.81427324,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.81427324,
                "_source": {
                    "name": "小米",
                    "sex": "男性",
                    "tel": "1234"
                }
            }
        ]
    }
}
//Body-JSON
{
    "query":{
        "match":{
            "sex":"男"
        }
    }
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}
//Body-JSON
{
    "query":{
        "match":{
            "sex":"男性"
        }
    }
}
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.4700036,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.4700036,
                "_source": {
                    "name": "小米",
                    "sex": "男性",
                    "tel": "1234"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1003",
                "_score": 0.4700036,
                "_source": {
                    "name": "bb",
                    "sex": "男性",
                    "tel": "1234"
                }
            }
        ]
    }
}
//Body-JSON
{
    "query":{
        "match":{
            "tel":"1234"
        }
    }
}
{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                "index_uuid": "2UQ0mzbSRBiG5o2rp62VGw",
                "index": "user"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "user",
                "node": "iKoe7VOzRBiSdEwtJICC_Q",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                    "index_uuid": "2UQ0mzbSRBiG5o2rp62VGw",
                    "index": "user",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Cannot search on field [tel] since it is not indexed."
                    }
                }
            }
        ]
    },
    "status": 400
}

文档分析

测试分析器

GET  http://127.0.0.1:9200/_analyze

//Body-JSON
{
    "analyzer": "standard",
    "text": "Text to analyze"
}
{
    "tokens": [
        {
            "token": "text",
            "start_offset": 0,
            "end_offset": 4,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "to",
            "start_offset": 5,
            "end_offset": 7,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "analyze",
            "start_offset": 8,
            "end_offset": 15,
            "type": "<ALPHANUM>",
            "position": 2
        }
    ]
}

IK分词器

GET  http://127.0.0.1:9200/_analyze

ik_max_word

//Body-JSON
{
    "text": "中国人",
    "analyzer":"ik_max_word"
}
{
    "tokens": [
        {
            "token": "中国人",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中国",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "国人",
            "start_offset": 1,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 2
        }
    ]
}

ik_smart

//Body-JSON
{
    "text": "中国人",
    "analyzer":"ik_smart"
}
{
    "tokens": [
        {
            "token": "中国人",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        }
    ]
}

拓展字典

//Body-JSON
{
    "text":"弗雷尔卓德",
    "analyzer":"ik_max_word"
}
{
    "tokens": [
        {
            "token": "弗",
            "start_offset": 0,
            "end_offset": 1,
            "type": "CN_CHAR",
            "position": 0
        },
        {
            "token": "雷",
            "start_offset": 1,
            "end_offset": 2,
            "type": "CN_CHAR",
            "position": 1
        },
        {
            "token": "尔",
            "start_offset": 2,
            "end_offset": 3,
            "type": "CN_CHAR",
            "position": 2
        },
        {
            "token": "卓",
            "start_offset": 3,
            "end_offset": 4,
            "type": "CN_CHAR",
            "position": 3
        },
        {
            "token": "德",
            "start_offset": 4,
            "end_offset": 5,
            "type": "CN_CHAR",
            "position": 4
        }
    ]
}

目录:elasticsearch-7.8.1\plugins\elasticsearch-analysis-ik-7.8.1\config\custom.dic

弗雷尔卓德

目录:elasticsearch-7.8.1\plugins\elasticsearch-analysis-ik-7.8.1\config\IKAnalyzer.cfg.xml

<entry key=“ext_dict”>custom.dic</entry>

重启elasticsearch

//Body-JSON
{
    "text":"弗雷尔卓德",
    "analyzer":"ik_max_word"
}
{
    "tokens": [
        {
            "token": "弗雷尔卓德",
            "start_offset": 0,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 0
        }
    ]
}

自定义分词器-创建

PUT  http://127.0.0.1:9200/my_index

//Body-JSON
{
    "settings": {
        //自定义分析器
        "analysis": {
            //字符过滤器
            "char_filter": {
                "&_to_and": {//自定义名称
                    "type": "mapping",
                    "mappings": ["&=> and "]//将&转换为and
                }
            },
            //词单元过滤器
            "filter": {
                "my_stopwords": {//自定义名称
                    "type": "stop",
                    "stopwords": ["the", "a"]//将the、a过滤
                }
            },
            //分词器
            "analyzer": {
                "my_analyzer": {
                    "type": "custom",//自定义类型名称
                    "char_filter": ["html_strip", "&_to_and"],//字符过滤器
                    "tokenizer": "standard",//token
                    "filter": ["lowercase", "my_stopwords"]//过滤器
                }
            }
        }
    }
}
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "my_index"
}

自定义分词器-验证

GET  http://127.0.0.1:9200/my_index/_analyze

//Body-JSON
{
    "text":"The quick & brown fox",
    "analyzer": "my_analyzer"
}
{
    "tokens": [
        {
            "token": "quick",
            "start_offset": 4,
            "end_offset": 9,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "and",
            "start_offset": 10,
            "end_offset": 11,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "brown",
            "start_offset": 12,
            "end_offset": 17,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "fox",
            "start_offset": 18,
            "end_offset": 21,
            "type": "<ALPHANUM>",
            "position": 4
        }
    ]
}

文档控制(_seq_no、_primary_term )

POST  http://127.0.0.1:9200/shopping/_update/1001?if_seq_no=0&if_primary_term=1

seq_no和primary_term值需要大于当前值(文档主键查询获得)

//Body-JSON
{
    "doc":{
        "title":"华为手机123"
    }
 }
//更新成功
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
//更新失败
{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[1001]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]",
                "index_uuid": "kdymKbh9Sw2RwG9-65O3iA",
                "shard": "0",
                "index": "shopping"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1001]: version conflict, required seqNo [0], primary term [1]. current document has seqNo [1] and primary term [1]",
        "index_uuid": "kdymKbh9Sw2RwG9-65O3iA",
        "shard": "0",
        "index": "shopping"
    },
    "status": 409
}

文档控制(_version )

POST  http://127.0.0.1:9200/shopping/_doc/1001?version=3&version_type=external

version值需要大于当前值(文档主键查询获得)

//Body-JSON
{
    "doc":{
        "title":"测试手机"
    }
 }
//更新成功
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
//更新失败
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

集群部署

集群-状态查询

GET  http://localhost:11001/_cluster/health

{
    "cluster_name": "my-application",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 3,
    "number_of_data_nodes": 3,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
}

集群-创建副本

PUT  http://localhost:11001/user

//Body-JSON
{
    "settings" : {
        "number_of_shards" : 3,//分片数量
        "number_of_replicas" : 1//副本数量
    }
}
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}

集群-查询副本情况

GET  http://localhost:11001/user

{
    "user": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1638932556920",
                "number_of_shards": "3",
                "number_of_replicas": "1",
                "uuid": "xcbNeijlRiubTwBIlxTP7w",
                "version": {
                    "created": "7080199"
                },
                "provided_name": "user"
            }
        }
    }
}

集群-水平扩容

副本为1

在这里插入图片描述

PUT  http://localhost:11001/user/_settings

//Body-JSON
{
    "number_of_replicas" : 2//设置副本数为2
}
{
    "acknowledged": true
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值