首页 > 其他分享 >[Elasticsearc] Elasticsearch 初见

[Elasticsearc] Elasticsearch 初见

时间:2024-02-06 16:22:58浏览次数:25  
标签:index shopping 请求 version 初见 Elasticsearc Elasticsearch id 9200

Elasticsearch 初见

启动

双击 bin 目录下的 elasticsearch.bat 文件,等待终端运行成功

索引的增删改查

  • 增(PUT)

    postman 发送请求 PUT 请求:http://127.0.0.1:9200/shopping

    返回结果:

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

    因为 PUT 请求说明接口是幂等性的,所以如果再次进行相同请求,会返回以下数据表示索引已存在:

    {
        "error": {
            "root_cause": [
                {
                    "type": "resource_already_exists_exception",
                    "reason": "index [shopping/uQ946aIJSwWwaZNTPgPwEw] already exists",
                    "index_uuid": "uQ946aIJSwWwaZNTPgPwEw",
                    "index": "shopping"
                }
            ],
            "type": "resource_already_exists_exception",
            "reason": "index [shopping/uQ946aIJSwWwaZNTPgPwEw] already exists",
            "index_uuid": "uQ946aIJSwWwaZNTPgPwEw",
            "index": "shopping"
        },
        "status": 400
    }
    
  • 删(DELETE)

    postman 发送请求 DELETE 请求:http://127.0.0.1:9200/shopping

    返回数据如下,表示删除成功

    {
        "acknowledged": true
    }
    
  • 查(GET)

    postman 发送 GET 请求:http://127.0.0.1:9200/shopping

    会返回 shopping 这个索引的相关信息:

    {
        "shopping": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "routing": {
                        "allocation": {
                            "include": {
                                "_tier_preference": "data_content"
                            }
                        }
                    },
                    "number_of_shards": "1",
                    "provided_name": "shopping",
                    "creation_date": "1707056309923",
                    "number_of_replicas": "1",
                    "uuid": "5i7qBmHNSvq5NubfCPyElg",
                    "version": {
                        "created": "8500008"
                    }
                }
            }
        }
    }
    

    postman 发送 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 dataset.size
    yellow open   shopping 5i7qBmHNSvq5NubfCPyElg   1   1          0            0       227b           227b         227b
    

文档的创建

文档需要在索引的基础上进行创建,所以可以看到,创建文档的请求地址可以是 http://127.0.0.1:9200/shopping/_doc

_doc 就代表文档。等同于数据库中创建数据。

postman 发送 post 请求:http://127.0.0.1:9200/shopping/_doc

但是这么发送请求会报错,因为没有请求体,也就是具体的需要创建的数据:

{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "request body is required"
            }
        ],
        "type": "parse_exception",
        "reason": "request body is required"
    },
    "status": 400
}

所以在用 post 发送请求的时候,需要带上请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "https://www.mi.com/",
    "price": 3999.0
}

返回数据创建成功数据:

{
    "_index": "shopping",
    "_id": "QxKDdI0B8aQTV-f6ZJpd",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

但是可以看到上述的返回数据中,_id 是随机的,这是 ES 自动随机生成的,且因为用的是 post 请求,所以接口并不是幂等接口,每次 post 相同接口都可以成功创建文档返回数据,但是返回的 _id 值不同。

当然创建文档也可以自定义 id,使之不适用 ES 自动创建的 id,则可发送类似请求:

http://127.0.0.1:9200/shopping/_doc/100001

返回的数据中,id 就是自定义的 id 了:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

当然,如果再次发送相同 id 的请求,版本会增加,且操作会变为更新操作:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

这就是所谓的全量更新。使用 put 请求也可以。

创建文档的请求也同样可以使用另外一种请求方式:http://127.0.0.1:9200/shopping/_create/100002,结果相同。

但如果使用 _create 再次发送相同请求,则会提示已经存在当前 id 数据,文档创建失败:

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[100002]: version conflict, document already exists (current version [1])",
                "index_uuid": "5i7qBmHNSvq5NubfCPyElg",
                "shard": "0",
                "index": "shopping"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[100002]: version conflict, document already exists (current version [1])",
        "index_uuid": "5i7qBmHNSvq5NubfCPyElg",
        "shard": "0",
        "index": "shopping"
    },
    "status": 409
}

文档的查询

文档的查询也很简单,以刚刚创建文档的数据为例,根据主键(id)进行查询,请求地址:http://127.0.0.1:9200/shopping/_doc/100001

返回结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 2,
    "_seq_no": 2,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "https://www.mi.com/",
        "price": 3999.0
    }
}

可以看到 fount 属性为 true,表示查询成功,_source 就表示查询到的数据。

当然,如果查询一个不存在的 id 值,比如发送请求:http://127.0.0.1:9200/shopping/_doc/100012

返回结果:

{
    "_index": "shopping",
    "_id": "100012",
    "found": false
}

fount 属性为 false,表示没有命中请求。

除了使用 id 进行查询之外,还可以使用全数据查询。发送请求:http://127.0.0.1:9200/shopping/_search

返回结果:

{
    "took": 63,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_id": "QxKDdI0B8aQTV-f6ZJpd",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            },
            {
                "_index": "shopping",
                "_id": "100001",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            },
            {
                "_index": "shopping",
                "_id": "100002",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            }
        ]
    }
}

如上,took 是查询的时间,单位为毫秒,timed_out 表示是否超时,hits 就是查询到的数据,一共有三条数据,索引是什么,id 是什么,数据是什么。

文档的修改

全量覆盖

postman 发出一个 PUT 请求:http://127.0.0.1:9200/shopping/_doc/100001,并修改请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "https://www.mi.com/",
    "price": 9999.0
}

返回更新成功结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 2
}

再次查看内容已经被成功修改。

局部修改

局部修改因为不是幂等性的所以需要使用 POST 请求,请求地址为:http://127.0.0.1:9200/shopping/_update/100001

请求体为具体需要修改的属性,如:

{
    "doc": {
        "title": "华为手机"
    }
}

返回结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 2
}

再次查看数据,发送请求:http://127.0.0.1:9200/shopping/_doc/100001

看到数据已经被修改:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 5,
    "_seq_no": 6,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "title": "华为手机",
        "category": "小米",
        "images": "https://www.mi.com/",
        "price": 9999.0
    }
}

文档的删除

删除比较简单,只需要发送 DELETE 请求,请求地址:http://127.0.0.1:9200/shopping/_doc/100001

返回删除成功数据:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 6,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 2
}

标签:index,shopping,请求,version,初见,Elasticsearc,Elasticsearch,id,9200
From: https://www.cnblogs.com/knqiufan/p/18009914

相关文章

  • [Elasticsearch] Elasticsearch 启动访问报错问题
    Elasticsearch启动访问报错问题产生的问题与解决方案环境:Windows10ES版本:8.12.0现象:双击elasticsearch.bat文件启动后,访问http://127.0.0.1:9200地址报了一个错误:receivedplaintexthttptrafficonanhttpschannel,closingconnectionNetty4HttpChannel.........
  • Python elasticsearch-py类库基础用法
    实践环境https://pypi.org/project/elasticsearch/pipinstallelasticsearch==7.6.0离线安装包及依赖包下载地址:https://files.pythonhosted.org/packages/f5/71/45d36a8df68f3ebb098d6861b2c017f3d094538c0fb98fa61d4dc43e69b9/urllib3-1.26.2-py2.py3-none-any.whl#sha256=d8ff9......
  • ElasticSearch使用(从入门到放弃)
    概述什么是ElasticSearch?也可简称为ES,顾名思义,可伸缩搜索,主要用来做检索的,再看看官网解释。Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎。作为ElasticStack的核心,Elasticsearch会集中存储您的数据,让您飞快完成搜索,微调相关性,进行强大的分析,并轻松缩放......
  • 【ElasticSearch】脚本条件
    RESTAPI{"query":{"bool":{"must":[{"term":{"sqStatus":{"value":3,"boost":1}......
  • elasticsearch 查询:聚合查询
    新建索引:POST/index/_search{"aggs":"名字":{"agg_type":{"属性":"值"}}} 1.去重计数查询去重计数,即Cardinality先将返回的文档中的field进行去重,......
  • elasticsearch 查询:经纬度查询
    geo_distance:直线距离检索方式geo_bounding_bos:以两个点确定一个矩形,获取在矩形内的全部数据geo_polygon:以多个点,确定一个多边形,获取多边形内的全部数据#测试geo--geo_distancePOST/king_test_map/_search{"query":{"geo_distance":{"location":{......
  • Easy-Es操作Elasticsearch
    目录1Easy-Es1.1简介1.2MySQL与Easy-Es语法对比1.3集成及配置1.3.1pom.xml1.3.2配置1.4使用1.4.1注解的使用1.4.2EsMapper接口1.4.3简单搜索1.5使用案例1.5.1综合商品搜索1.5.2相关商品推荐1.5.3聚合搜索商品相关信息1Easy-Es使用过SpringData操作ES的小伙伴应......
  • elasticsearch 查询:term&terms
    1.term查询term查询:完全匹配查询,搜索前不会对关键字进行分词。只支持单个feild查询。不设置from,size。默认返回10条#测试--term查询POST/king_test_person/_search{"from":0,#limit?"size":20,#limitx,?"query":{"term":{......
  • elasticsearch 查询:match查询
    1.match查询match查询属于高层查询,会根据查询内容不一样,采用不同的查询方式。查询的内容如果是日期或者数值,会将你基于的字符串查询内容转换为日期或者数值对待;如果查询的内容是一个不能被分词的内容(keyword),match查询不会将指定的关键字分词;如果查询内容是一个可以被分词的......
  • elasticsearch 查询:其他查询prefix&fuzzy&wildcard&range®exp
    1.prefix查询前缀查询,可以通过一个关键字去指定一个field的前缀,从而查到指定的文档。#测试--prefix查询POST/king_test_person/_search{"query":{"prefix":{"name":{"value":"张"}}}} 2. fuzzy查询模糊查询,......