ElasticSearch使用java-api方式查询

 PutMappingRequest mappingRequest =Requests.putMappingRequest(tableName).type(tableName);
XContentBuilder builder =
        XContentFactory.jsonBuilder().startObject().field("properties")
                .startObject().field(column).startObject()
                .field("type","text").field("fielddata", "true").endObject()
                .endObject().endObject();
mappingRequest.source(builder);
client.admin().indices().putMapping(mappingRequest).actionGet();
BoolQueryBuilder bQueryBuilder = QueryBuilders.boolQuery();
SearchRequestBuilder sbuilder = client.prepareSearch(tableName());
sbuilder.setQuery(bQueryBuilder);
sbuilder.addAggregation(tb);
sbuilder.addSort(排序列名, SortOrder.DESC);//添加排序,可多个
SearchResponse response = sbuilder.execute().actionGet();

-------------------------------------------------建立查询

//es集群配置信息
Settings settings = Settings.builder().put("cluster.name","es-cluster").put("client.transport.sniff", true).build();
//建立连接
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(地址, 端口号));
BoolQueryBuilder bQueryBuilder = QueryBuilders.boolQuery();
bQueryBuilder.must(QueryBuilders.matchPhraseQuery("tpss_acct_item_tp_nm", "4G"));
SearchResponse response = client.prepareSearch(表名)
//.setQuery(bQueryBuilder)
.setFrom(0).setSize(10).setExplain(false)
.execute().actionGet();
SearchHits shs = response.getHits();
System.out.println("总数:"+shs.getTotalHits());
for(SearchHit hit : shs){
System.out.println(hit.getSourceAsString());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值