Search in sources :

Example 46 with SearchSourceBuilder

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;
}
Also used : SearchResult(org.apache.metron.indexing.dao.search.SearchResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse)

Example 47 with SearchSourceBuilder

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);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Count(io.searchbox.core.Count) CountResult(io.searchbox.core.CountResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Example 48 with SearchSourceBuilder

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());
    }
}
Also used : Hit(io.searchbox.core.SearchResult.Hit) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JsonObject(com.google.gson.JsonObject) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Example 49 with SearchSourceBuilder

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());
    }
}
Also used : Hit(io.searchbox.core.SearchResult.Hit) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JsonObject(com.google.gson.JsonObject) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Example 50 with SearchSourceBuilder

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());
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResult(io.searchbox.core.SearchResult) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Hit(io.searchbox.core.SearchResult.Hit) Search(io.searchbox.core.Search) List(java.util.List) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder) Test(org.junit.Test)

Aggregations

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)197 SearchResponse (org.elasticsearch.action.search.SearchResponse)103 SearchRequest (org.elasticsearch.action.search.SearchRequest)80 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)41 IOException (java.io.IOException)37 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)34 Search (io.searchbox.core.Search)32 Matchers.containsString (org.hamcrest.Matchers.containsString)32 Test (org.junit.Test)30 List (java.util.List)26 Map (java.util.Map)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 ArrayList (java.util.ArrayList)20 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)20 Set (java.util.Set)19 Collectors (java.util.stream.Collectors)18 SearchHit (org.elasticsearch.search.SearchHit)18 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)17