ElasticSearch BoolQueryBuilder使用

package com.agileeagle.gf.dw.test;

import com.agileeagle.gf.dw.util.DateUtils;
import com.google.common.collect.Lists;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.*;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;

/**
 * Created by AlexWang on 2016/11/8.
 */
public class TestNew {
    public static final String  clusterName = "my-application";
    public static final String  host = "192.168.0.209";
    public static final int  port = 9300;
    private static final String TIMESTAMP = "@timestamp";
    private static final String DCREATETIME = "d_create_time";
    private static final String DEFAULTSTRING = "0";
    private static final String PARAMETER = "s_content";
    private static final String GROUPID = "n_group_id";
    private static final String NID = "n_id";
    private static final String QUANCOMMENTS = "quan-dynamic-comments";
    private static final String QUANINFO = "quan-dynamic-info";
    private static final String TOKEN_URL = "token.connect.url";
    private static final String GROUP_URL = "cid.connect.url";
    private static final int ESSIZE = 100;
    private static final int GROUP = 1;
    private static final int IM = 2;
    private static final int DYNAMIC = 3;
    private static final int STARTNUM = 0;

    private DateUtils dateUtils = new DateUtils();


    private TransportClient getTransportClient() {
        TransportClient transportClient = null;
        Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).put("client.transport.sniff", true).build();
        try {
            transportClient = new TransportClient.Builder().settings(settings).build()
                    .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(host, port)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return transportClient;
    }

    @org.junit.Test
    public void testNew() throws Exception {
        int info = 1;
        String startDate = "2016-10-01 00:00:00";
        String endDate = "2016-11-08 23:59:59";
        String torres = "测试";
        String uid = "";
        String groupId = "320";

        String[] indexName = {"group-message-2016.10"};
        String[] type = {"group-message"};

        List> mapList = getGroupInfoFromES(startDate, endDate, torres, uid, groupId, indexName, type );
        System.out.println(mapList);
    }


    private List> getGroupInfoFromES(String startDate, String endDate, String torres, String uid, String groupId, String[] indexName, String[] type) throws Exception{
        Client client = getTransportClient();

        SearchRequestBuilder srq = client.prepareSearch(indexName).setTypes(type);
        srq.setSearchType(SearchType.DFS_QUERY_AND_FETCH);

        BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery();
        if (Strings.isNullOrEmpty(torres)) {
//            MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
//            srq.setQuery(matchAllQueryBuilder);
            booleanQueryBuilder.must(QueryBuilders.matchAllQuery());
        } else {
//            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery(PARAMETER, torres).slop(0);
//            srq.setQuery(matchQueryBuilder);
            booleanQueryBuilder.must(QueryBuilders.matchPhraseQuery(PARAMETER, torres).slop(0));
        }
        if (!Strings.isNullOrEmpty(startDate) && !Strings.isNullOrEmpty(endDate)) {
            srq.setPostFilter(QueryBuilders.rangeQuery(TIMESTAMP).from(
                            dateUtils.getUTCTimeZoneTime(startDate)).to(dateUtils.getUTCTimeZoneTime(endDate))
            );
        }
        if (!Strings.isNullOrEmpty(uid)) {
//            MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(NID, uid);
//            srq.setQuery(matchQuery);
            booleanQueryBuilder.must(QueryBuilders.matchQuery(NID, uid));
        }
        if (!Strings.isNullOrEmpty(groupId)) {
//            MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(GROUPID, groupId);
//            srq.setQuery(matchQuery);
            booleanQueryBuilder.must(QueryBuilders.matchQuery(GROUPID, groupId));
        }
        srq.setQuery(booleanQueryBuilder).addSort(TIMESTAMP, SortOrder.DESC);

        SearchResponse response = srq.setFrom(STARTNUM).setSize(ESSIZE).execute().actionGet();
        SearchHits hits = response.getHits();

        List> mapList = Lists.newArrayList();
        for (int i = STARTNUM; i < hits.getHits().length; i++) {
            SearchHit hit = hits.getHits()[i];
            Map val = hit.getSource();
            mapList.add(val);
        }

        return mapList;
    }
}

 

你可能感兴趣的:(ElasticSearch)