elasticsearch

简称ES: 是一个开源的高扩展的分布式全文搜索引擎
es是面向文档型数据库,一条数据在这里就是一个文档

索引: (数据库)
类型: (表--已删除)
文档: (行)
字段: (列)

倒排索引:
正排索引:

//添加索引(新建一个数据库)
http://127.0.0.1:9200/goods   PUT请求

返回:
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "goods"
}

PUT 两次操作返回结果是一样
POST  两次操作返回结果不是一样

//获取索引
http://127.0.0.1:9200/goods  GET请求

{
    "goods": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "goods",
                "creation_date": "1628603743375",
                "number_of_replicas": "1",
                "uuid": "4cDVsU8aQL62iYxlELPM_w",
                "version": {
                    "created": "7140099"
                }
            }
        }
    }
}
//获取所有索引详细信息
http://127.0.0.1:9200/_cat/indices?v
//删除索引
http://127.0.0.1:9200/goods DELETE请求
{
    "acknowledged": true
}


//添加数据
http://127.0.0.1:9200/goods/_doc/1001    POST请求  (不能用PUT)
http://127.0.0.1:9200/goods/_create/1001    POST请求

请求数据:
{
    "title":"小米手机",
    "category":"小米",
    "price":"3999.90",
    
}

返回:
{
    "_index": "goods",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

//查询数据
http://127.0.0.1:9200/goods/_doc/1001  GET 
{
    "_index": "goods",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true, //表示找到了
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "price": "3999.90"
    }
}

//获取所有文档数据
http://127.0.0.1:9200/goods/_search  GET 

{
    "took": 23, //耗费时间 毫秒为单位
    "timed_out": false, //是否超时
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "goods",
                "_type": "_doc",
                "_id": "XgtiMHsBqmDLeiGrn7BB",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "price": "3999.90"
                }
            },
            {
                "_index": "goods",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "price": "3999.90"
                }
            }
        ]
    }
}

//完全覆盖修改
http://127.0.0.1:9200/goods/_doc/1001   PUT
请求:
{
    "title":"小米手机",
    "category":"小米",
    "price":"4999.90",
    
}

返回:
{
    "_index": "goods",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

//局部覆盖修改
http://127.0.0.1:9200/goods/_update/1001   POST
请求:
{
    "doc":{
        "title":"华为手机"
    }
}

返回:
{
    "_index": "goods",
    "_type": "_doc",
    "_id": "1001",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

//删除数据
http://127.0.0.1:9200/goods/_doc/1001  DELETE请求

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

//条件查询(参数放url中)
http://127.0.0.1:9200/goods/_search?q=category:小米
{
    "took": 509,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.26706278,
        "hits": [
            {
                "_index": "goods",
                "_type": "_doc",
                "_id": "XgtiMHsBqmDLeiGrn7BB",
                "_score": 0.26706278,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "price": "3999.90"
                }
            }
        ]
    }
}
//条件查询-参数放请求体
http://127.0.0.1:9200/goods/_search
请求:
{
    "query":{ 
        "match":{
            "category":"小米"
        }
    }
}
或
全部查询
{
    "query":{ 
        "match_all":{
            
        }
    }
}
或
多条件查询
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "category":"小米"
                    },    
                }
            ],
            "filter":{
                "range":{
                    "price":{
                        "lt" : 5000
                    }
                }
            }
        }
    }
}
或
完全匹配(不做分词匹配)
{
    "query":{ 
        "match_phrase":{
            "category":"小米"
        }
    },
    "highlight":{
        "fields":{
            "category":{}
        }
    }
}

返回:
{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "goods",
                "_type": "_doc",
                "_id": "XgtiMHsBqmDLeiGrn7BB",
                "_score": 0.5753642,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "price": "3999.90"
                }
            }
        ]
    }
}

//映射(其实就是定规则)
http://127.0.0.1:9200/user/_mapping  PUT
请求:
{
    "properties": {
        "name": {
            "type": "text",
            "index": true
        },
        "sex": {
            "type": "keyword",
            "index": true
        },
        "mobile": {
            "type": "keyword",
            "index": false
        }
    }
}
text- 文本类型可以被全文检索
keyword -是一个关键字  不可以全文检索 必须精准
index - false  没有索引 不支持查询

 

posted @ 2021-08-10 23:53  FinnYY  阅读(51)  评论(0编辑  收藏  举报