Elasticsearch5.2.2查询,返回全部字段数据

package com.query.test;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

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.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.es.allpeizhi.EsAll;

/**
 * 文件名 : ElasticSearchBulkIn.java
 * 包 名 :
 * 描 述 : 可查询es所有字段
 * 机能名称:
 * 技能ID :
 * 作 者 : smkj
 * 版 本 : V1.0
 */
public class JavaESQueryEnd {

    @SuppressWarnings("resource")
    @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());

        /**
         * 组合查询
         * must(QueryBuilders) : AND
         * mustNot(QueryBuilders): NOT
         * should: : OR
         */
        List alltitle = new ArrayList<>();
//        String name = "重庆市政府采购网 ";
        String[] names = searchName.split(" ");
        for (int j = 0, len = names.length; j < len; j++) {
            names[j].toString();

//            String c=".should(QueryBuilders.matchPhraseQuery("+a+","+b+"))";
            SearchResponse searchResponse = client.prepareSearch(esall.IndexName).setTypes(esall.TypeName)
                    // responsebuilder.setPostFilter(QueryBuilders.missingQuery("title"))
                    .setQuery(QueryBuilders.boolQuery()
                            // .should(QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT", b))
                            // .should(QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("DISPLAY_CONTENT", b)).boost(1f))
                            .must(QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT", searchName))
                    // .must(QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT","安置房"))
                    // .should(QueryBuilders.matchPhraseQuery("DISPLAY_CONTENT", "安置房"))
                    )// matchQuery
                    .setFrom(0).setSize(10)
                    // .setSearchType(SearchType.DFS_QUERY_AND_FETCH).setSize(250).setScroll(TimeValue.timeValueMinutes(1))
                    .setExplain(true)// 按查询匹配度排序
                    .execute().actionGet();
            SearchHits hits = searchResponse.getHits();
            for (int i = 0; i < hits.getHits().length; i++) {
//                       System.out.println(hits.getHits()[i].getSourceAsString());
                JSONObject jsonObject = JSONObject.parseObject(hits.getHits()[i].getSourceAsString());// 获取到key为shoppingCartItemList的值
                String conton = jsonObject.getString("TITLE");
//                       String name=jsonObject.ge4tString("TITLE");
                alltitle.add(conton);
//                           System.out.println(jsonObject);
            }
            System.out.println("命中总数量:" + searchResponse.getHits().getTotalHits());
        }
        System.out.println(alltitle);
        System.err.println("------------------------es查询完成------------------------");
//                     List personList = elasticsearchTemplate.queryForList(searchQuery, Person.class);
        System.out.println("耗时:" + (System.currentTimeMillis() - nowTime) / 1000 + "秒");
    }

}
 

你可能感兴趣的:(Elasticsearch5.2.2查询,返回全部字段数据)