Elasticsearch5.2.2 过滤字段查询,指定返回字段数据

package com.query.test;

import java.net.InetAddress;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import com.es.allpeizhi.EsAll;

/**
 * 文件名 : ElasticSearchBulkIn.java
 * 包 名 :
 * 描 述 : 过滤字段查询,指定返回字段数据
 * 机能名称:
 * 技能ID :
 * 作 者 : smkj
 * 版 本 : V1.0
 */
public class searchGuolv {
    @Test
    public static void main(String[] args) throws Exception {
        EsAll esall = new EsAll();
        // 查询关键字
        String searchName = "重庆市政府采购网";
        Long nowTime = System.currentTimeMillis();
        
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName(esall.host), esall.prot);
        // 配置settiong
        Settings settings = Settings.builder().put("cluster.name", "my-application").build();
        
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        System.out.println("ES--ip地址:" + node.getAddress());
        
        String index = esall.IndexName;
        String type = esall.TypeName;
        // 指定要返回的字段
        String[] fields = new String[1];
        fields[0] = "TITLE"; // 字段1名称
        // 构造query
        SearchRequestBuilder requestBuilder = client.prepareSearch(index);
        // setFields 2.x版本
        requestBuilder.setFrom(0).setSize(10).setTimeout(TimeValue.timeValueMillis(10)).setTypes(type).setFetchSource(fields, null);// 排除字段null
        ;
        // bool 条件
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        MatchPhraseQueryBuilder tqb = QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT", keword);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchPhraseQueryBuilder tqb = QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT", searchName);
        
        boolQueryBuilder.must(tqb);
        // set query
        requestBuilder.setQuery(boolQueryBuilder);
        // get response
        SearchResponse response = requestBuilder.execute().actionGet();
        // 遍历返回的字段
        SearchHits searchHits = response.getHits();
//        List id = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            System.out.println(hit.getSource().get("TITLE"));
//            System.out.println(hit.getFields().get("TITLE").getValue().toString());//2.x版本
        }
        client.close();
        System.out.println("命中总数量:" + response.getHits().getTotalHits());
        
//        System.out.println(id.size());
        System.out.println("耗时:" + (System.currentTimeMillis() - nowTime) / 1000 + "秒");
    }
    
}
 

你可能感兴趣的:(Elasticsearch5.2.2 过滤字段查询,指定返回字段数据)