java API参考
package com.hfqx.decodelpd.dao;
import com.hfqx.decodelpd.utils.PropertiesUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Requests;
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.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import javax.annotation.PreDestroy;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
* description
*
* @author ${user}
* @Time 2019-06-19
*/
public class ElasticSearch {
private static String eshost;
private static int esport;
private static String esclustername;
private static TransportClient client = null;
// private static BulkRequestBuilder bulkRequest = null;
private static PropertiesUtils properties = new PropertiesUtils();
private SearchRequestBuilder searchRequestBuilder = null;
static {
eshost = properties.getProperty("es.node-host");
esport = Integer.valueOf(properties.getProperty("es.node-port"));
esclustername = properties.getProperty("es.cluster-name");
System.out.println(eshost);
System.out.println(esport);
System.out.println(esclustername);
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", esclustername).build();
client = new TransportClient(settings);
try {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(eshost), 9300));
// bulkRequest = client.prepareBulk();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
* 构建索引 mapping
* @return
*/
public XContentBuilder getMapping() {
XContentBuilder mapping = null;
try {
mapping = jsonBuilder().startObject()
.startObject("properties")
.startObject("province")
.field("type","string")
.field("index","not_analyzed")
.endObject()
.startObject("city")
.field("type","string")
.field("index","not_analyzed")
.endObject()
.startObject("county")
.field("type","string")
.field("index","not_analyzed")
.endObject()
.endObject()
.endObject();
} catch (IOException e) {
e.printStackTrace();
}
return mapping;
}
/**
* apply to create mapping that implement the fields
* @param index
* @param type
*/
// 创建映射关系
public void createMapping(String index, String type) {
if ( !existIndex(index) ) {
createIndex(index);
}
PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(getMapping());
client.admin().indices().putMapping(mappingRequest).actionGet();
}
// 创建索引
public void createIndex(String index) {
CreateIndexRequest request = new CreateIndexRequest(index);
client.admin().indices().create(request);
}
// 检查索引是否存储
public boolean existIndex(String index) {
boolean flag = client.admin().indices().exists(new IndicesExistsRequest()
.indices(new String[]{index})).actionGet().isExists();
return flag;
}
public void SyncIndex(String index, String type, String id, Map<String, Object> map) {
BulkRequestBuilder bulkRequest = null;
bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareIndex(index, type, id).setSource(map));
bulkRequest.execute().actionGet();
return;
}
public List<Map<String, Object>> SearchByQuery(String index, String type, List<QueryBuilder> queryBuildersList) {
List<Map<String, Object>> result = new ArrayList<>();
searchRequestBuilder = client.prepareSearch(index).setTypes(type).setSearchType(SearchType.DEFAULT);
// for(QueryBuilder builder:queryBuildersList) {
// searchRequestBuilder.setQuery(builder);
// }
searchRequestBuilder.setQuery(QueryBuilders.boolQuery().must(queryBuildersList.get(0)).must(queryBuildersList.get(1)));
// SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
SearchHit[] hitArray = searchRespons.getHits().getHits();
for( int i = 0; i < hitArray.length; i++ ) {
System.out.println(hitArray[i].id());
System.out.println(hitArray[i].sourceAsMap());
result.add(hitArray[i].sourceAsMap());
}
return result;
}
}