• Elasticsearch学习系列之多文档操作mget


    测试数据

    GET /library/books/1

    {
       "_index": "library",
       "_type": "books",
       "_id": "1",
       "_version": 1,
       "found": true,
       "_source": {
          "title": "Elasticsearch:the definitive guide",
          "name": {
             "first": "zachary",
             "last": "tong"
          },
          "publish_date": "2017-02-19",
          "price": "49.99"
       }
    }

    GET /library/books/2

    {
       "_index": "library",
       "_type": "books",
       "_id": "2",
       "_version": 1,
       "found": true,
       "_source": {
          "title": "Elasticsearch:the definitive guide",
          "name": {
             "first": "zachary",
             "last": "tong"
          },
          "publish_date": "2017-02-19",
          "price": "59.99"
       }
    }

    multi get

    多字段查询可以设置多个文档查询条件,每个查询条件在结构上都比较类似

    GET /_mget
    {
      
        "docs": [
          {
          "_index" : "library",
          "_type"  :  "books",
          "_id"    :  "1"
        },
        {
          "_index" : "library",
          "_type"  : "books",
          "_id"    : "2"
        }
        ]
      
      
    }

    当然,在查询条件中,body中_index字段也可以放在查询字符串中

    GET /library/_mget
    {
      
        "docs": [
          {
          
          "_type"  :  "books",
          "_id"    :  "1"
        },
        {
          
          "_type"  : "books",
          "_id"    : "2"
        }
        ]
      
      
    }

    对于type也是一样:

    GET /library/books/_mget
    {
      
        "docs": [
          {
          "_id"    :  "1"
        },
        {
          "_id"    : "2"
        }
        ]
    }

    如果索引和类型都放在查询URL中,那么字段ID就可以放在一个数组中:

    GET /library/books/_mget
    {
      "ids" : ["1","2"]
    }

    如果想要查询不通类型的相同ID,就需要指定类型名称

    GET /test/_mget/
    {
      "docs" : [
            {
                "_type":"typeA",
                "_id" : "1"
            },
            {
                "_type":"typeB",
                "_id" : "1"
            }
        ]
    }
    #这个例子不适用上面的测试数据

    Fields过滤

    fields过滤是获取指定的字段

    代码

    GET /_mget 
    {
      "docs" : [
          {
            "_index":"library",
            "_type" : "books",
            "_id"   : "1",
            "fields" : ["publish_date","price"]
          },
          {
            "_index":"library",
            "_type" : "books",
            "_id"   : "2",
            "fields" : ["publish_date","price"]
          }
        ]
      
    }

    结果

    {
       "docs": [
          {
             "_index": "library",
             "_type": "books",
             "_id": "1",
             "_version": 1,
             "found": true,
             "fields": {
                "publish_date": [
                   "2017-02-19"
                ],
                "price": [
                   "49.99"
                ]
             }
          },
          {
             "_index": "library",
             "_type": "books",
             "_id": "2",
             "_version": 1,
             "found": true,
             "fields": {
                "publish_date": [
                   "2017-02-19"
                ],
                "price": [
                   "59.99"
                ]
             }
          }
       ]
    }
  • 相关阅读:
    [工作札记]01: CS系统中分页控件的制作
    【非技术】试占新型肺炎的情况与发展趋势
    给培训学校讲解ORM框架的课件
    SAAS云平台搭建札记: (二) Linux Ubutu下.Net Core整套运行环境的搭建
    SAAS云平台搭建札记: (一) 浅论SAAS多租户自助云服务平台的产品、服务和订单
    开源三维地球GIS引擎Cesium常用功能的开发
    Asp.net管理信息系统中数据统计功能的实现
    [Thrift]学习使用Thrift
    使用Netty实现一下简单RPC
    [Redis]Redis做简单的分布式限流
  • 原文地址:https://www.cnblogs.com/zhaijunming5/p/6424800.html
Copyright © 2020-2023  润新知