欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

ElasticSearch6.x:26.JavaAPI文档管理

程序员文章站 2022-07-09 23:14:44
1、文档获取 package cn.hadron; import cn.hadron.es.*; import org.elasticsearch.action.get.GetRes...

1、文档获取

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;

public class GetDocDemo {
    public static void main(String[] args){
        TransportClient client=ESUtil.getClient();
        GetResponse response =client.prepareGet("index1","blog","1").get();
        System.out.println(response.isExists());
        System.out.println(response.getIndex());
        System.out.println(response.getType());
        System.out.println(response.getId());
        System.out.println(response.getVersion());
        String source=response.getSourceAsString();
        System.out.println(source);
    }
}

ElasticSearch6.x:26.JavaAPI文档管理

2、文档删除

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
public class DeleteDocDemo {
    public static void main(String[] args){
        TransportClient client=ESUtil.getClient();
        DeleteResponse response=client.prepareDelete("index1","blog","1").get();
        //删除成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被删除文档的类型
        System.out.println(response.getType());
        //返回被删除文档的ID
        System.out.println(response.getId());
        //返回被删除文档的版本信息
        System.out.println(response.getVersion());
    }
}

ElasticSearch6.x:26.JavaAPI文档管理

3、文档更新

package cn.hadron;
import cn.hadron.es.*;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
public class UpdateDocDemo {
    public static void main(String[] args)throws Exception{
        TransportClient client=ESUtil.getClient();
        UpdateRequest request=new UpdateRequest();
        request.index("index1")
                .type("blog")
                .id("2")
                .doc(
                        jsonBuilder().startObject()
                        .field("title","单例模式解读")
                        .endObject()
                );
        UpdateResponse response=client.update(request).get();
        //更新成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被更新文档的类型
        System.out.println(response.getType());
        //返回被更新文档的ID
        System.out.println(response.getId());
        //返回被更新文档的版本信息
        System.out.println(response.getVersion());
    }
}

ElasticSearch6.x:26.JavaAPI文档管理

4、文档upsert操作

package cn.hadron;

import cn.hadron.es.ESUtil;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 *  文档upsert操作:如果文档存在则执行更新操作,否则执行添加操作
 */
public class UpsertDocDemo {
    public static void main(String[] args)throws Exception{
        TransportClient client=ESUtil.getClient();
        IndexRequest request1 =new IndexRequest("index1","blog","1")
                .source(
                        jsonBuilder().startObject()
                                .field("id","1")
                                .field("title","装饰模式")
                                .field("content","动态地扩展一个对象的功能")
                                .field("postdate","2018-02-03 14:38:10")
                                .field("url","csdn.net/79239072")
                                .endObject()
                );
        UpdateRequest request2=new UpdateRequest("index1","blog","1")
                .doc(
                        jsonBuilder().startObject()
                        .field("title","装饰模式解读")
                        .endObject()
                ).upsert(request1);
        UpdateResponse response=client.update(request2).get();
        //upsert操作成功返回OK,否则返回NOT_FOUND
        System.out.println(response.status());
        //返回被操作文档的类型
        System.out.println(response.getType());
        //返回被操作文档的ID
        System.out.println(response.getId());
        //返回被操作文档的版本信息
        System.out.println(response.getVersion());
    }
}

ElasticSearch6.x:26.JavaAPI文档管理