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<Map<String,Object>> mapList = getGroupInfoFromES(startDate, endDate, torres, uid, groupId, indexName, type );
        System.out.println(mapList);
    }


    private List<Map<String,Object>> 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<Map<String,Object>> mapList = Lists.newArrayList();
        for (int i = STARTNUM; i < hits.getHits().length; i++) {
            SearchHit hit = hits.getHits()[i];
            Map<String, Object> val = hit.getSource();
            mapList.add(val);
        }

        return mapList;
    }
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值