ElasticSearch封装(创建索引,删除索引,创建Mapping,批量插入,批量删除,搜索)

package com.koolearn.framework.search.implement;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
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.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import com.koolearn.framework.common.utils.PropertiesConfigUtils;
import com.koolearn.framework.search.declare.Example;
import com.koolearn.framework.search.declare.ISearch;


public class SearchImp implements ISearch, InitializingBean, DisposableBean {
private Client searchClient = null;
private Logger logger = Logger.getLogger(SearchImp.class);
@Autowired
private String analyzerRegex = null;
@Autowired
private String wildcardRegex = null;
@Autowired
private List<String> clusterList = null;
@Autowired
private Boolean reCreateIndex = false;
@Autowired
private HashMap<String, ArrayList<String>> indexMap = null;


public void setIndexMap(HashMap<String, ArrayList<String>> indexMap) {
this.indexMap = indexMap;
}


public enum FieldType {
error, normal, date, wildcard;
}


private synchronized void close() {
if (this.searchClient == null) {
return;
}
this.searchClient.close();
}


/*
* 创建搜索客户端
* tcp连接搜索服务器
* 创建索引
* 创建mapping
* */
private synchronized boolean open() {
if (this.searchClient != null) {
return false;
}
try {
/*如果10秒没有连接上搜索服务器,即超时*/
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.ping_timeout", "10s")
.build();
/*创建搜索客户端*/
this.searchClient = new TransportClient(settings);
if (CollectionUtils.isEmpty(this.clusterList)) {
String cluster = PropertiesConfigUtils.getProperty("search.clusterList");
if (cluster != null) {
this.clusterList = Arrays.asList(cluster.split(","));
}
}
for (String item : this.clusterList) {
String address = item.split(":")[0];
int port = Integer.parseInt(item.split(":")[1]);
/*通过tcp连接搜索服务器*/
this.searchClient = ((TransportClient) this.searchClient)
.addTransportAddress(new InetSocketTransportAddress(address, port));
}
/*如果需要重新创建索引*/
if (this.reCreateIndex) {
Iterator<Entry<String, ArrayList<String>>> iterator = this.indexMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String,
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值