ElasticSearch学习(二)——索引、文档简单操作 下

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: ElasticSearch学习(二)——索引、文档简单操作 下

1.3 修改

1.3.1 全局修改

在Postman中发起PUT请求:http://127.0.0.1:9200/index_name/_doc/id

例子:

修改内容:

{
    "title": "华为手机",
    "category": "HuaWei",
    "images": "http://xxx.com/xm.jpg",
    "price": 4000.00
}

响应:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 4,
    "result": "updated", // 修改
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 2
}

1.3.2 局部修改

不可以使用PUT请求,要使用POST请求。

在Postman中发起POST请求:http://127.0.0.1:9200/index_name/_update/id

例子:

请求体内容:

{
    "doc":{
        "price":5000.00
    }
}

响应:

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

1.4 删除

在Postman中发起DELETE请求:http://127.0.0.1:9200/index_name/_update/id

例子:

响应:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 6,
    "result": "deleted", // 删除成功
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 2
}

如果删除一个不存在的文档:

{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "not_found", // 未找到
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 2
}

3. 映射关系mapping

创建一个user索引

设置index映射信息:

请求体:

{
    "properties":{
        "name":{// 字段名称
            "type":"text", // 字段类型。text:文本,可以分词
            "index":true // index:true 表示这个字段可以通过索引查询。
        },
        "sex":{
            "type":"keyword", // keyword 表示不可以分词,必须完整匹配
            "index":true
        },
        "tel":{
             "type":"keyword",
            "index":false // 不可被索引,即不可被查询
        }
    }
}

获取index映射信息:

响应:

{
    "user": {
        "mappings": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "sex": {
                    "type": "keyword"
                },
                "tel": {
                    "type": "keyword",
                    "index": false
                }
            }
        }
    }
}

验证映射信息:

在user中创建一个文档

请求体:

{
        "name": "小米",
        "sex": "男的",
        "tel": "1111"
}

查询该文档

根据name查询:

请求体:

{
    "query":{
        "match":{
            "name":"小" // 因为name为text类型,是支持分词,也就是全文检索的
        }
    }
}

响应:

{
    "took": 606,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.2876821,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "1111"
                }
            }
        ]
    }
}

根据sex查询:

请求体:

{
    "query":{
        "match":{
            "sex":"男" // sex类型为keyword,只支持完全查询,不支持全文检索
        }
    }
}

响应:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

根据tel查询:

请求体:

{
    "query":{
        "match":{
            "tel":"1111" // tel的index:false 不可被查询 所以会报错
        }
    }
}

会报错:

"reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
// "reason": "创建查询失败:无法在[tel]字段上搜索,因为它没有被索引",

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
9天前
|
自然语言处理 测试技术 网络安全
ElasticSearch7最新实战文档-附带logstash同步方案
ElasticSearch7最新实战文档-附带logstash同步方案
16 0
|
11天前
|
Kubernetes 关系型数据库 MySQL
实时计算 Flink版产品使用合集之在Kubernetes(k8s)中同步MySQL变更到Elasticsearch该怎么操作
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
19天前
|
存储 缓存 自然语言处理
Elasticsearch框架学习的难点和重点有哪些
Elasticsearch是基于Lucene的开源搜索引擎,广泛应用于全文检索和日志分析。学习重点包括理解节点、集群、索引、分片和副本等基本概念,掌握数据索引、查询DSL、聚合和性能优化。倒排索引和分词器是全文搜索的核心,集群管理和监控对于稳定性至关重要。实践中需根据数据量和查询模式优化分片策略,利用缓存提升搜索性能。学习Elasticsearch要结合实际项目,关注官方文档和社区资源。【5月更文挑战第6天】
|
19天前
|
Java API
Java操作elasticsearch
Java操作elasticsearch
18 0
|
19天前
|
运维 安全 API
Elasticsearch 悬挂索引解析与管理指南
Elasticsearch 悬挂索引解析与管理指南
35 7
|
19天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
26 4
|
19天前
|
存储 数据处理 索引
Elasticsearch 8.X 小技巧:使用存储脚本优化数据索引与转换过程
Elasticsearch 8.X 小技巧:使用存储脚本优化数据索引与转换过程
38 6
|
19天前
|
人工智能 架构师 开发者
大模型时代,该如何更好的学习 Elasticsearch?
大模型时代,该如何更好的学习 Elasticsearch?
19 0
|
19天前
|
存储 安全 数据处理
Elasticsearch 为什么会产生文档版本冲突?如何避免?
Elasticsearch 为什么会产生文档版本冲突?如何避免?
20 0
|
19天前
|
安全 Python
Elasticsearch 删除重复文档实现方式,你知道几个?
Elasticsearch 删除重复文档实现方式,你知道几个?
12 0