feat: There is no way the, rest es conf
This commit is contained in:
@@ -118,6 +118,11 @@ public interface ElasticsearchService {
|
||||
*/
|
||||
Map<String, Object> searchDataById(String index, String type, String id, String fields);
|
||||
|
||||
/**
|
||||
* 使用索引查询所有
|
||||
*/
|
||||
|
||||
List<Map<String,Object>> searchAll(String index);
|
||||
|
||||
/**
|
||||
* 使用分词查询
|
||||
|
||||
+55
@@ -8,6 +8,7 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
||||
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
|
||||
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
|
||||
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
@@ -24,11 +25,16 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.action.update.UpdateResponse;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.text.Text;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.aggregations.BucketOrder;
|
||||
import org.elasticsearch.search.aggregations.bucket.range.Range;
|
||||
@@ -36,6 +42,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
|
||||
import org.elasticsearch.search.aggregations.metrics.Avg;
|
||||
import org.elasticsearch.search.aggregations.metrics.Max;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -273,6 +280,54 @@ public class ElasticsearchServiceImpl implements ElasticsearchService {
|
||||
return getResponse.getSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用索引查询所有
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, Object>> searchAll(String index) {
|
||||
List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();
|
||||
|
||||
//1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称
|
||||
// Settings settings = Settings.builder().put("cluster.name", "my-application").build();
|
||||
QueryBuilder qBuilder = QueryBuilders.matchAllQuery();
|
||||
SearchResponse sResponse = client.prepareSearch(index)
|
||||
.setQuery(qBuilder).setTrackTotalHits(true)
|
||||
.get();
|
||||
SearchHits hits = sResponse.getHits();
|
||||
if(hits.getTotalHits().value > 0){
|
||||
SearchResponse scrollResp = search(index,qBuilder, 1,(int) hits.getTotalHits().value);
|
||||
for (SearchHit hit : scrollResp.getHits().getHits()) {
|
||||
sourceList.add(hit.getSourceAsMap());
|
||||
}
|
||||
}
|
||||
return sourceList;
|
||||
}
|
||||
|
||||
|
||||
public SearchResponse search(String index, QueryBuilder query,int page, int size) {
|
||||
|
||||
updateIndex(index, page,size);
|
||||
|
||||
SearchResponse searchResponse = client.prepareSearch(index)
|
||||
.setScroll(new TimeValue(360000))
|
||||
.setQuery(query).setTrackTotalHits(true).setSize(size)
|
||||
.get();
|
||||
return searchResponse;
|
||||
}
|
||||
|
||||
//更新索引的max_result_window参数
|
||||
private boolean updateIndex(String indices, int from,int size) {
|
||||
int records = from * size + size;
|
||||
if (records <= 10000) return true;
|
||||
AcknowledgedResponse indexResponse = client.admin().indices()
|
||||
.prepareUpdateSettings(indices)
|
||||
.setSettings(Settings.builder()
|
||||
.put("index.max_result_window", records)
|
||||
.build()
|
||||
).get();
|
||||
return indexResponse.isAcknowledged();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用分词查询
|
||||
|
||||
Reference in New Issue
Block a user