es php配置返回的字段,ElasticSearch API实现返回指定的字段

source中包含多个字段,想在结果中返回用户指定的字段。下面的代码实现此功能。

SearchRequest request = new SearchRequest();

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

searchSourceBuilder.from(0);

searchSourceBuilder.size(4);

String[] fields = {"hostIP","pathFile"};

FetchSourceContext sourceContext = new FetchSourceContext(fields);

//searchSourceBuilder.fetchSource(sourceContext);

boolQueryBuilder.should(new MatchQueryBuilder("hostIP", "10.184.133.213").operator(

MatchQueryBuilder.Operator.AND).type(MatchQueryBuilder.Type.PHRASE));

searchSourceBuilder.query(boolQueryBuilder);

request.extraSource(searchSourceBuilder);

request.indices("ies_logcenter:pacihppackifeaaekhffmdafhiblbggm_20160706");

request.types("logCenter");

for(SearchHit hit:client.search(request).actionGet().getHits())

{

System.out.println(hit.getSourceAsString());

}

result

{"category":"logAgent","hostIP":"10.184.133.213","hostName":"","lineNum":14677840880220008,"logContent":"2016-07-06 13:48:08,022 INFO [http-bio-127.0.0.1-31803-exec-3][ROOT][logaccessservice.transfer.LogInfoQueue 148] log array size is 10","logTime":1467784088022,"pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log","tags":""}

{"category":"logAgent","hostIP":"10.184.133.213","hostName":"","lineNum":14677840880230009,"logContent":"2016-07-06 13:48:08,023 INFO [http-bio-127.0.0.1-31803-exec-3][ROOT][logaccessservice.transfer.LogSendThreadPool 51] current thread num:0","logTime":1467784088023,"pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log","tags":""}

{"category":"logAgent","hostIP":"10.184.133.213","hostName":"","lineNum":14677840880230010,"logContent":"2016-07-06 13:48:08,023 INFO [pool-5-thread-5793][ROOT][logaccessservice.transfer.JsonCvt 55] lineNum is 14676114189370001","logTime":1467784088023,"pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log","tags":""}

{"category":"logAgent","hostIP":"10.184.133.213","hostName":"","lineNum":14677840880240014,"logContent":"2016-07-06 13:48:08,024 INFO [pool-5-thread-5793][ROOT][logaccessservice.transfer.JsonCvt 55] lineNum is 14676114197970005","logTime":1467784088024,"pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log","tags":""}

如果指定resouce字段

searchSourceBuilder.fetchSource(sourceContext);

结果就变为:

{"hostIP":"10.184.133.213","pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log"}

{"hostIP":"10.184.133.213","pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log"}

{"hostIP":"10.184.133.213","pathFile":"//LogAccessService/logaccessservice-3176-0/log/root.log"}

{"hostIP":"10.184.133.213","pathFile":"/LogAccessService/logaccessservice-3176-0/log/root.log"}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值