Elasticsearch的使用
我这边是以elasticsearch-2.4.3为例:引入maven
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
1、创建连接器:
import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import java.net.InetAddress; import java.net.UnknownHostException; public class EsTcpClient { private static TransportClient client; /** * 获取TCP 客户端 * * @return */ public static synchronized TransportClient getClient() { if (client == null) { build(); } return client; } /** * 关闭客户端 */ public static void close() { if (client != null) { client.close(); } } /** * 建立连接 * * @return */ private static void build() { try { //特别注意:如果cluster 起了名字,需要在连接时指定名字,否则验证客户端连接的不是默认集群elasticsearch,会忽略,则无法找到节点 Settings settings = Settings.settingsBuilder() .put("cluster.name", "elasticsearch").build(); // .put("client.transport.ignore_cluster_name", true).build(); // 忽略集群名字验证, 打开后集群名字不对也能连接上 client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName('localhost'), 9300)); } catch (UnknownHostException e) { throw new RuntimeException(e); } } }
impl类
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.count.CountResponse; 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.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortOrder; import com.gky.common.constant.ElasticSearchIndexTypeConstant.IndexType; import com.gky.common.util.GsonHolder; @SuppressWarnings("deprecation") public class ElasticSearchTcpDaoImpl{ //TCP连接客户端: private TransportClient client = null; public ElasticSearchTcpDaoImpl(TransportClient client) { this.client = client; } public TransportClient getClient() { return client; } public boolean careateIndex(String Mapperjson, IndexType indexType) { CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(indexType.INDEX()).addMapping(indexType.INDEX(), Mapperjson).get(); return createIndexResponse.isAcknowledged(); } public boolean insert(Object doc,Long id,IndexType indexType) { String json = GsonHolder.gson.toJson(doc); IndexResponse response = client.prepareIndex(indexType.INDEX(), indexType.TYPE(), String.valueOf(id)).setSource(json).get(); return response.isCreated(); } public boolean update(Object doc,Long oid,IndexType indexType) { String json = GsonHolder.gson.toJson(doc); UpdateResponse response = client.prepareUpdate(indexType.INDEX(), indexType.TYPE(), String.valueOf(oid)) .setDoc(json) .get(); return !response.isCreated(); } public boolean delete(long id,IndexType indexType) { DeleteResponse response = client.prepareDelete(indexType.INDEX(), indexType.TYPE(), String.valueOf(id)).get(); return response.isFound(); } public Object searchById(Class<?> cls, long id,IndexType indexType) { GetResponse response = client.prepareGet(indexType.INDEX(), indexType.TYPE(), String.valueOf(id)).get(); if (response.isExists()) { String json = response.getSourceAsString(); return Gson.gson.fromJson(json, cls); } return null; } public List<Object> searchByUserId(Class<?> cls, long id,IndexType indexType, Integer pageStart, Integer pageSize) { QueryBuilder filter = new BoolQueryBuilder() .must(QueryBuilders.termQuery("modifiedId", id)); SearchResponse response = client.prepareSearch(indexType.INDEX()) .setTypes(indexType.TYPE()) .setQuery(filter) .setFrom(pageStart) .addSort("addTime", SortOrder.DESC) .setSize(pageSize) .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); List<Object> objs = new ArrayList<>(); for (SearchHit hit : hits) { Object obj= GsonHolder.gson.fromJson(hit.getSourceAsString(), cls); objs.add(obj); } return objs; } /** * 全查 */ public List<Object> searchAll(Class<?> cls,IndexType indexType, Integer pageStart, Integer pageSize,String start,String end,String model,String makeObject,String modifiedName) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if(indexType.name().equals("LOGS")){ if (StringUtils.isNotBlank(model)) { //通配符查询 boolQueryBuilder.must(QueryBuilders.wildcardQuery("model", model)); } if (StringUtils.isNotBlank(makeObject)) { //查询解析查询字符串 boolQueryBuilder.must(QueryBuilders.queryStringQuery(makeObject).field("content")); } if (StringUtils.isNotBlank(modifiedName)) { boolQueryBuilder.must(QueryBuilders.queryStringQuery(modifiedName).field("modifiedName")); } }else{ if (StringUtils.isNotBlank(start)) { //范围内查询 boolQueryBuilder.must(QueryBuilders.rangeQuery("addTime").format(start)); } if (StringUtils.isNotBlank(end)) { boolQueryBuilder.must(QueryBuilders.rangeQuery("addTime").to(end)); } if (StringUtils.isNotBlank(model)) { //通配符查询 boolQueryBuilder.must(QueryBuilders.wildcardQuery("model", model)); } if (StringUtils.isNotBlank(makeObject)) { //查询解析查询字符串 boolQueryBuilder.must(QueryBuilders.queryStringQuery(makeObject).field("makeObject")); } } SearchResponse response = client.prepareSearch(indexType.INDEX()) .setTypes(indexType.TYPE()) //.setQuery(QueryBuilders.matchAllQuery()) //.addSort(SortBuilders.fieldSort("addTime").order(SortOrder.DESC)) .addSort("addTime", SortOrder.DESC) .setFrom(pageStart) .setSize(pageSize) .setQuery(boolQueryBuilder) .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); List<Object> objs = new ArrayList<>(); for (SearchHit hit : hits) { Object obj= Gson.gson.fromJson(hit.getSourceAsString(), cls); objs.add(obj); } return objs; } public List<Object> searchAll(Class<?> cls,IndexType indexType, Integer pageStart, Integer pageSize, String mode) { QueryBuilder filter = new BoolQueryBuilder() .must(QueryBuilders.termQuery("searchMode", mode)); SearchResponse response = client.prepareSearch(indexType.INDEX()) .setTypes(indexType.TYPE()) //.setQuery(QueryBuilders.matchAllQuery()) .setQuery(filter) //.addSort(SortBuilders.fieldSort("addTime").order(SortOrder.DESC)) .addSort("addTime", SortOrder.DESC) .setFrom(pageStart) .setSize(pageSize) .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); List<Object> objs = new ArrayList<>(); for (SearchHit hit : hits) { Object obj= Gson.gson.fromJson(hit.getSourceAsString(), cls); objs.add(obj); } return objs; } public List<Object> searchAll(Class<?> cls,IndexType indexType, Integer pageStart, Integer pageSize, String mode,Object modifierId) { QueryBuilder filter = new BoolQueryBuilder() .must(QueryBuilders.termQuery("searchMode", mode)) .must(QueryBuilders.termQuery("modifierId", modifierId)); SearchResponse response = client.prepareSearch(indexType.INDEX()) .setTypes(indexType.TYPE()) //.setQuery(QueryBuilders.matchAllQuery()) .setQuery(filter) //.addSort(SortBuilders.fieldSort("addTime").order(SortOrder.DESC)) .addSort("addTime", SortOrder.DESC) .setFrom(pageStart) .setSize(pageSize) .execute() .actionGet(); SearchHit[] hits = response.getHits().getHits(); List<Object> objs = new ArrayList<>(); for (SearchHit hit : hits) { Object obj= Gson.gson.fromJson(hit.getSourceAsString(), cls); objs.add(obj); } return objs; } /** * total */ public Integer getTotal(IndexType indexType) { CountResponse countResponse = client.prepareCount(indexType.INDEX()).setTypes(indexType.TYPE()).get(); Long logt=countResponse.getCount(); return logt.intValue(); } public Integer getTotal(IndexType indexType,Long oid) { QueryBuilder filter = new BoolQueryBuilder() .must(QueryBuilders.termQuery("modifiedId", oid)); CountResponse countResponse = client.prepareCount(indexType.INDEX()).setTypes(indexType.TYPE()).setQuery(filter).get(); Long logt=countResponse.getCount(); return logt.intValue(); } public Integer getTotal(IndexType indexType,String start,String end,String model,String makeObject,String modifiedName) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if(indexType.name().equals("LOG")){ if (StringUtils.isNotBlank(model)) { boolQueryBuilder.must(QueryBuilders.wildcardQuery("modifiyMode", model)); } if (StringUtils.isNotBlank(makeObject)) { boolQueryBuilder.must(QueryBuilders.queryStringQuery(makeObject).field("content")); } if (StringUtils.isNotBlank(modifiedName)) { boolQueryBuilder.must(QueryBuilders.queryStringQuery(modifiedName).field("modifiedName")); } }else{ if (StringUtils.isNotBlank(start)) { boolQueryBuilder.must(QueryBuilders.rangeQuery("addTime").format(start)); } if (StringUtils.isNotBlank(end)) { boolQueryBuilder.must(QueryBuilders.rangeQuery("addTime").to(end)); } if (StringUtils.isNotBlank(model)) { boolQueryBuilder.must(QueryBuilders.wildcardQuery("model", model)); } if (StringUtils.isNotBlank(makeObject)) { boolQueryBuilder.must(QueryBuilders.queryStringQuery(makeObject).field("makeObject")); } } CountResponse countResponse = client.prepareCount(indexType.INDEX()).setTypes(indexType.TYPE()).setQuery(boolQueryBuilder).get(); Long logt=countResponse.getCount(); return logt.intValue(); } }
Gson类
public class Gson { public static Gson gson; public static JsonParser jsonParser; static { gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); jsonParser = new JsonParser(); } public static JsonElement getTopLevelElement(String json, String fieldName) { JsonObject jsonObject = GsonHolder.jsonParser.parse(json).getAsJsonObject(); return jsonObject.get(fieldName); } }