TestElasticsearchFilter

package com.itheima.elasticsearch.test;

import java.awt.MultipleGradientPaint;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.elasticsearch.domain.Article;

public class TestElasticsearchFilter {

private Client client;


/**获取Transport Client
 * @throws Exception */
@Before
public void getClient() throws Exception{
	// ElasticSearch 服务默认端口 9300
	client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(
			InetAddress.getByName("127.0.0.1"),9300));
}


/**
 * 范围过滤器
 */
@Test
public void testFilterRange() throws Exception{
	SearchResponse searchResponse = client.prepareSearch("blog")
			.setTypes("article")
			.setPostFilter(QueryBuilders.rangeQuery("id").from(1).to(10))
			.get();
	SearchHits hits = searchResponse.getHits();
	
	this.searchValue(hits);
	//关闭
	client.close();
}

/**
 * 布尔过滤器
 */
@Test
public void testFilterBool() throws Exception{
	SearchResponse searchResponse = client.prepareSearch("blog")
			.setTypes("article")
			.setPostFilter(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title", "搜索"))
					.must(QueryBuilders.termQuery("content", "搜索")))
			.setRequestCache(true)//放置到缓存
			.get();
	SearchHits hits = searchResponse.getHits();
	
	this.searchValue(hits);
	//关闭
	client.close();
}



// 显示查询的结果数据
private void searchValue(SearchHits hits) {
	System.out.println("查询的结果数量有"+hits.getTotalHits()+"条");
	System.out.println("结果中最多的分数:"+hits.getMaxScore());
	// 遍历每条数据
	Iterator iterator = hits.iterator();
	while(iterator.hasNext()){
		SearchHit searchHit = iterator.next();
		// System.out.println("所有的数据JSON的数据格式:"+searchHit.getSourceAsString());
		System.out.println("当前文章的得分:"+searchHit.getScore());
		// 获取每个字段的数据
		System.out.println("id:"+searchHit.getSource().get("id"));
		System.out.println("title:"+searchHit.getSource().get("title"));
		System.out.println("content:"+searchHit.getSource().get("content"));
		System.out.println("comment:"+searchHit.getSource().get("comment"));
		System.out.println("**********************************************");
		for(Iterator ite = searchHit.iterator();ite.hasNext();){
			SearchHitField next = ite.next();
			System.out.println(next.getValues());
		}
	}
}

}

你可能感兴趣的:(code,es)