• elasticsearch API


    一、elasticsearch的API:

    1、查询整个集群中的文档的数量

    GET _count

    {
      "query": {
        "match_all": {}
      }
    } 

    2、查询整个集群中的文档的详细信息

    GET _search

    {
      "query": {
        "match_all": {}
      }
    }

    3、添加数据

    PUT index_name/doc_type_name/id (添加和更新)(更新时_version会增加)

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    }

    PUT index_name/doc_type_name/id?version=1 (更新)(更新时_version会增加)(增加了并发控制)

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    } 

    PUT index_name/doc_type_name/id?version=1&version_type=external (更新和创建都可以)(使用外部版本控制系统)

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    } 

    POST index_name/doc_type_name/id/_update (局部更新)(增加tags和views两个字段)

    {
      "doc" : {
        "tags" : [ "testing" ],
        "views": 0
      }
     

    POST index_name/doc_type_name/id/_update (局部更新)(views字段的值+1)

    {
      "script" : "ctx._source.views+=1"
    }

    POST index_name/doc_type_name/id/_update (局部更新)(tags数组字段增加新成员“search”)

    {
      "script" : "ctx._source.tags+=new_tag",
      "params" : {
        "new_tag" : "search"
      }
    } 

    POST index_name/doc_type_name/id/_update (局部更新)(如果views==1,就删除文档)

    {
      "script" : "ctx.op = ctx._source.views == count ? 'delete' : 'none'",
      "params" : {
        "count": 1
      }
    }

    POST index_name/doc_type_name/id/_update (局部更新)(更新可能不存在的文档)

    {
      "script" : "ctx._source.views+=1",
      "upsert": {
        "views": 1
      }
    } 

    POST index_name/doc_type_name/id/_update?retry_on_conflict=5 (局部更新)(并发情况下失败后的尝试更新次数)

    {
      "script" : "ctx._source.views+=1",
      "upsert": {
        "views": 0
      }
    } 

    PUT index_name/doc_type_name/id?op_type=create(创建)

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    }

    PUT index_name/doc_type_name/id/_create(创建)

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    }

    POST index_name/doc_type_name (创建)自动生成ID

    {
      "first_name" : "John",
      "last_name" : "Smith",
      "age" : 25,
      "about" : "I love to go rock climbing"
    }

    4、查询单一行的数据(DLETE方法删除文档、HEAD方法检查文档是否存在、PUT方法更新文档)

    GET index_name/doc_type_name/id

    GET index_name/doc_type_name/id?_source=last_name,about

    GET index_name/doc_type_name/id/_source

    5、查询某一doc_type的所有数据(默认情况下搜索会返回前10个结果)

    GET index_name/doc_type_name/_search

    6、条件查询

    GET index_name/doc_type_name/_search?q=last_name:Smith

    GET index_name/doc_type_name/_search 

    {
      "query" : {
        "match" : {
          "last_name" : "Smith"
        }
      }
    }

    下面这个查询用到了过滤器

    {
        "query": {
            "filtered": {
                "filter": {
                    "range": {
                        "age": {
                            "gt": 30
                        }
                    }
                },
                "query": {
                    "match": {
                        "last_name": "smith"
                    }
                }
            }
        }
    }

    7、全文搜索(也是条件查询)(可以看出查询体和6一模一样,只是7中有两个单词,这个查询单词是或的关系,也就是说只要about字段含有rock或者climbing的行都会被查到)

    GET index_name/doc_type_name/_search 

    {
      "query" : {
        "match" : {
          "about" : "rock climbing"
        }
      }
    }

    8、短语搜索(也是条件查询)(这个查询与7的不同就是,这两个单词是且的关系,也就是说about字段同时含有rock和climbing的行才会被查到)

    GET index_name/doc_type_name/_search 

    {
      "query" : {
        "match_phrase" : {
          "about" : "rock climbing"
        }
      }
    }

    9、高亮我们的搜索(也是条件查询)

    GET index_name/doc_type_name/_search 

    {
      "query" : {
        "match_phrase" : {
          "about" : "rock climbing"
        }
      },

      "highlight": {
        "fields" : {
          "about" : {}
        }
      }

    }

    10、聚合(统计分析+条件查询)

    PUT index_name/_mapping/doc_type_name
    {
      "properties": {
        "field_name": {
          "type": "text",
          "fielddata": true
        }
      }
    }

    GET index_name/doc_type_name/_search 

    {
      "aggs": {
        "all_interests": {
          "terms": {
            "field": "last_name"
          }
        }
      }
    }

    GET index_name/doc_type_name/_search 

    {
      "query": {
        "match": {
          "last_name": "smith"
        }
      },
      "aggs": {
        "all_interests": {
          "terms": {
            "field": "interests"
          }
        }
      }
    } 

    GET index_name/doc_type_name/_search 

    {
      "aggs" : {
        "all_interests" : {
          "terms" : { "field" : "interests" },
          "aggs" : {
            "avg_age" : {
              "avg" : { "field" : "age" }
            }
          }
        }
      }
    }

    10、集群健康状况

    GET _cluster/health

    11、创建索引

    PUT index_name 分配3个主分片和每个主分片都有一个复制分片;当索引创建完成后,主分片的数量就不可以变了

    {
      "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 1
      }
    }

    PUT index_name/_settings 索引创建完成后,复制分片的数量是可以改变的
    {
      "number_of_replicas" : 2
    }

    12、检查文档是否存在

    HEAD index_name/doc_type_name/id

    13、删除文档

    DELETE index_name/doc_type_name/id(删除时_version会增加)

    14、检索多个文档

    POST _mget

    {
      "docs" : [
        {
          "_index" : "website",
          "_type" : "blog",
          "_id" : 2
        },
        {  
          "_index" : "website",
          "_type" : "pageviews",
          "_id" : 1,
          "_source": "views"
        }
      ]
    }

    POST index_name/doc_type_name/_mget

    {
      "docs" : [
        { "_id" : 2 },
        { "_type" : "pageviews", "_id" : 1 }
      ]
    }

    POST index_name/doc_type_name/_mget

    {
      "ids" : [ "2", "1" ]
    }

    15、批量操作(每个请求都是独立的,所以不能实现事务)

  • 相关阅读:
    桶排序
    Ultra-QuickSort
    Merge Sort
    Max Sum
    快排
    Oil Deposits
    1009: josephus问题
    A+B Again(在某个数中找大于m的最小约数)
    luogu4181 [USACO18JAN]Rental Service (贪心)
    luogu4185 [USACO18JAN]MooTube (并查集)
  • 原文地址:https://www.cnblogs.com/erdanyang/p/10955037.html
Copyright © 2020-2023  润新知