elasticsearch Java接口汇总 博客分类: 技术总结 elasticsearchJava接口汇总
服务器端和客户端的elasticsearch版本号须一致
setExplain(true)设置是否按查询匹配度排序
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.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;
public class Searcher {
private TransportClient client;
public void init(){
client = new TransportClient().addTransportAddress(
new InetSocketTransportAddress("10.18.6.90", 9300));// localhost9300
}
//生成索引
public void generateIndex() {
Map<String, Object> json = new HashMap<String, Object>();
json.put("user", "hdidkv");
json.put("postDate", new Date());
json.put("message", "哥大富大贵");
IndexResponse response = this.client
.prepareIndex("twitter", "tweet", "1")
.setSource(json)
.execute()
.actionGet();
}
//查看索引
public void getIndex() {
GetResponse response = client.prepareGet("twitter", "test", "1")
.execute().actionGet();
Map<String, Object> rpMap = response.getSource();
if (rpMap == null) {
System.out.println("empty");
return;
}
Iterator<Entry<String, Object>> rpItor = rpMap.entrySet().iterator();
while (rpItor.hasNext()) {
Entry<String, Object> rpEnt = rpItor.next();
System.out.println(rpEnt.getKey() + " : " + rpEnt.getValue());
}
}
//创建一个termQuery查询
public void searchIndex() {
QueryBuilder qb = QueryBuilders.termQuery("user", "hdidkv");
SearchResponse scrollResp = client.prepareSearch("twitter")
//.setSearchType(SearchType.SCAN)
.setTypes("tweet")
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).execute().actionGet(); //100 hits per shard will be returned for each scroll
//Scroll until no hits are returned
while (true) {
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).
setScroll(new TimeValue(600000)).execute().actionGet();
System.out.println(scrollResp.getHits().getTotalHits());
for (SearchHit hit : scrollResp.getHits()) {
Iterator<Entry<String, Object>> rpItor = hit.getSource().entrySet().iterator();
while (rpItor.hasNext()) {
Entry<String, Object> rpEnt = rpItor.next();
System.out.println(rpEnt.getKey() + " 1:1 " + rpEnt.getValue());
}
}
if (scrollResp.getHits().hits().length == 0) {
break;
}
}
}
//删除索引
public void deleteIndex() {
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1")
.execute().actionGet();
}
//关闭连接
public void closeClient() {
this.client.close();
}
//练习使用elasticsearch的Java接口
public static void main(String[] args) {
//TransportClient client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("10.18.6.90", 9200));
Searcher searcher = new Searcher();
searcher.init();
searcher.generateIndex();
//searcher.getIndex();
searcher.searchIndex();
searcher.closeClient();
}
}