将重复代码写入一个方法中,减少代码冗余度
public class QueryServiceImpl implements QueryService {
public void baseQuery(String index, String type, QueryBuilder query) throws IOException {
RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(type);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] searchHits = response.getHits().getHits();
for (SearchHit searchHit : searchHits) {
System.err.println(searchHit.getSourceAsString());
}
}
@Override
public void termQuery(String index, String type, String field, String value) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.termQuery(field,value));
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
//baseQuery(index,type,QueryBuilders.termQuery(field,value));
termsQuery(index, type, field, value);
}
@Override
public void termsQuery(String index, String type, String field, String... value) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.termsQuery(field,value));
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
baseQuery(index, type, QueryBuilders.termsQuery(field, value));
}
@Override
public void queryAll(String index, String type) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
baseQuery(index, type, QueryBuilders.matchAllQuery());
}
@Override
public void matchQuery(String index, String type, String field, String value) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.matchQuery(field,value));
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
baseQuery(index, type, QueryBuilders.matchQuery(field, value));
}
@Override
public void matchQueryWithOperator(String index, String type, String field, String value, Operator operator) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.matchQuery(field,value).operator(operator));
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
baseQuery(index, type, QueryBuilders.matchQuery(field, value).operator(operator));
}
@Override
public void matchQueryMulti(String index, String type, String value, String... field) throws Exception {
// RestHighLevelClient client = ConnectionUtil.restHighLevelClient();
// SearchRequest searchRequest = new SearchRequest(index).types(type);
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(QueryBuilders.multiMatchQuery(field,value));
// searchRequest.source(searchSourceBuilder);
// SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchHit[] hits = response.getHits().getHits();
// for (SearchHit hit : hits) {
// System.err.println(hit.getSourceAsString());
// }
baseQuery(index, type, QueryBuilders.multiMatchQuery(field, value));
}
@Override
public void idsQuery(String index, String type, String... id) throws Exception {
baseQuery(index, type, QueryBuilders.idsQuery(type).addIds(id));
}