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,
ElasticSearch封装(创建索引,删除索引,创建Mapping,批量插入,批量删除,搜索)
最新推荐文章于 2024-04-01 10:32:51 发布