<div class="iteye-blog-content-contain" style="font-size: 14px"><
package org.dennisit.elastic.process;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.dennisit.entity.DataFactory;
import org.dennisit.entity.Medicine;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
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.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ElasticSearchHandler {
private Client client;
public ElasticSearchHandler(){
try
{
Settings settings = Settings.settingsBuilder().put("cluster.name", "test-elasticsearch").build();
client = TransportClient.builder()
.settings(settings)
.build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.229.85"), 9300))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.229.86"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 建立索引,索引建立好之后,会在elasticsearch-0.20.6\data\elasticsearch\nodes\0创建所以你看
* @param indexName 为索引库名,一个es集群中可以有多个索引库。 名称必须为小写
* @param indexType Type为索引类型,是用来区分同索引库下不同类型的数据的,一个索引库下可以有多个索引类型。
* @param jsondata json格式的数据集合
*
* @return
*/
public void createIndexResponse(String indexname, String type, List<String> jsondata){
//创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据
IndexRequestBuilder requestBuilder = client.prepareIndex(indexname, type).setRefresh(true);
for(int i=0; i<jsondata.size(); i++){
requestBuilder.setSource(jsondata.get(i)).execute().actionGet();
}
}
public void deleteIndexResponse(String indexname){
//创
client.admin().indices().prepareDelete(indexname).execute().actionGet();
// client.prepareDelete().setIndex(indexname).execute().actionGet();
}
public void deleteIndexResponse(String indexname, String type, List<String> jsondata){
//创建索引库 需要注意的是.setRefresh(true)这里一定要设置,否则第一次建立索引查找不到数据
if(jsondata == null || type == null )
{
this.deleteIndexResponse(indexname);
}
else
{
for(String str:jsondata)
{
client.prepareDelete(indexname,type,str).execute().actionGet();
}
}
}
/**
* 创建索引
* @param client
* @param jsondata
* @return
*/
public IndexResponse createIndexResponse(String indexname, String type,String jsondata){
IndexResponse response = client.prepareIndex(indexname, type)
.setSource(jsondata)
.execute()
.actionGet();
return response;
}
/**
* 执行搜索
* @param queryBuilder
* @param indexname
* @param type
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public List<Medicine> searcher(QueryBuilder queryBuilder, String indexname, String type) throws JsonParseException, JsonMappingException, IOException{
List<Medicine> list = new ArrayList<Medicine>();
SearchResponse searchResponse = client.prepareSearch(indexname).setTypes(type)
.setQuery(queryBuilder)
.execute()
.actionGet();
SearchHits hits = searchResponse.getHits();
System.out.println("查询到记录数=" + hits.getTotalHits());
SearchHit[] searchHists = hits.getHits();
if(searchHists.length>0){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SearchHits searchHits = searchResponse.getHits();
for(SearchHit searchHit : searchHits){
String json = searchHit.getSourceAsString();
Medicine model = objectMapper.readValue(json ,Medicine.class);
list.add(model);
}
}
return list;
}
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ElasticSearchHandler esHandler = new ElasticSearchHandler();
List<String> jsondata = DataFactory.getInitJsonData();
String indexname = "indexdemo1";
String type = "typedemo1";
esHandler.createIndexResponse(indexname, type, jsondata);
QueryBuilder queryBuilder = QueryBuilders
.boolQuery()
.must(QueryBuilders.commonTermsQuery("name", "颗粒"))
.must(QueryBuilders.commonTermsQuery("funct", "解热镇痛"));
List<Medicine> result = esHandler.searcher(queryBuilder, indexname, type);
for(int i=0; i<result.size(); i++){
Medicine medicine = result.get(i);
System.out.println("(" + medicine.getId() + ")药品名称:" +medicine.getName() + "\t\t" + medicine.getFunct());
}
// esHandler.deleteIndexResponse("indexdemo1");
}
}
/div>