use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project metron by apache.
the class ElasticsearchUtils method queryAllResults.
/**
* Elasticsearch queries default to 10 records returned. Some internal queries require that all
* results are returned. Rather than setting an arbitrarily high size, this method pages through results
* and returns them all in a single SearchResponse.
* @param qb A QueryBuilder that provides the query to be run.
* @return A SearchResponse containing the appropriate results.
*/
public static SearchResponse queryAllResults(RestHighLevelClient transportClient, QueryBuilder qb, String index, int pageSize) throws IOException {
org.elasticsearch.action.search.SearchRequest request = new org.elasticsearch.action.search.SearchRequest();
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(qb);
builder.size(pageSize);
builder.fetchSource(true);
builder.storedField("*");
request.source(builder);
request.indices(index);
org.elasticsearch.action.search.SearchResponse esResponse = transportClient.search(request);
List<SearchResult> allResults = getSearchResults(esResponse);
long total = esResponse.getHits().getTotalHits();
if (total > pageSize) {
int pages = (int) (total / pageSize) + 1;
for (int i = 1; i < pages; i++) {
int from = i * pageSize;
builder.from(from);
esResponse = transportClient.search(request);
allResults.addAll(getSearchResults(esResponse));
}
}
SearchResponse searchResponse = new SearchResponse();
searchResponse.setTotal(total);
searchResponse.setResults(allResults);
return searchResponse;
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method count.
/**
* Count文档
*
* @throws Exception
*/
@Test
public void count() throws Exception {
String[] name = new String[] { "T:o\"m-", "Jerry" };
String from = "2016-09-01T00:00:00";
String to = "2016-10-01T00:00:00";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("name", name)).must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));
searchSourceBuilder.query(queryBuilder);
String query = searchSourceBuilder.toString();
System.out.println(query);
Count count = new Count.Builder().addIndex(indexName).addType(typeName).query(query).build();
CountResult results = jestClient.execute(count);
Double counts = results.getCount();
System.out.println("Count:" + counts);
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method prefixQuery.
/**
* 前缀查询
*
* @throws Exception
*/
@Test
public void prefixQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 前缀查询
QueryBuilder queryBuilder = QueryBuilders.prefixQuery("name", "T:o");
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
Search search = new Search.Builder(query).addIndex(indexName).addType(typeName).build();
SearchResult result = jestClient.execute(search);
List<Hit<Object, Void>> hits = result.getHits(Object.class);
System.out.println("Size:" + hits.size());
for (Hit<Object, Void> hit : hits) {
Object news = hit.source;
System.out.println(news.toString());
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method rangeQuery.
/**
* 区间查询
*
* @throws Exception
*/
@Test
public void rangeQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("birth").gte("2016-09-01T00:00:00").lte("2016-10-01T00:00:00").includeLower(true).includeUpper(// 区间查询
true);
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
Search search = new Search.Builder(query).addIndex(indexName).addType(typeName).build();
SearchResult result = jestClient.execute(search);
List<Hit<Object, Void>> hits = result.getHits(Object.class);
System.out.println("Size:" + hits.size());
for (Hit<Object, Void> hit : hits) {
Object news = hit.source;
System.out.println(news.toString());
}
}
use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project dq-easy-cloud by dq-open-cloud.
the class TransportClient method createSearch.
/**
* 搜索高亮显示
*
* @throws Exception
*/
@Test
public void createSearch() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("view", "200"));
HighlightBuilder highlightBuilder = new HighlightBuilder();
// 高亮title
highlightBuilder.field("view");
// 高亮标签
highlightBuilder.preTags("<em>").postTags("</em>");
// 高亮内容长度
highlightBuilder.fragmentSize(500);
// searchSourceBuilder.highlight(highlightBuilder);
System.out.println(searchSourceBuilder.toString());
Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).build();
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
System.out.println("本次查询共查到:" + result.getTotal() + "篇文章!");
List<Hit<CsdnBlog, Void>> hits = result.getHits(CsdnBlog.class);
System.out.println(hits.size());
for (Hit<CsdnBlog, Void> hit : hits) {
CsdnBlog source = hit.source;
// 获取高亮后的内容
Map<String, List<String>> highlight = hit.highlight;
// 高亮后的title
List<String> views = highlight.get("view");
if (views != null) {
source.setView(views.get(0));
}
System.out.println("标题:" + source.getTitile());
System.out.println("内容:" + source.getContent());
System.out.println("浏览数:" + source.getView());
System.out.println("标签:" + source.getTag());
System.out.println("作者:" + source.getAuthor());
}
}
Aggregations