zoukankan      html  css  js  c++  java
  • elasticsearch-基础查询语法整理

    常用查询

    • 关闭服务
    curl -XPOST 'http://localhost:9200/_shutdown'
    • 查询集群健康
    GET /_cluster/health
    • 修改复制分片的数量
    PUT /blogs/_settings
    {
       "number_of_replicas" : 2
    }
    • 创建索引并设置分片数
    //分配3个主分片和一个复制分片(每个主分片都有一个复制分片)
    PUT /blogs
    {
       "settings" : {
          "number_of_shards" : 3,
          "number_of_replicas" : 1
       }
    }
    • 计算集群中的文档数量
    curl -XGET 'http://localhost:9200/_count?pretty' -d '
    {
        "query": {
            "match_all": {}
        }
    }
    '
    • 添加
    POST /megacorp/employee/2
    {
        "first_name" : "孙悟空",
        "last_name" :  "孙悟空",
        "age" :        33,
        "about" :      "四大名著 西游记 西天取经",
        "interests": [ "打怪", "睡觉" ]
    }
    
    //自定义id创建
    //方式一
    PUT /megacorp/employee/123?op_type=create
    {
        "first_name" : "孙悟空",
        "last_name" :  "孙悟空",
        "age" :        33,
        "about" :      "四大名著 西游记 西天取经",
        "interests": [ "打怪", "睡觉" ]
    }
    //方式二
    PUT /megacorp/employee/123/_create
    {
        "first_name" : "孙悟空",
        "last_name" :  "孙悟空",
        "age" :        33,
        "about" :      "四大名著 西游记 西天取经",
        "interests": [ "打怪", "睡觉" ]
    }
    • 修改
    PUT /megacorp/employee/2
    {
        "first_name" : "孙悟空",
        "last_name" :  "孙悟空",
        "age" :        33,
        "about" :      "四大名著 西游记 西天取经",
        "interests": [ "打怪", "吃桃" ]
    }
    
    //自定义版本号修改
    PUT /website/blog/2?version=5&version_type=external
    {
        "first_name" : "孙悟空",
        "last_name" :  "孙悟空",
        "age" :        33,
        "about" :      "四大名著 西游记 西天取经",
        "interests": [ "打怪", "吃桃" ]
    }
    
    //自增修改字段
    //?retry_on_conflict=5当出现版本冲突时的重试次数
    POST /megacorp/employee/5/_update?retry_on_conflict=5
    {
      "script": "ctx._source.age+=1",
      "upsert": {
        "age": 0
      }
    }
    • 删除
    DELETE /megacorp/employee/2

    简单搜索查询

    //按id查询
    GET /megacorp/employee/1
    
    //按id查询格式化
    GET /megacorp/employee/1?pretty
    
    //只返回_source部分字段
    GET /megacorp/employee/1?_source=first_name,age
    
    //只返回_source字段而不要其他的元数据
    GET /megacorp/employee/1/_source
    
    //搜索全部
    GET /megacorp/employee/_search
    
    //查询字符串
    GET /megacorp/employee/_search?q=last_name:Smith
    • 批量查询mget

    合并多个请求可以避免每个请求单独的网络开销

    //不同的index查询
    POST /_mget
    {
      "docs": [
        {
          "_index": "myblog",
          "_type": "mb_articles",
          "_id": 2
        },
        {
          "_index": "megacorp",
          "_type": "employee",
          "_id": 5
        }
      ]
    }
    
    //同一个type查询
    POST /megacorp/employee/_mget
    {
      "docs": [
        {
          "_id": 5
        },
        {
          "_id": 1
        }
      ]
    }
    或者
    POST /megacorp/employee/_mget
    {
      "ids": [
        "2",
        "1"
      ]
    }
    • 批量更新 bulk

    格式:

    这种格式类似于用" "符号连接起来的一行一行的JSON文档流(stream)。两个重要的点需要注意:

    • 1、每行必须以" "符号结尾,包括最后一行。这些都是作为每行有效的分离而做的标记。
    • 2、每一行的数据不能包含未被转义的换行符,它们会干扰分析——这意味着JSON不能被美化打印
    { action: { metadata }}
    
    { request body        }
    
    { action: { metadata }}
    
    { request body        }
    
    ...
    • action:
    行为解释
    create 当文档不存在时创建之
    index 创建新文档或替换已有文档
    update 局部更新文档
    delete 删除一个文档
    POST /_bulk
    { "create": { "_index": "website", "_type": "blog", "_id": "123" }}
    { "title":    "My first blog post" }
    { "index":  { "_index": "website", "_type": "blog" }}
    { "title":    "My second blog post" }
  • 相关阅读:
    [扩展推荐] Laravel 中利用 GeoIP 获取用户地理位置信息
    10 个优质的 Laravel 扩展推荐
    5 个非常有用的 Laravel Blade 指令,你用过哪些?
    PHP 7.3 我们将迎来灵活的 heredoc 和 nowdoc 句法结构
    使用 Swoole 来加速你的 Laravel 应用
    一个成功的 Git 分支模型(适用于商业应用开发)
    github搜索语法
    python协程爬虫-aiohttp+aiomultiprocess使用
    python-协程、多线程、多进程性能比较
    functools模块-为函数预设args/kwargs参数
  • 原文地址:https://www.cnblogs.com/dqh123/p/13225511.html
Copyright © 2011-2022 走看看