elasticsearch客户端基于索引,文档的增删改查操作

package com.example.test.esClientDemo;
import com.example.pojo.User;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.*;
import org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.net.InetAddress.getByName;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
 * com.xinguangnet.tuchao.merchant.manage
 *
 * @Author : Wukn
 * @Date : 2018/2/5
 */
public class EsClientTest {

    //开启客户端
    TransportClient client;

    {
        try {
            client = new PreBuiltTransportClient( Settings.EMPTY )
                    //设置集群节点
                    .addTransportAddress(new InetSocketTransportAddress( getByName("host1"), 9300))
                    .addTransportAddress(new InetSocketTransportAddress( getByName("host2"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace( );
        }
    }





    public static void main(String[] args) throws Exception {
        jackson();

    }


    /**
     * 日志
     */
    private  static Logger logger = LoggerFactory.getLogger( EsClientTest.class );


    public static final long serialVersionUID = -763638353551774166L;

    /**
     * 索引名称
     */
    public static final String INDEX_NAME = "index_entity";

    /**
     * 索引类型
     */
    public static final String TYPE = "tstype";






    /**
     *创建客户端
     */
    public void buildClient() throws UnknownHostException {
        //开启客户端
        TransportClient client = new PreBuiltTransportClient( Settings.EMPTY )
                //设置集群节点
                .addTransportAddress(new InetSocketTransportAddress( getByName("host1"), 9300))
                .addTransportAddress(new InetSocketTransportAddress( getByName("host2"), 9300));

        //关闭
        client.close();
    }




    /**
     *创建客户端,如果您使用与“elasticsearch”不同的名称,则必须设置群集名称:
     * @return
     */
    public TransportClient nameSeting() {
        Settings settings = Settings.builder().put( "cluster.name","muclusterName",true ).build();
        TransportClient client = new PreBuiltTransportClient( settings );
        return client;
    }






    /**
     * 利用elasticsearch内置方法生成josn
     */
    public void jsonTest() throws Exception{
        /**
         *
          String json =“{”+
                     \”user \“\”kimchy \“”+
                     \”postDate \“\”2013-01-30 \“”+
                     \”message \“\”尝试Elasticsearch \“”+
                      "}”;

         */
        //elasticsearch内置方法生成josn
        XContentBuilder builder = jsonBuilder()
                .startObject()
                .field("user", "kimchy")
                .field("postDate", new Date())
                .field("message", "trying out Elasticsearch")
                .endObject();

        //获取JSON
        String str = builder.string();
    }




    /**
     * 还可以将文档编入索引为JSON字符串,并且不必提供ID
     */
    public void indejs() {
        String json = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";

        IndexResponse response = client.prepareIndex("twitter", "tweet")
                .setSource(json)
                .get();


        //对象会给你一个报告
        // Index name
        String _index = response.getIndex();
        // Type name
                String _type = response.getType();
        // Document ID (generated or not)
                String _id = response.getId();
        // Version (if it's the first time you index this document, you will get: 1)
                long _version = response.getVersion();
        // status has stored current instance statement.
        RestStatus status = response.status();

    }






    /**
     * 使用Jacksonbean序列化为JSON
     */
    public static  void jackson() throws Exception {
        //实例化一个josn映射器
        ObjectMapper mapper = new ObjectMapper(  );
        User user = new User( 12,"sd" );
        byte [] json = mapper.writeValueAsBytes( user );
    }






    /**
     * 创建索引
     * 以下示例将一个JSON文档索引为一个名为twitter的索引,类型是tweet,值为1     */
    public void indexbuild() throws Exception {
        //开启客户端
        TransportClient client = new PreBuiltTransportClient( Settings.EMPTY )
                //设置集群节点
                .addTransportAddress(new InetSocketTransportAddress( getByName("host1"), 9300))
                .addTransportAddress(new InetSocketTransportAddress( getByName("host2"), 9300));


        //将一个JSON文档索引为一个名为twitter的索引,类型是tweet,值为1
        IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
                //elasticsearch内置方法生成josn
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
                .get();
    }



    /**
     * 创建索引可以不提供id,此时elasticsearch会自动生成一个唯一id
     */
    public void indexbu() throws Exception {
        //创建客户端
        TransportClient client = new PreBuiltTransportClient( Settings.EMPTY )
                //设置集群
                .addTransportAddress(new InetSocketTransportAddress( getByName("host1"), 9300))
                .addTransportAddress(new InetSocketTransportAddress( getByName("host2"), 9300));

        //定义一个josn字符串
        String json = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";

        //将一个JSON文档索引为一个名为twitter的索引,类型是tweet        IndexResponse response = client.prepareIndex("twitter", "tweet")
                //elasticsearch内置方法生成josn
                .setSource( ).get(json);


        //通过IndexResponse得到所有信息
        //索引名称
        String indexName = response.getIndex();
        //索引类型
        String indexType = response.getType();
        //文档Id
        String id  = response.getId();
        //版本号
        Long versionId = response.getVersion();
        //状态已经存储了当前的实例语句
        RestStatus restStatus = response.status();
    }





    /**
     *
     * 通过客户端获取文档
     * get API允许从索引中获取基于其id的类型化JSON文档。以下示例从名为twitter的索引中获取JSON文档,该类型名为tweetID1     */
    public void clientqueryjson() throws Exception{
        //创建客户端
        TransportClient client = new PreBuiltTransportClient( Settings.EMPTY )
                //设置集群
                .addTransportAddress(new InetSocketTransportAddress( getByName("host1"), 9300))
                .addTransportAddress(new InetSocketTransportAddress( getByName("host2"), 9300));

        //通过客户端获取索引信息
        GetResponse response = client.prepareGet("twitter", "tweet", "1").get();

        /**
         * 默认情况下,operationThreaded设置为true表示在不同的线程上执行操作。这是一个例子,它将其设置为 false         */
//        GetResponse response2 = client.prepareGet("twitter", "tweet", "1")
//                .setOperationThreaded( false )
//                .get();

        //通过IndexResponse得到所有信息
        //索引名称
        String indexName = response.getIndex();
        //索引类型
        String indexType = response.getType();
        //文档Id
        String id  = response.getId();
        //版本号
        Long versionId = response.getVersion();
    }




    /**
     * 多获取API允许基于它们获取文档列表indextype并且id
     */
    public void listApi() throws Exception{
        MultiGetResponse responses = client.prepareMultiGet()
                //得到一个单一的id
                .add("twitter", "tweet", "1")
                //或通过相同索引/类型的ID列表
                .add("twitter", "tweet", "2", "3", "4")
                //也可以从另一个索引获得
                .add("another", "type", "foo")
                .get();



        //迭代结果集
        for(MultiGetItemResponse multiGetItemResponses:responses) {
            GetResponse getFields =multiGetItemResponses.getResponse();
            //检查文档是否存在
            if(getFields.isExists()) {
                String json = getFields.getSourceAsString();
            }
        }

    }




    /**
     * 删除文档
     */
    public void deleteindex() throws Exception{
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) );


        //以下示例从名为twitter的索引中删除JSON文档,该类型名为tweetID1        DeleteResponse response = client.prepareDelete("twitter" , "twwe" , "1").get();

    }


    /**
     * 通过查询删除api
     */
    public void quersydelete() throws Exception{
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) );

        BulkByScrollResponse response =
                DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                        //询问
                        .filter( QueryBuilders.matchQuery("gender", "male"))
                        //指数
                        .source("persons")
                        //执行操作
                        .get();

        //删除的数量
        long number =  response.getDeleted();
    }



    public void deleteApi() throws Exception{
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) )
                .addTransportAddress( new InetSocketTransportAddress( getByName( "h1" ) , 9300 ) );


        //因为它可能是一个长时间运行的操作,所以如果你希望异步执行它,你可以调用,execute而不是get 像下面这样提供一个监听器
        DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("gender", "male"))
                .source("persons")
               .execute( new ActionListener<BulkByScrollResponse>( ) {
                   @Override
                   public void onResponse(BulkByScrollResponse bulkByScrollResponse) {
                       //删除数量
                       //long number = .getDeleted();
                   }

                   @Override
                   public void onFailure(Exception e) {

                   }
               } );
    }




    /**
     * 允许在单个请求中索引和删除多个文档
     */
    public void deteteli() throws Exception{
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        // 使用客户端#准备,或使用请求#直接构建索引/删除请求
        bulkRequest.add(client.prepareIndex("twitter", "tweet", "1")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
        );

        bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "another post")
                        .endObject()
                )
        );

        //执行删除操作
        BulkResponse bulkResponse = bulkRequest.get();
        //判断处理结果是否成功
        if (bulkResponse.hasFailures()) {
            //遍历每个批量响应项来处理失败
            throw new Exception( "处理失败");
        }
    }





    /**
     * 更新Api方法一
     * 创建一个UpdateRequest并将其发送给客户端:
     */
    public void uodateApi() throws Exception{
        UpdateRequest request = new UpdateRequest(  );
        request.index("index");
        request.type("type");
        request.id("1");
        request.doc(jsonBuilder().startObject().field( "ge" ,  "maol").endObject());


        client.update( request ).get();

    }

    /**
     * 更新Api方法一方法二
     *也有支持upsert。如果文档不存在,upsert 元素的内容将用于索引新鲜文档:
     * @throws Exception
     */
    public void fg() throws Exception{
        //设置文档索引
        IndexRequest indexRequest = new IndexRequest("index", "type", "1")
                .source(jsonBuilder()
                        .startObject()
                        .field("name", "Joe Smith")
                        .field("gender", "male")
                        .endObject());

        //修改文档内容
        UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
                .doc(jsonBuilder()
                        .startObject()
                        .field("gender", "male")
                        .endObject())
                .upsert(indexRequest);

        client.update(updateRequest).get();
    }





    /**
     * ulkProcessor类提供了一个简单的界面,自动冲水,基于数或请求的大小批量操作,或在给定的时间。
     要使用它,请先创建一个BulkProcessor实例:
     */
    public void pildf() throws Exception{
        BulkProcessor bulkProcessor = BulkProcessor.builder(
                //添加您的elasticsearch客户端
                client,

                new BulkProcessor.Listener() {
                    //这个方法在执行bulk之前被调用。例如,你可以看到numberOfActions request.numberOfActions()
                    @Override
                    public void beforeBulk(long executionId, BulkRequest request) {

                    }

                    //批量执行后调用此方法。你可以例如检查是否有一些失败的请求response.hasFailures()
                    @Override
                    public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {


                    }
                    //这个方法在批量失败并被提出时调用 Throwable
                    @Override
                    public void afterBulk(long executionId, BulkRequest request, Throwable failure) {

                    }
                })
                //我们希望每10万次请求执行一次批量操作
                .setBulkActions(10000)
                //我们希望每5mb刷新一次散装
                .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB))
                //无论请求的数量是多少,我们都希望每5秒刷新一次
                .setFlushInterval( TimeValue.timeValueSeconds(5))
                //设置并发请求的数量。值为0意味着只有一个请求将被允许执行。值为1表示在累积新的批量请求时允许执行1个并发请求
                .setConcurrentRequests(1)
                //设置自定义退避策略,最初将等待100ms,成倍增加,并重试三次。每当有一个或多个批量项目请求失败时尝试重试,
                // EsRejectedExecutionException 这表示可用于处理请求的计算资源太少。要禁用退避,请传递BackoffPolicy.noBackoff()                .setBackoffPolicy(
                        BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3))
                .build();


        //然后,您可以简单地将您的请求添加到BulkProcessor        bulkProcessor.add(new IndexRequest("twitter", "tweet", "1").source( "sdds" ));
        bulkProcessor.add(new DeleteRequest("twitter", "tweet", "2"));

        //关闭处理器
        bulkProcessor.awaitClose( 10,TimeUnit.MINUTES );
        //或者
        // bulkProcessor.close();

    }


    /**
     *如果您正在使用elasticsearch运行测试并且正在使用BulkProcessor填充数据集,
     * 则应该更好地设置并发请求的数量,0以便以同步方式执行批量刷新操作:
     */
    public void gh() {
        BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
            @Override
            public void beforeBulk(long l, BulkRequest bulkRequest) {

            }

            @Override
            public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {

            }

            @Override
            public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {

            } /* Listener methods */ })
                .setBulkActions(10000)
                .setConcurrentRequests(0)
                .build();

            /* 添加你的请求 */
                   // bulkProcessor.add();

            //刷新任何剩余的请求
                    bulkProcessor.flush();

            //或者关闭bulkProcessor,如果你不再需要的话
                    bulkProcessor.close();

            //刷新你的索引
                    client.admin().indices().prepareRefresh().get();

            //现在你可以开始搜索!
                    client.prepareSearch().get();

    }



























}
阅读更多
个人分类: elasticsearch
上一篇elastisearch javaApi之客户 端基础知识
下一篇elastisearch javaApi之搜索基础知识
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭