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
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.