ElasticSearch的java API基本操作


package test;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

/**
 * ElasticSearch的java API基本操作
 * @author Administrator
 *
 */
public class ElasticSearchTest {

	private TransportClient client;

	//1、获取client实例,链接本地9300端口
	public void getClient(){
		
		Settings settings = ImmutableSettings.settingsBuilder()
				.put("cluster.name", "foxCluster").build();
		client = new TransportClient(settings)
			.addTransportAddress(new InetSocketTransportAddress(
				"localhost", 9300));
		
	}
	
	//2、生成一个索引
	public void generateIndex(){
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("user", "kimchy");
		json.put("postDate", new Date());
		json.put("message", "trying out Elastic Search");
		
		IndexResponse response = client.prepareIndex("twitter2", "tweet2", "1")
				.setSource(json)
				.execute().actionGet();
	}
	
	//3、查询某个索引
	public void getIndex(){
		
		GetResponse response = client.prepareGet("twitter", "tweet", "1")
				.execute().actionGet();
		Map<String, Object> map = response.getSource();
		if(map == null){
			System.out.println("empty");
			return;
		}
		
		Iterator<Entry<String, Object>> it = map.entrySet().iterator();
		while(it.hasNext()){
			Entry<String, Object> e = it.next();
			System.out.println(e.getKey()+":"+e.getValue());
		}
		
	}
	
	//4、搜索
	public void searchIndex(){
		
		QueryBuilder qb = QueryBuilders.termQuery("user", "kimchy");
		SearchResponse searchResponse = client.prepareSearch("twitter")
				.setSearchType(SearchType.SCAN)
				.setScroll(new TimeValue(5000))
				.setQuery(qb)
				.setSize(100)
				.execute().actionGet();
		
		while(true){
			
			searchResponse = client.prepareSearchScroll(searchResponse.getScrollId())
					.setScroll(new TimeValue(5000))
					.execute().actionGet();
			
			for(SearchHit hit : searchResponse.getHits()){
				
				Iterator<Entry<String,Object>> it = hit.getSource()
						.entrySet().iterator();
				while(it.hasNext()){
					Entry<String, Object> e = it.next();
					System.out.println(e.getKey() + ":" + e.getValue());
				}
				
			}
			
			if(searchResponse.getHits().hits().length == 0)
				break;
			
		}
	}
	
	//5、删除
	public void deleteIndex(){
		
		DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
				.execute().actionGet();
	}
	
	//关闭client链接
	public void closeClient(){
		client.close();
	}
	
	public static void main(String[] args) {
		
		ElasticSearchTest test = new ElasticSearchTest();
		
		test.getClient();
		test.generateIndex();
//		test.getIndex();
		
//		test.deleteIndex();
	}
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值