Elasticsearch常用API

一、动态更新模板

curl -XPUT 'http://localhost:9200/rcs-errstat-201707/_mapping/abctype' -d 
'{
    "abctype" : {
        "_all": {"enabled": false},
        "properties":{
            "a" : {
                 "type" : "long",
                 "index" : "not_analyzed",
                 "doc_values" : true
            },
            "b" : {
                 "type" : "long",
                 "index" : "not_analyzed",
                 "doc_values" : true
            },
            "c" : {
                 "type" : "long",
                 "index" : "not_analyzed",
                 "doc_values" : true
            },
            "d" : {
                 "type" : "long",
                 "index" : "not_analyzed",
                 "doc_values" : true
            }
        }
    }
}'

二、提交ES查询

BoolQueryBuilder query = QueryBuilders.boolQuery();
            query.must(QueryBuilders.rangeQuery("time").gte(starttime).lt(endtime));
            query.must(QueryBuilders.termQuery("topic", topic));

            SearchRequestBuilder requestBuilder = client.prepareSearch(indices.split(",", -1))
                                                        .setTypes(types)
                                                        .setQuery(query)
                                                        .addSort("time", SortOrder.ASC)
                                                        .setSize(1);
            SearchResponse searchReponse = requestBuilder.execute().actionGet();

三、获取ES客户端

 Client client = null;
        String masterIP = getConfig("ES_NODES");
        try
        {
            Settings settings = ImmutableSettings.settingsBuilder()
                                                 .put("client.transport.sniff", true)
                                                 .put("cluster.name", getConfig("ES_CLUSTER_NAME"))
                                                 .build();
            TransportClient transportClient = new TransportClient(settings);
            if(masterIP != null)
            {
                for(String ip : masterIP.split(","))
                {
                    transportClient.addTransportAddress(new InetSocketTransportAddress(ip, 9300));
                }
                client = transportClient;
            }else {
                transportClient.close();
            }
        }
        catch(Exception e)
        {
            logger.error("init es transportclient[" + masterIP + "] error," + e.getMessage());
            throw new Exception(e.getMessage());
        }

四、获取索引操作

String dayIndex = idxPrefix + dateFormat.format(startCal.getTime()).substring(0, 8);
            IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest(dayIndex);

            if(indicesAdminClient.exists(indicesExistsRequest).actionGet().isExists())
            {
                return dayIndex;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值