• Elasticpostman简单增删改查


    一、基本操作目录

    1、查看当前所有索引

    # 所有索引
    GET http://localhost:9200/_cat/indices?v
    
    # 查看s开头的索引
    GET http://localhost:9200/_cat/indices/s*?v

    2、查看当前所有索引别名

    GET http://127.0.0.1:9200/_alias

    3、配置账户密码访问

    GET https://username:password@127.0.0.1:9200/

    4、查询索引(别名)下的数据

    GET http://127.0.0.1:9200/employee/_search
    {
        "query" : {
            "match" : {
                "last_name" : "Smith"
            }
        }
    }

    只展示部分字段查找

    GET student/_search
    {
      "query": {
        "match": {
          "age": "12"
        }
      },
      "_source": {
        "includes": [
          "name"
        ]
      }
    }

    5、部分更新一个文档

    POST /website/blog/1/_update
    {
       "doc" : {
          "tags" : [ "testing" ],
          "views": 0
       }
    }

    脚本更新(https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting.html)

    POST /website/blog/1/_update
    {
       "script" : "ctx._source.tags+=new_tag",
       "params" : {
          "new_tag" : "search"
       }
    }

    6、删除一整个索引

    DELETE https://admin:123456@127.0.0.1:9200/test_2022-05-13

    RESTClient

    # 脚本更新
    request.setScript(
        new Script(
            ScriptType.INLINE, "painless",
            "ctx._source.tags+=new_tag",
            Collections.emptyMap())); 

    https://blog.csdn.net/wyaoyao93/article/details/112467431

    package study.wyy.esclient.high.document;
    
    import lombok.extern.slf4j.Slf4j;
    import org.elasticsearch.action.DocWriteResponse;
    import org.elasticsearch.action.support.WriteRequest;
    import org.elasticsearch.action.support.replication.ReplicationResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.common.document.DocumentField;
    import org.elasticsearch.common.unit.TimeValue;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.elasticsearch.index.get.GetResult;
    import org.junit.Test;
    import study.wyy.esclient.high.BaseTest;
    
    import java.io.IOException;
    import java.util.Map;
    
    /**
     * @author wyaoyao
     * @description
     * @date 2021/1/11 14:52
     * 更新文档测试
     */
    @Slf4j
    public class UpdateDocumentTest extends BaseTest {
    
        /****
         * 同步执行
         */
        @Test
        public void testSync() throws IOException {
            // 1 构建请求 索引库 文档id
            UpdateRequest request = new UpdateRequest("books","1");
            // 2 配置参数
            // 2.1 设置路由
            //request.routing("hello");
            // 2.2 设置超时时间
            request.timeout(TimeValue.timeValueMinutes(2));
            // 2.3 设置刷新策略
            request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
            // 2.4 设置:如果更新的文档在更新时被另一个操作更改,则重试更新的次数
            request.retryOnConflict(3);
            // 2.5 启用源检查,默认是关闭 就是restAPI中的是是否返回Source字段
            request.fetchSource(true);
            // 设置include和exclude
                         String[] includes = new String[]{"title","language"};
            String[] exclude  = new String[]{"description"};
            request.fetchSource(includes,exclude);
    
            // 3 构建json文档: 前面已经介绍过了es支持三种方式:json字符串,Map,XContentBuilder
            XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
            xContentBuilder.startObject();
            xContentBuilder.field("language","python");
            xContentBuilder.field("title","改个题目试试");
            xContentBuilder.endObject();
            request= request.doc(xContentBuilder);
    
            // 4 执行
            UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    
            // 5 解析响应
            // 5.1 获取索引库和文档id
            String index = response.getIndex();
            String id = response.getId();
            log.info("索引库: {}; 文档id: {}",index,id);
            // 5.2 获取结果:枚举值
            DocWriteResponse.Result result = response.getResult();
            log.info("响应结果: {}",result);
            // 5.3 分片信息
            ReplicationResponse.ShardInfo shardInfo = response.getShardInfo();
            log.info("分片信息: {}",shardInfo);
            // 5.4 版本号
            long version = response.getVersion();
            log.info("版本号: {}",version);
            GetResult getResult = response.getGetResult();
            Map<String, Object> source = getResult.getSource();
            log.info("source: {}",source);
            Map<String, DocumentField> documentFields = getResult.getDocumentFields();
            log.info("documentFields: {}",documentFields);
            Map<String, DocumentField> metadataFields = getResult.getMetadataFields();
            log.info("metadataFields: {}",metadataFields);
            client.close();
    
        }
    }

    6、简单的批量操作

    POST /_bulk
    { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} 
    { "create": { "_index": "website", "_type": "blog", "_id": "123" }}
    { "title":    "My first blog post" }
    { "index":  { "_index": "website", "_type": "blog" }}
    { "title":    "My second blog post" }
    { "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
    { "doc" : {"title" : "My updated blog post"} } 

    7、批量添加文档

    POST http://127.0.0.1:9200/blog/
    { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }

    8、删除一个文档

    DELETE /website/blog/123

    二、官方文档地址

    表达式搜索 - https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_with_query_dsl.html

    复杂的搜索 - https://www.elastic.co/guide/cn/elasticsearch/guide/current/_more_complicated_searches.html

    文档部分更新 - https://www.elastic.co/guide/cn/elasticsearch/guide/current/partial-updates.html

    深入搜索 - https://www.elastic.co/guide/cn/elasticsearch/guide/current/search-in-depth.html

    代价较小的批量操作 - https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html

    三、常用的语言的客户端

    官方手册 - https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.1/index.html

    民间简单入门手册 - https://www.letianbiji.com/elasticsearch/es7-java-RestClient.html

    官方手册简单版 - https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-supported-apis.html

    四、docker安装Elastic 

    docker pull docker.elastic.co/elasticsearch/elasticsearch:7.7.0

     docker run -d --name=elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.7.0

    # 如果有需要更改的配置进入容器中 
    vi config/elasticsearch.yml
    docker restart es

    五、相关博客

    索引别名的使用 - https://blog.csdn.net/qq330983778/article/details/102980861

    Elastic查询语法详解 - https://www.knowledgedict.com/tutorial/elasticsearch-query.html

    通过 _bulk 批量添加文档 - https://www.letianbiji.com/elasticsearch/es7-bulk-add-doc.html

    查询只展示部分字段 - https://www.letianbiji.com/elasticsearch/es7-show-partial-fields.html

    ES操作指引干货 - https://zhuanlan.zhihu.com/p/355515434

     六、优秀手册

    乐天笔记 - Elastic7快速上手 - https://www.letianbiji.com/elasticsearch/es7-quick-start.html

    K - https://www.knowledgedict.com/tutorial/elasticsearch-intro.html

  • 相关阅读:
    Linux XOR.DDoS样本取证特征与清除
    利用Volatility对Linux内存取证分析-常用命令翻译
    【黑客免杀攻防】读书笔记14
    CertUtil.exe被利用来下载恶意软件
    利用rundll32执行程序的函数执行程序
    揭秘Patchwork APT攻击-恶意软件样本BADNEWS
    【CTF MISC】pyc文件反编译到Python源码-2017世安杯CTF writeup详解
    [ 总结 ] 删除通过find查找到的文件
    [ 脚本 ] RHEL6.x 及Centos6.x 初始化脚本
    [ 手记 ] 联想rd650服务器整列及系统安装
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/16257882.html
Copyright © 2020-2023  润新知