@Component public class ElasticsearchBaseClient<T extends BaseT> extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
private co.elastic.clients.elasticsearch.ElasticsearchClient |
client |
private static String |
DEFAULT_SCRIPT_LANG |
private ElasticsearchConfig.EsConfig |
esConfig |
private static Integer |
PAGE_ONE |
private static Integer |
PAGE_SIZE |
private static String |
SIMPLE_SCRIPT_ID |
private co.elastic.clients.transport.rest_client.RestClientTransport |
transport |
| 构造器和说明 |
|---|
ElasticsearchBaseClient(co.elastic.clients.elasticsearch.ElasticsearchClient client,
co.elastic.clients.transport.rest_client.RestClientTransport transport)
Instantiation
|
ElasticsearchBaseClient(ElasticsearchConfig.EsConfig esConfig)
Instantiation
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
createIndex(String index,
String indexJson)
create index by json
|
boolean |
createIndexByJsonFile(String index,
String jsonPath)
create index by json file
|
private void |
createQueryScript(String scriptId,
String scriptSource,
String language)
create script
|
void |
createQueryScript(String scriptId,
String scriptSource,
String language,
boolean force)
create script
|
private void |
createSimpleScriptTemplate()
create simple script template
|
co.elastic.clients.elasticsearch.core.BulkResponse |
deleteBulkWithList(String index,
List<String> ids)
delete doc by list
|
String |
deleteDoc(String index,
String id)
delete doc by id
|
boolean |
deleteIndex(String index)
delete index
|
boolean |
deleteScriptById(String scriptId)
delete script template
|
List<co.elastic.clients.elasticsearch._types.aggregations.HistogramBucket> |
getHistogramBucketByJson(String index,
String aggregationJson,
String aggName)
get histogramBucket by json
|
co.elastic.clients.elasticsearch.core.SearchRequest |
getSearchRequest(String index,
String json,
org.springframework.data.domain.PageRequest pageRequest)
get searchRequest by json
|
private co.elastic.clients.elasticsearch.core.SearchResponse<Void> |
getSearchResponse(String index,
String json,
org.springframework.data.domain.PageRequest pageRequest)
get searchResponse
|
co.elastic.clients.elasticsearch.core.SearchResponse<T> |
getSearchResponse(String index,
String json,
org.springframework.data.domain.PageRequest pageRequest,
Class<T> clazz)
get searchResponse by json
|
List<co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket> |
getStringTermsBucketByJson(String index,
String aggregationJson,
String aggName)
get stringTermsBucket by json
|
Set<String> |
getSuggestionsByJson(String index,
String suggestionJson,
String suggestName)
get suggestion sets by json
|
boolean |
hasDocExist(String index,
String id)
Check if the doc exists
|
boolean |
hasIndexExist(String index)
check if the index exists
|
boolean |
hasScriptExist(String scriptId)
check script exist
|
T |
queryById(String index,
String id,
Class<T> clazz)
query by id
|
List<T> |
queryByJson(String index,
String json,
Class<T> clazz)
query by json
|
org.springframework.data.domain.Page<T> |
queryByJsonWithPage(String index,
String json,
Integer pageNum,
Integer pageSize,
Class<T> clazz)
query by json with page
|
org.springframework.data.domain.Page<T> |
queryByJsonWithPage(String index,
String json,
org.springframework.data.domain.PageRequest pageRequest,
Class<T> clazz)
query by json with page
|
List<T> |
queryByKeywordSimple(String index,
String field,
String keyword,
Class<T> clazz)
query by keyword simple match
|
List<T> |
queryByScriptTemplate(Function<co.elastic.clients.elasticsearch.core.SearchTemplateRequest.Builder,co.elastic.clients.util.ObjectBuilder<co.elastic.clients.elasticsearch.core.SearchTemplateRequest>> fn,
Class<T> clazz)
query by script template
|
List<T> |
queryByScriptTemplate(String index,
String scriptId,
Map<String,co.elastic.clients.json.JsonData> map,
Class<T> clazz)
query by script template
|
List<T> |
queryBySimpleTemplate(String index,
String field,
String value,
Class<T> clazz)
query by simple script template
DSL example:
GET /product/_search/template
{
"id":"es-simple-script",
"params": {
"field":"title",
"value":"大米"
}
}
|
private String |
saveOrUpdateDoc(String index,
T t)
save or upate by T
|
String |
saveOrUpdateDoc(String index,
T t,
boolean hasForce)
save or update by T
|
co.elastic.clients.elasticsearch.core.BulkResponse |
saveOrUpdateDocBulkWithJson(String index,
List<String> jsons)
save or update doc by list
|
co.elastic.clients.elasticsearch.core.BulkResponse |
saveOrUpdateDocBulkWithJsonFiles(String index,
String path)
save or update doc by bulk mode
Note: json needs to be configured with id
|
co.elastic.clients.elasticsearch.core.BulkResponse |
saveOrUpdateDocBulkWithList(String index,
List<T> list)
save or update doc by List
|
private String |
saveOrUpdateDocByJson(String index,
String json,
String id)
save or update doc
|
String |
saveOrUpdateDocByJson(String index,
String json,
String id,
Boolean hasForce)
save or update doc
|
private String |
saveOrUpdateDocByJsonFile(String index,
String file,
String id)
save or update doc by json file
|
String |
saveOrUpdateDocByJsonFile(String index,
String file,
String id,
Boolean hasForce)
save or update doc by json file
|
private co.elastic.clients.elasticsearch.core.UpdateByQueryResponse |
updateByQuery(String index,
String json)
get UpdateByQueryResponse
|
Long |
updateByQueryWithJson(String index,
String json)
update by query with json
DSL example
first: create script
PUT /_scripts/price_add
{
"script":{
"lang": "painless",
"source": "ctx.
|
@Autowired private ElasticsearchConfig.EsConfig esConfig
private static final Integer PAGE_ONE
private static final Integer PAGE_SIZE
private co.elastic.clients.elasticsearch.ElasticsearchClient client
private co.elastic.clients.transport.rest_client.RestClientTransport transport
@Autowired public ElasticsearchBaseClient(ElasticsearchConfig.EsConfig esConfig)
public ElasticsearchBaseClient(co.elastic.clients.elasticsearch.ElasticsearchClient client,
co.elastic.clients.transport.rest_client.RestClientTransport transport)
client - ElasticsearchClienttransport - RestClientTransportpublic boolean hasIndexExist(String index) throws IOException
index - indexIOExceptionpublic boolean createIndex(String index, String indexJson) throws IOException
index - indexJson - IOExceptionpublic boolean createIndexByJsonFile(String index, String jsonPath) throws IOException
index - jsonPath - IOExceptionpublic boolean deleteIndex(String index) throws IOException
index - IOExceptionpublic boolean hasDocExist(String index, String id) throws IOException
index - indexid - doc idIOExceptionpublic String deleteDoc(String index, String id) throws IOException
index - indexid - doc idIOExceptionpublic co.elastic.clients.elasticsearch.core.BulkResponse deleteBulkWithList(String index, List<String> ids) throws IOException
index - indexids - listIOExceptionpublic String saveOrUpdateDocByJson(String index, String json, String id, Boolean hasForce) throws IOException
index - indexjson - jsonid - idhasForce - has forceIOExceptionprivate String saveOrUpdateDocByJson(String index, String json, String id) throws IOException
index - indexjson - jsonid - doc idIOExceptionpublic String saveOrUpdateDocByJsonFile(String index, String file, String id, Boolean hasForce) throws IOException
index - indexfile - fileid - doc idhasForce - has forceIOExceptionprivate String saveOrUpdateDocByJsonFile(String index, String file, String id) throws IOException
index - indexfile - fileid - doc idIOExceptionpublic String saveOrUpdateDoc(String index, T t, boolean hasForce) throws IOException
index - indext - ThasForce - IOExceptionprivate String saveOrUpdateDoc(String index, T t) throws IOException
index - indext - TIOExceptionpublic co.elastic.clients.elasticsearch.core.BulkResponse saveOrUpdateDocBulkWithJsonFiles(String index, String path) throws IOException
index - indexpath - pathIOExceptionpublic co.elastic.clients.elasticsearch.core.BulkResponse saveOrUpdateDocBulkWithJson(String index, List<String> jsons) throws IOException
index - indexjsons - json listIOExceptionpublic co.elastic.clients.elasticsearch.core.BulkResponse saveOrUpdateDocBulkWithList(String index, List<T> list) throws IOException
index - indexlist - lsit TIOExceptionprivate co.elastic.clients.elasticsearch.core.SearchResponse<Void> getSearchResponse(String index, String json, org.springframework.data.domain.PageRequest pageRequest) throws IOException
index - indexjson - jsonIOExceptionpublic co.elastic.clients.elasticsearch.core.SearchResponse<T> getSearchResponse(String index, String json, org.springframework.data.domain.PageRequest pageRequest, Class<T> clazz) throws IOException
index - indexjson - jsonpageRequest - pageRequest, nullableclazz - classIOExceptionpublic co.elastic.clients.elasticsearch.core.SearchRequest getSearchRequest(String index, String json, org.springframework.data.domain.PageRequest pageRequest)
index - indexjson - jsonpageRequest - pageRequest, nullablepublic T queryById(String index, String id, Class<T> clazz) throws IOException
index - indexid - idclazz - classIOExceptionpublic List<T> queryByKeywordSimple(String index, String field, String keyword, Class<T> clazz) throws IOException
index - indexfield - fieldkeyword - keywordclazz - classIOExceptionpublic List<T> queryByJson(String index, String json, Class<T> clazz) throws IOException
index - indexjson - jsonclazz - classIOExceptionpublic org.springframework.data.domain.Page<T> queryByJsonWithPage(String index, String json, org.springframework.data.domain.PageRequest pageRequest, Class<T> clazz) throws IOException
index - indexjson - jsonpageRequest - pageRequestclazz - classIOExceptionpublic org.springframework.data.domain.Page<T> queryByJsonWithPage(String index, String json, Integer pageNum, Integer pageSize, Class<T> clazz) throws IOException
index - indexjson - jsonpageNum - pageNumpageSize - pageSizeclazz - classIOExceptionpublic List<co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket> getStringTermsBucketByJson(String index, String aggregationJson, String aggName) throws IOException
index - indexaggregationJson - jsonaggName - agg nameIOExceptionpublic Set<String> getSuggestionsByJson(String index, String suggestionJson, String suggestName) throws IOException
index - indexsuggestionJson - suggestion jsonsuggestName - suggest nameIOExceptionpublic List<co.elastic.clients.elasticsearch._types.aggregations.HistogramBucket> getHistogramBucketByJson(String index, String aggregationJson, String aggName) throws IOException
index - indexaggregationJson - aggregationJsonaggName - agg nameIOExceptionpublic void createQueryScript(String scriptId, String scriptSource, String language, boolean force) throws IOException
scriptId - script idscriptSource - script source, Content of sourcelanguage - script languageforce - has forceIOExceptionprivate void createQueryScript(String scriptId, String scriptSource, String language) throws IOException
scriptId - script idscriptSource - script templatelanguage - languageIOExceptionprivate void createSimpleScriptTemplate()
throws IOException
IOExceptionpublic boolean hasScriptExist(String scriptId) throws IOException
scriptId - scriptIdIOExceptionpublic boolean deleteScriptById(String scriptId) throws IOException
scriptId - IOExceptionpublic List<T> queryBySimpleTemplate(String index, String field, String value, Class<T> clazz) throws IOException
DSL example: GET /product/_search/template { "id":"es-simple-script", "params": { "field":"title", "value":"大米" } }
index - indexfield - fieldvalue - valueclazz - classIOExceptionpublic List<T> queryByScriptTemplate(Function<co.elastic.clients.elasticsearch.core.SearchTemplateRequest.Builder,co.elastic.clients.util.ObjectBuilder<co.elastic.clients.elasticsearch.core.SearchTemplateRequest>> fn, Class<T> clazz) throws IOException
fn - fnclazz - classIOExceptionpublic List<T> queryByScriptTemplate(String index, String scriptId, Map<String,co.elastic.clients.json.JsonData> map, Class<T> clazz) throws IOException
index - indexscriptId - scriptIdmap - mapclazz - classIOExceptionpublic Long updateByQueryWithJson(String index, String json) throws IOException
DSL example first: create script PUT /_scripts/price_add { "script":{ "lang": "painless", "source": "ctx._source.price += params.value" } } and then: POST /product/_update_by_query { "query": { "match_all": {} }, "script": { "id": "price_add", "params": { "value": 5 } } } all price will be increased by 5 java code reference test
index - indexjson - jsonIOExceptionprivate co.elastic.clients.elasticsearch.core.UpdateByQueryResponse updateByQuery(String index, String json) throws IOException
index - indexjson - jsonIOExceptionCopyright © 2022. All rights reserved.