zoukankan      html  css  js  c++  java
  • elasticsearch批量操作

    1、批量查询的好处

    就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的

    如果进行批量查询的话,查询100条数据,就只要发送1次网络请求,网络请求的性能开销缩减100

    mget的语法

    mget批量查询

    GET /_mget

    {

       "docs" : [

          {

             "_index" : "test_index",

             "_type" :  "test_type",

             "_id" :    1

          },

          {

             "_index" : "test_index",

             "_type" :  "test_type",

             "_id" :    2

          }

       ]

    }

    {

      "docs": [

        {

          "_index": "test_index",

          "_type": "test_type",

          "_id": "1",

          "_version": 2,

          "found": true,

          "_source": {

            "test_field1": "test field1",

            "test_field2": "test field2"

          }

        },

        {

          "_index": "test_index",

          "_type": "test_type",

          "_id": "2",

          "_version": 1,

          "found": true,

          "_source": {

            "test_content": "my test"

          }

        }

      ]

    }

    3)如果查询的document是一个index下的不同type种的话

    GET /test_index/_mget

    {

       "docs" : [

          {

             "_type" :  "test_type",

             "_id" :    1

          },

          {

             "_type" :  "test_type",

             "_id" :    2

          }

       ]

    }

    4)如果查询的数据都在同一个index下的同一个type下,最简单了

    GET /test_index/test_type/_mget

    {

       "ids": [1, 2]

    }

    3mget的重要性

    可以说mget是很重要的,一般来说,在进行查询的时候,如果一次性要查询多条数据的话,那么一定要用batch批量操作的api

    尽可能减少网络开销次数,可能可以将性能提升数倍,甚至数十倍,非常非常之重要

    bulk语法

    POST /_bulk

    { "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}

    { "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}

    { "test_field":    "test12" }

    { "index":  { "_index": "test_index", "_type": "test_type", "_id": "2" }}

    { "test_field":    "replaced test2" }

    { "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

    { "doc" : {"test_field2" : "bulk test1"} }

    每一个操作要两个json串,语法如下:

    {"action": {"metadata"}}

    {"data"}

    举例,比如你现在要创建一个文档,放bulk里面,看起来会是这样子的:

    {"index": {"_index": "test_index", "_type", "test_type", "_id": "1"}}

    {"test_field1": "test1", "test_field2": "test2"}

    有哪些类型的操作可以执行呢?

    1delete:删除一个文档,只要1json串就可以了

    { "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }}

    2createPUT /index/type/id/_create,强制创建

    { "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}

    { "test_field":    "test12" }

    3index:普通的put操作,可以是创建文档,也可以是全量替换文档

    { "index":  { "_index": "test_index", "_type": "test_type", "_id": "2" }}

    { "test_field":    "replaced test2" }

    4update:执行的partial update操作

    { "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

    { "doc" : {"test_field2" : "bulk test1"} }

    注意:

    bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志。bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从1000~5000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在5~15MB之间。

    multi-indexmulti-type搜索模式

    告诉你如何一次性搜索多个index和多个type下的数据

    /_search:所有索引,所有type下的所有数据都搜索出来

    /index1/_search:指定一个index,搜索其下所有type的数据

    /index1,index2/_search:同时搜索两个index下的数据

    /test1_*,test2_*/_search:按照通配符去匹配多个索引

    /index1/type1/_search:搜索一个index下指定的type的数据

    /index1/type1,type2/_search:可以搜索一个index下多个type的数据

    /index1,index2/type1,type2/_search:搜索多个index下的多个type的数据

    /_all/type1,type2/_search_all,可以代表搜索所有index下的指定type的数据

    分页搜索

    将这9条数据分成3页,每一页是3条数据

    GET /test_index/test_type/_search?from=0&size=3

  • 相关阅读:
    LeetCode Binary Tree Inorder Traversal
    LeetCode Populating Next Right Pointers in Each Node
    LeetCode Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode Reverse Linked List II
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Pascal's Triangle
    Palindrome Construct Binary Tree from Preorder and Inorder Traversal
    Pascal's Triangle II
    LeetCode Word Ladder
    LeetCode Binary Tree Zigzag Level Order Traversal
  • 原文地址:https://www.cnblogs.com/kesimin/p/9559958.html
Copyright © 2011-2022 走看看