import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import kd.bos.exception.BosErrorCode;
import kd.bos.exception.KDException;
import kd.bos.fulltext.common.ConstantKeys;
/**
* Rest High Level 方式与 Elastic Search 交互
*
* Default port: 9200
*
* index/indice: 相当于mysql中的data base
* type: 相当于mysql中的表
* document: 相当于mysql中的行
* field: 相当于mysql中的字段
*
* 若与服务端版本不一致可能出现版本问题
* @author rd_jianbin_lai
*/
public class HighLevelAccess {
private HighLevelAccess(){
}
private static final String RETRY_ON_CONFLICT_KEY = "fulltext.retryonconflict";
public static RestHighLevelClient createRestHighLevelClient(String ip, int port, String username, String password) {
//use username and password for authentication
if(username != null && password != null) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
return new RestHighLevelClient(RestClient.builder(new HttpHost(ip, port))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}));
}
//without authentication
return new RestHighLevelClient(RestClient.builder(new HttpHost(ip, port, "http")));
}
public static boolean isIndexExist(RestHighLevelClient client, String index) throws IOException {
GetIndexRequest req = new GetIndexRequest(index);
return client.indices().exists(req, RequestOptions.DEFAULT);
}
public static void createIndex(RestHighLevelClient client, String index, Map settings)
throws IOException {
CreateIndexRequest req = new CreateIndexRequest(index);
client.indices().create(req.settings(settings), RequestOptions.DEFAULT);
}
public static void createIndexMapping(RestHighLevelClient client, String index, String type,String fieldsMapping) throws IOException {
PutMappingRequest req = new PutMappingRequest(index);
req.type(type);
req.source(fieldsMapping, XContentType.JSON);
AcknowledgedResponse res = client.indices().putMapping(req, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
throw new KDException(BosErrorCode.fulltextException, "Failed to create index:" + index + res.toString());
}
}
public static void updateIndexSettings(RestHighLevelClient client, String[] indexs,
Map indexSettings) throws IOException {
String shardsNum = "number_of_shards";
if (indexSettings.containsKey(shardsNum)) {
indexSettings.remove(shardsNum);
}
UpdateSettingsRequest req = new UpdateSettingsRequest(indexs);
req.settings(indexSettings);
AcknowledgedResponse res = client.indices().putSettings(req, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
throw new KDException(BosErrorCode.fulltextException, "failed to update indexs settings:" + res.toString());
}
}
public static List batchSave(RestHighLevelClient client, String index, String type,
List