美文网首页
elasticsearch简单操作

elasticsearch简单操作

作者: 念䋛 | 来源:发表于2022-05-14 15:20 被阅读0次

通过postman操作elasticsearch版本为7.8.0

添加索引

http://127.0.0.1:9200/shopping  put方式
{
    "acknowledged": true,  //操作成功
    "shards_acknowledged": true,  分片成功
    "index": "shopping"  //索引名称
}

不能重复的创建索引

{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [shopping/Qxdn6w1BQMKFE-sFgnsgLA] already exists", //提示已经存在
                "index_uuid": "Qxdn6w1BQMKFE-sFgnsgLA",
                "index": "shopping"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [shopping/Qxdn6w1BQMKFE-sFgnsgLA] already exists",
        "index_uuid": "Qxdn6w1BQMKFE-sFgnsgLA",
        "index": "shopping"
    },
    "status": 400
}

查看索引

http://127.0.0.1:9200/_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping Qxdn6w1BQMKFE-sFgnsgLA   1   1          0            0       208b           208b
_cat  查看
indexs 索引
?v title

字段的含义


image.png

创建数据

127.0.0.1:9200/shopping/_doc   post请求
body
{
 "title":"小米手机",
 "category":"小米",
 "images":"http://www.gulixueyuan.com/xm.jpg",
 "price":3999.00
}
结果
{
    "_index": "shopping",  //索引
    "_type": "_doc",  //文档
    "_id": "84RmwIAB1y2B1Y7rWCvM", //创建数据id  /shopping/_doc/1 规定id
    "_version": 1,
    "result": "created", //创建成功
    "_shards": { //分片
        "total": 2,  //分片总数
        "successful": 1,  //成功一条
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
如果没有创建mapping(字段),直接创建数据,会根据数据传入值默认设置字段类型 
如果后续创建数据,添加新的字段, 查询映射关系可以看到新增加的字段,
老数据不会看到新的字段

查看单个索引状态

127.0.0.1:9200/shopping
结果
{
    "shopping": { //索引名称
        "aliases": {}, //别名
        "mappings": { //字段映射
            "properties": {
                "category": { //字段名称
                    "type": "text", //字段类型
                    "fields": {  
                        "keyword": {
                            "type": "keyword", //keyword代表查询的时候 不能分词,
                                                         //创建数据中多余的字段,会自动的创建映射
                                                         //该字段的类型同时为text类型和keyword
                            "ignore_above": 256 //如果长度256,会被存储,但是不会索引
                                                              //例 长度如果为2   存储 01 , 012  条件为01时 只能查询01 
                                                              //012不能查询 条件为 012 不会查询到012
                        }
                    }
                },
                "images": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "price": {
                    "type": "float"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1652493495165",
                "number_of_shards": "1",   //主分片数量
                "number_of_replicas": "1", //副分片数量
                "uuid": "Qxdn6w1BQMKFE-sFgnsgLA", //索引唯一标识
                "version": { //索引版本
                    "created": "7080099"
                },
                "provided_name": "shopping" //索引名称
            }
        }
    }
}

查看文档

查看文档分两种通过id去查询,查看所有的文档,这里不包含高级查询
查看所有文档

127.0.0.1:9200/shopping/_search
{
    "took": 65,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,  //命中数量
            "relation": "eq"  // eq 表示计数准确, gte 表示计数不准确
        },
        "max_score": 1.0, //命中最大分数
        "hits": [ //结果集
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "84RmwIAB1y2B1Y7rWCvM",
                "_score": 1.0, //命中分钟 排序用
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00,
                    "name": "小米12"
                }
            }
        ]
    }
}

通过id查找

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

修改文档

修改文档和创建文档是一样的,修改需要提供id值,如果数据发生变化就会覆盖以前的数据  
127.0.0.1:9200/shopping/_doc/1   post请求
body
{
 "title":"小米手机",
 "category":"小米修改",
 "images":"http://www.gulixueyuan.com/xm.jpg",
 "price":3999.00
}
结果
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 2, //版本号修改为2
    "result": "updated", //修改
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

修改局部数据

http://127.0.0.1:9200/shopping/_update/1 post请求 
body
{ 
 "doc": {
 "price":3000.00
 } 
}
结果
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 3, //版本号变化
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

删除文档

文档的删除为逻辑删除
http://127.0.0.1:9200/shopping/_doc/1 delete请求
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted", //已删除
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}
再次查询的时候
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "found": false  //提示找不到
}

批量插入数据

127.0.0.1:9200/shopping/_doc/_bulk post请求
{"index": {"_id": "1"}}
{"title": "小米手机","category": "小米", "images": "http://www.gulixueyuan.com/xm.jpg","price": 4000.00}
{"index": {"_id": "2"}}
{"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 4000.00}

注意header:Content-Type:application/json
image.png

条件删除文档

http://127.0.0.1:9200/shopping/_delete_by_query post请求
{
    "query": {
        "match": {
            "price": 4000.00 //删除price 为4000的数据
        }
    }
}

创建映射1

127.0.0.1:9200/student/_mapping put请求
{
    "properties": {
        "name": {
            "type": "text", //可以分词查询
            "index": true   //是否索引
        },
        "sex": {
            "type": "text",
            "index": false
        },
        "age": {
            "type": "long",
            "index": false
        }
    }
}
结果
{
    "acknowledged": true 成功
}
字段名:任意填写,下面指定许多属性,例如:title、subtitle、images、price
type:类型,Elasticsearch 中支持的数据类型非常丰富,说几个关键的:
     String 类型,又分两种:
         text:可分词
         keyword:不可分词,数据会作为完整字段进行匹配
     Numerical:数值类型,分两类
        基本数据类型:long、integer、short、byte、double、float、half_float
        浮点数的高精度类型:scaled_float
     Date:日期类型
     Array:数组类型
     Object:对象
     index:是否索引,默认为 true,也就是说你不进行任何配置,所有字段都会被索引。
            true:字段会被索引,则可以用来进行搜索
            false:字段不会被索引,不能用来搜索
     store:是否将数据进行独立存储,默认为 false
           原始的文本会存储在_source 里面,默认情况下其他提取出来的字段都不是独立存储 
           的,是从_source 里面提取出来的。当然你也可以独立的存储某个字段,只要设置
           "store": true 即可,获取独立存储的字段要比从_source 中解析快得多,但是也会占用
           更多的空间,所以要根据实际业务需求来设置。
     analyzer:分词器,这里的 ik_max_word 即使用 ik 分词器,后面会学习

查看映射

127.0.0.1:9200/student/_mapping?pretty get请求
这里的返回值包括创建的映射和创建数据中的映射
例如 创建的映射只有name,创建数据的时候有name和sex,查询到的映射有name和sex,其中sex为默认的类型

创建映射2

http://127.0.0.1:9200/student1 put请求
body
{
    "settings": {},
    "mappings": { //请求路径中没有_mapping
        "properties": {
            "name": {
                "type": "text",
                "index": true
            },
            "sex": {
                "type": "text",
                "index": false
            },
            "age": {
                "type": "long",
                "index": false
            }
        }
    }
}

postman-json文件
git@gitee.com:zhangjijige/file.git
参考:尚硅谷-谷粒学院

相关文章

网友评论

      本文标题:elasticsearch简单操作

      本文链接:https://www.haomeiwen.com/subject/ghmpurtx.html