ElasticSearch2.3.4之Java Api調用例子

ElasticSearch2.3.4

序號 覆蓋功能例子
1 客戶端連接初始化
2 filte無評分查詢用法
3 query有評分查詢用法
4 單字段分組用法
5 多字段分組用法
6 讀取有索引無存儲數據的用法
7 設置指定字段返回

代碼以下:

package com.curd.es;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.joda.time.DateTime;

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

public class ElasticSearchToolsTest {


	//elasticsearch2.3的客戶端實例
	static Client client=null;
	static {
		//設置集羣名字
		Settings settings = Settings.settingsBuilder()
		        .put("cluster.name", "search")
		        .put("client.transport.sniff", true)
		       . build();
		try {
			//初始化鏈接客戶端
			client = new TransportClient.Builder().settings(settings).build()
					.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.5",9300)))
					.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.6",9300)))
					.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.201.7",9300)));
		}catch (Exception e){
			e.printStackTrace();
		}

	}


	/****
	 * 測試讀取僅僅索引沒有存儲的字段,從倒排裏面加載數據
	 * 過濾數據
	 */
	public static void filterQueryString(){
		//構建查詢請求
		SearchRequestBuilder search=client.prepareSearch("monitor").setTypes("monitor");
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
				.must(QueryBuilders.queryStringQuery("*:*"))
				.filter(QueryBuilders.queryStringQuery("+mtitle:北京奇虎科技     +dtime:[1443196800000 TO 1451059200000] ").defaultOperator(Operator.AND));
				search.setQuery(boolQuery);
		search.addFieldDataField("t1");//索引,沒有存儲的域,從field data裏面加載數據
		search.addFieldDataField("t2");//索引,沒有存儲的域,從field data裏面加載數據
		search.addFieldDataField("t3");//索引,沒有存儲的域,從field data裏面加載數據
		search.addFieldDataField("dtime");//索引,沒有存儲的域,從field data裏面加載數據
		SearchResponse r = search.get();
		for(SearchHit hit:r.getHits()){
			String id=hit.getId();
			String index=hit.getIndex();
			String type=hit.getType();
			String t1=hit.getFields().get("t1").getValue();
			String t2=hit.getFields().get("t2").getValue();
			String t3=hit.getFields().get("t3").getValue();
			Long time=hit.getFields().get("dtime").getValue();
			Float score=hit.getScore();
			System.out.println(id+"\t"+t1+"\t"+t2+"\t"+t3+"\t"+time+"\t"+score);
		}
		System.out.println("總數量:"+r.getHits().getTotalHits());
		System.out.println("耗時:"+r.getTookInMillis());
	}


	public static void main(String[] args) throws Exception {

		//單個聚合字段,支持.寫法
		String aggField="data.subjectId";
//		testOneAggString(aggField);
		testFilter();
		client.close();

	}


	/***
	 * 每一天的select count(distinct(actid)) from talbe group by date
     */
	public static void countDistinctByField(){


		//構造search請求
		SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
		search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 8, 0, 0, 0).getMillis()
				+" TO "+new DateTime(2016, 8, 15, 0, 0, 0).getMillis()+"}"
		));
		search.setSize(0);
		//一級分組字段
		DateHistogramBuilder dateagg = AggregationBuilders.dateHistogram("dateagg");
		dateagg.field("@timestamp");//聚合時間字段
//		dateagg.interval(DateHistogramInterval.HOUR);//按小時聚合
		dateagg.interval(DateHistogramInterval.DAY);//按天聚合
//		dateagg.format("yyyy-MM-dd HH"); //格式化時間
		dateagg.format("yyyy-MM-dd"); //格式化時間
		dateagg.timeZone("Asia/Shanghai");//設置時區,注意若是程序部署在其餘國家使用時,使用Joda-Time來動態獲取時區 new DateTime().getZone()

		//二級分組字段
//		TermsBuilder twoAgg = AggregationBuilders.terms("stragg").field("actId");
		MetricsAggregationBuilder twoAgg = AggregationBuilders.cardinality("stragg").field("actId");

		//組裝聚合字段
		dateagg.subAggregation(twoAgg);
		//向search請求添加
		search.addAggregation(dateagg);
		//獲取結果
		SearchResponse r = search.get();
		Histogram h = r.getAggregations().get("dateagg");
		//獲得一級聚合結果裏面的分桶集合
		List<Histogram.Bucket> buckets = (List<Histogram.Bucket>) h.getBuckets();
		//遍歷分桶集
		for(Histogram.Bucket b:buckets){
			//讀取二級聚合數據集引用
			Aggregations sub = b.getAggregations();
			//獲取二級聚合集合
			Cardinality agg = sub.get("stragg");
			//獲取去重後的值
			long value = agg.getValue();
			//若是設置日期的format的時候,須要使用keyAsString取出,不然獲取的是UTC的標準時間
			System.out.println(b.getKeyAsString() +"  " +b.getDocCount()+" "+value);
		}
	}


	/***
	 *  獲取search請求的結果,並輸出打印結果信息
	 * @param search
	 * @throws Exception
     */
	public  static void showResult(SearchRequestBuilder search) throws Exception{
		SearchResponse r = search.get();//獲得查詢結果
		for(SearchHit hits:r.getHits()){
			//只能獲取addFields裏面添加的字段
//			System.out.println(hits.getFields().get("userId").getValue());
			//默承認會source裏面獲取所需字段
			System.out.println(hits.getSource().get("actId"));
			//注意不支持data.subjectName這樣的訪問方式
			//System.out.println(hits.getId()+"  "+hits.score()+"  "+data.get("subjectName"));
			//若是是個嵌套json,須要轉成map後,訪問其屬性
//			Map data=(Map) hits.getSource().get("data");
//			System.out.println(hits.getId()+"  "+hits.score()+"  "+data.get("subjectName"));


		}
		long hits=r.getHits().getTotalHits();//讀取命中數量
		System.out.println(hits);
	}

	/***
	 * 最新版elasticsearch2.3的query測試,結果會評分
	 * @throws Exception
     */
	public static void testQuery() throws Exception{
		SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
		String subjectName="語文";
		//注意查詢的時候,支持嵌套的json查詢,經過點符號訪問下層字段,讀取結果時不支持這種方式
 		search.setQuery(QueryBuilders.queryStringQuery("+data.subjectName:* -data.subjectName:"+subjectName+"  "));
		showResult(search);
	}

	/***
	 * 最新版的elasticsearch2.3的filterquery測試,結果不會評分
	 * @throws Exception
     */
	public static void testFilter() throws Exception{
		SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
		//第一個參數包含的字段數組,第二個字段排除的字段數組
//		search.setFetchSource(new String[]{"userId","actId"},null);
//		search.addFields("actId","userId"); //另外一種寫法
		String schoolName="沙河市第三小學";
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
				.must(QueryBuilders.queryStringQuery("*:*"))
				.filter(QueryBuilders.queryStringQuery("+data.subjectName:* +schoolName:"+schoolName).defaultOperator(Operator.AND));
		//設置query
		search.setQuery(boolQuery);
		//打印結果數據
		showResult(search);
	}

	/***
	 *  兩個字段分組測試,在時間的維度上加上任意其餘的字段聚合,相似group by field1,field2
	 * @throws Exception
     */
	public static void testTwoAggString() throws Exception{
		//構造search請求
		SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
		search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 10, 0, 0, 0).getMillis()
		+" TO "+new DateTime(2016, 8, 11, 0, 0, 0).getMillis()+"}"
		));
		//一級分組字段
		DateHistogramBuilder dateagg = AggregationBuilders.dateHistogram("dateagg");
		dateagg.field("@timestamp");//聚合時間字段
		dateagg.interval(DateHistogramInterval.HOUR);//按小時聚合
		dateagg.format("yyyy-MM-dd HH"); //格式化時間
		dateagg.timeZone("Asia/Shanghai");//設置時區,注意若是程序部署在其餘國家使用時,使用Joda-Time來動態獲取時區 new DateTime().getZone()

		//二級分組字段
		TermsBuilder twoAgg = AggregationBuilders.terms("stragg").field("module");
		//組裝聚合字段
		dateagg.subAggregation(twoAgg);
		//向search請求添加
		search.addAggregation(dateagg);
		//獲取結果
		SearchResponse r = search.get();
		Histogram h = r.getAggregations().get("dateagg");
		//獲得一級聚合結果裏面的分桶集合
		List<Histogram.Bucket> buckets = (List<Histogram.Bucket>) h.getBuckets();
		//遍歷分桶集
		for(Histogram.Bucket b:buckets){
			//讀取二級聚合數據集引用
			Aggregations sub = b.getAggregations();
			//獲取二級聚合集合
			StringTerms count = sub.get("stragg");
			//若是設置日期的format的時候,須要使用keyAsString取出,不然獲取的是UTC的標準時間
			System.out.println(b.getKeyAsString() +"  " +b.getDocCount());
			System.out.println("=============================================");
			for(Terms.Bucket bket:(List<Terms.Bucket>)count.getBuckets()){

				System.out.println(bket.getKeyAsString() +"  "+bket.getDocCount());
			}
			System.out.println("************************************************");

		}

	}


	/***
	 *  一個字段聚合,相似數據庫的group by field1
	 * @param field 測試聚合的字段
	 * @throws Exception
     */
	public static  void testOneAggString(String field)throws Exception{
		//構造search請求
		SearchRequestBuilder search=client.prepareSearch("userlog*").setTypes("logs");
		//查詢昨天的數據
		search.setQuery(QueryBuilders.queryStringQuery("@timestamp:[ "+new DateTime(2016, 8, 10, 0, 0, 0).getMillis()
				+" TO "+new DateTime(2016, 8, 11, 0, 0, 0).getMillis()+"}"
		));
		//聚合構造
		TermsBuilder termsBuilder = AggregationBuilders.terms("agg").field(field);
		//添加到search請求
		search.addAggregation(termsBuilder);
		//獲取結果
		SearchResponse searchResponse = search.get();
		//獲取agg標識下面的結果
		Terms agg1 = searchResponse.getAggregations().get("agg");
		//獲取bucket
		List<Terms.Bucket> buckets = (List<Terms.Bucket>) agg1.getBuckets();
		long sum=0;
		for(Terms.Bucket b:buckets){
			Aggregations sub = b.getAggregations();
			System.out.println(b.getKeyAsString()+"  "+b.getDocCount());
			sum+=b.getDocCount();
		}
		System.out.println("總數:"+sum);
	}

}

有什麼問題能夠掃碼關注微信公衆號:我是攻城師(woshigcs),在後臺留言諮詢。 技術債不能欠,健康債更不能欠, 求道之路,與君同行。 imagehtml

ElasticSearch2.x API變化官網連接

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/breaking-changes-2.0.htmljava

相關文章
相關標籤/搜索