ElasticSearch 查询实践(上)

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: ElasticSearch 查询实践

一.文档批量操作


这里多个文档是指,批量操作多个文档,搜索查询文档将在之后的章节讲解


1.批量获取文档数据


批量获取文档数据是通过_mget的API来实现的


(1)在URL中不指定index和type


请求方式:GET


请求地址:_mget


功能说明 : 可以通过ID批量获取不同index和type的数据


请求参数:


  • docs : 文档数组参数
  • _index : 指定index
  • _type : 指定type
  • _id : 指定id
  • _source : 指定要查询的字段


GET _mget 
{ 
"docs": [ 
{ 
"_index": "es_db", 
"_type": "_doc", 
"_id": 1 
}, 
{ 
"_index": "es_db", 
"_type": "_doc", 
"_id": 2 
} 
] 
}


响应结果如下:


{
  "docs" : [
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "张三",
        "sex" : 1,
        "age" : 25,
        "address" : "广州天河公园",
        "remark" : "java developer"
      }
    },
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "李四",
        "sex" : 1,
        "age" : 28,
        "address" : "广州荔湾大厦",
        "remark" : "java assistant"
      }
    }
  ]
}


(2)在URL中指定index


请求方式:GET


请求地址:/{{indexName}}/_mget


功能说明 : 可以通过ID批量获取不同index和type的数据请求参数:


docs : 文档数组参数

_index : 指定index

_type : 指定type

_id : 指定id

_source : 指定要查询的字段


GET /user/_mget 
{ 
"docs": [ 
{ 
"_type":"_doc", 
"_id": 3 
}, 
{ 
"_type":"_doc", 
"_id": 4 
} 
] 
} 


返回结果(由于我没有创建 /user 索引所以返回 index_not_found_exception):


#! Deprecation: [types removal] Specifying types in multi get requests is deprecated.
{
  "docs" : [
    {
      "_index" : "user",
      "_type" : "_doc",
      "_id" : "3",
      "error" : {
        "root_cause" : [
          {
            "type" : "index_not_found_exception",
            "reason" : "no such index [user]",
            "resource.type" : "index_expression",
            "resource.id" : "user",
            "index_uuid" : "_na_",
            "index" : "user"
          }
        ],
        "type" : "index_not_found_exception",
        "reason" : "no such index [user]",
        "resource.type" : "index_expression",
        "resource.id" : "user",
        "index_uuid" : "_na_",
        "index" : "user"
      }
    },
    {
      "_index" : "user",
      "_type" : "_doc",
      "_id" : "4",
      "error" : {
        "root_cause" : [
          {
            "type" : "index_not_found_exception",
            "reason" : "no such index [user]",
            "resource.type" : "index_expression",
            "resource.id" : "user",
            "index_uuid" : "_na_",
            "index" : "user"
          }
        ],
        "type" : "index_not_found_exception",
        "reason" : "no such index [user]",
        "resource.type" : "index_expression",
        "resource.id" : "user",
        "index_uuid" : "_na_",
        "index" : "user"
      }
    }
  ]
}


(3)在URL中指定index和type


请求方式:GET


请求地址:/{{indexName}}/{{typeName}}/_mget


功能说明 : 可以通过ID批量获取不同index和type的数据


请求参数:


docs : 文档数组参数

_index : 指定index

_type : 指定type

_id : 指定id

_source : 指定要查询的字段


GET /es_db/_doc/_mget 
{
  "docs": [ 
{ 
"_id": 1 
}, 
{ 
"_id": 2 
} 
] 
} 


返回结果


#! Deprecation: [types removal] Specifying types in multi get requests is deprecated.
{
  "docs" : [
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "张三",
        "sex" : 1,
        "age" : 25,
        "address" : "广州天河公园",
        "remark" : "java developer"
      }
    },
    {
      "_index" : "es_db",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "李四",
        "sex" : 1,
        "age" : 28,
        "address" : "广州荔湾大厦",
        "remark" : "java assistant"
      }
    }
  ]
}


2.批量操作文档数据


批量对文档进行写操作是通过_bulk的API来实现的


请求方式:POST


请求地址:_bulk


请求参数:通过_bulk操作文档,一般至少有两行参数(或偶数行参数)


第一行参数为指定操作的类型及操作的对象


(index,type和id)


第二行参数才是操作的数据


参数类似于:


{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}} 
{"field1":"value1", "field2":"value2"} 


actionName:表示操作类型,主要有create,index,delete和update


(1)批量创建文档create


POST _bulk 
{"create":{"_index":"article", "_type":"_doc", "_id":3}} 
{"id":3,"title":"李白","content":"李白666","tags":["java", "面向对 
象"],"create_time":1554015482530} 
{"create":{"_index":"article", "_type":"_doc", "_id":4}} 
{"id":4,"title":"李白","content":"李白NB","tags":["java", "面向对 
象"],"create_time":1554015482530} 


返回结果


{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Invalid UTF-8 start byte 0xb1\n at [Source: (byte[])\"POST /_bulk?pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0.0.1\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":3}}\n{\"id\":3,\"title\":\"李白\",\"content\":\"李白666\",\"tags\":[\"java\", \"面向对 \n象\"],\"create_time\":1554015482530}\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":4}}\n{\"id\":4,\"title\":\"李白\",\"c\"[truncated 89 bytes]; line: 1, column: 3]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Invalid UTF-8 start byte 0xb1\n at [Source: (byte[])\"POST /_bulk?pretty=true HTTP/1.1\r\nx-forwarded-for: 127.0.0.1\r\nx-forwarded-port: 61337\r\nx-forwarded-proto: http\r\nx-forwarded-host: localhost:5601\r\ncontent-type: application/json\r\nhost: localhost\r\ntransfer-encoding: chunked\r\nConnection: close\r\n\r\n14d\r\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":3}}\n{\"id\":3,\"title\":\"李白\",\"content\":\"李白666\",\"tags\":[\"java\", \"面向对 \n象\"],\"create_time\":1554015482530}\n{\"create\":{\"_index\":\"article\", \"_type\":\"_doc\", \"_id\":4}}\n{\"id\":4,\"title\":\"李白\",\"c\"[truncated 89 bytes]; line: 1, column: 3]"
  },
  "status" : 400
}


(2)普通创建或全量替换index


POST _bulk 
{"index":{"_index":"article", "_type":"_doc", "_id":3}} 
{"id":3,"title":"杜甫(一)","content":"杜甫666","tags":["java", "面向对象"],"create_time":1554015482530} 
{"index":{"_index":"article", "_type":"_doc", "_id":4}}
{"id":4,"title":"杜甫(二)", "content":"杜甫NB","tags":["java", "面向对象"],"create_time":1554015482530} 


**如果原文档不存在,则是创建 **


**如果原文档存在,则是替换(全量修改原文档) **

返回结果


#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 651,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}


(3)批量删除delete


POST _bulk 
{"delete":{"_index":"article", "_type":"_doc", "_id":3}} 
{"delete":{"_index":"article", "_type":"_doc", "_id":4}} 


返回结果


#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 32,
  "errors" : false,
  "items" : [
    {
      "delete" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "delete" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 3,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}


(4)批量修改update


POST _bulk 
{"update":{"_index":"article", "_type":"_doc", "_id":3}} 
{"doc":{"title":"ES大法必修内功"}} 
{"update":{"_index":"article", "_type":"_doc", "_id":4}} 
{"doc":{"create_time":1554018421008}} 


返回结果


#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
  "took" : 3,
  "errors" : true,
  "items" : [
    {
      "update" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "3",
        "status" : 404,
        "error" : {
          "type" : "document_missing_exception",
          "reason" : "[_doc][3]: document missing",
          "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q",
          "shard" : "0",
          "index" : "article"
        }
      }
    },
    {
      "update" : {
        "_index" : "article",
        "_type" : "_doc",
        "_id" : "4",
        "status" : 404,
        "error" : {
          "type" : "document_missing_exception",
          "reason" : "[_doc][4]: document missing",
          "index_uuid" : "uTqmplLRR3CpwQXWtAvJ7Q",
          "shard" : "0",
          "index" : "article"
        }
      }
    }
  ]
}



相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
6月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
254 20
|
存储 关系型数据库 MySQL
浅谈Elasticsearch的入门与实践
本文主要围绕ES核心特性:分布式存储特性和分析检索能力,介绍了概念、原理与实践案例,希望让读者快速理解ES的核心特性与应用场景。
525 13
|
8月前
|
存储 运维 监控
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
中信银行信用卡中心每日新增日志数据 140 亿条(80TB),全量归档日志量超 40PB,早期基于 Elasticsearch 构建的日志云平台,面临存储成本高、实时写入性能差、文本检索慢以及日志分析能力不足等问题。因此使用 Apache Doris 替换 Elasticsearch,实现资源投入降低 50%、查询速度提升 2~4 倍,同时显著提高了运维效率。
328 3
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
|
9月前
|
数据采集 人工智能 运维
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
本文介绍了阿里云 Elasticsearch 推出的创新型 AI 搜索方案
490 3
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
|
8月前
|
数据采集 人工智能 运维
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
本文介绍了阿里云 Elasticsearch 推出的创新型 AI 搜索方案。
695 5
|
人工智能 自然语言处理 搜索推荐
阿里云Elasticsearch AI搜索实践
本文介绍了阿里云 Elasticsearch 在AI 搜索方面的技术实践与探索。
19652 21
|
11月前
|
存储 JSON 监控
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
386 4
|
12月前
|
JSON 自然语言处理 算法
ElasticSearch基础2——DSL查询文档,黑马旅游项目查询功能
DSL查询文档、RestClient查询文档、全文检索查询、精准查询、复合查询、地理坐标查询、分页、排序、高亮、黑马旅游案例
ElasticSearch基础2——DSL查询文档,黑马旅游项目查询功能
|
11月前
|
自然语言处理 搜索推荐 Java
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(一)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图
208 0
|
11月前
|
存储 自然语言处理 搜索推荐
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
168 0