Search in sources :

Example 61 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project graylog2-server by Graylog2.

the class Searches method terms.

public TermsResult terms(String field, int size, String query, String filter, TimeRange range, Sorting.Direction sorting) {
    Terms.Order termsOrder;
    if (size == 0) {
        size = 50;
    }
    if (sorting == Sorting.Direction.DESC) {
        termsOrder = Terms.Order.count(false);
    } else {
        termsOrder = Terms.Order.count(true);
    }
    SearchRequestBuilder srb;
    if (filter == null) {
        srb = standardSearchRequest(query, determineAffectedIndices(range, null), range);
    } else {
        srb = filteredSearchRequest(query, filter, determineAffectedIndices(range, filter), range);
    }
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.terms(AGG_TERMS).field(field).size(size).order(termsOrder)).subAggregation(AggregationBuilders.missing("missing").field(field)).filter(standardAggregationFilters(range, filter));
    srb.addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new TermsResult(f.getAggregations().get(AGG_TERMS), f.getAggregations().get("missing"), f.getDocCount(), query, request.source(), r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TermsResult(org.graylog2.indexer.results.TermsResult) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 62 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project graylog2-server by Graylog2.

the class Searches method search.

public SearchResult search(SearchesConfig config) {
    Set<IndexRange> indices = determineAffectedIndicesWithRanges(config.range(), config.filter());
    Set<String> indexNames = Sets.newHashSet();
    for (IndexRange index : indices) {
        indexNames.add(index.indexName());
    }
    SearchRequest request = searchRequest(config, indexNames).request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, config.range());
    return new SearchResult(r.getHits(), indices, config.query(), request.source(), r.getTook());
}
Also used : IndexRange(org.graylog2.indexer.ranges.IndexRange) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchResult(org.graylog2.indexer.results.SearchResult) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 63 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project graylog2-server by Graylog2.

the class Searches method histogram.

public HistogramResult histogram(String query, DateHistogramInterval interval, String filter, TimeRange range) {
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.dateHistogram(AGG_HISTOGRAM).field("timestamp").interval(interval.toESInterval())).filter(standardAggregationFilters(range, filter));
    QueryStringQueryBuilder qs = queryStringQuery(query);
    qs.allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
    final Set<String> affectedIndices = determineAffectedIndices(range, filter);
    final SearchRequestBuilder srb = c.prepareSearch(affectedIndices.toArray(new String[affectedIndices.size()])).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(qs).addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new DateHistogramResult(f.getAggregations().get(AGG_HISTOGRAM), query, request.source(), interval, r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) DateHistogramResult(org.graylog2.indexer.results.DateHistogramResult) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 64 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiSearch.

@Test
public void testMultiSearch() throws Exception {
    //first, INDEX two values
    Map<String, Object> headers = new HashMap<String, Object>();
    node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
    node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
    //now, verify MULTISEARCH
    headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.MULTISEARCH);
    SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx"));
    SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy"));
    SearchRequestBuilder srb3 = node.client().prepareSearch("instagram").setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue"));
    List<SearchRequest> list = new ArrayList<>();
    list.add(srb1.request());
    list.add(srb2.request());
    list.add(srb3.request());
    MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class);
    MultiSearchResponse.Item[] responses = response.getResponses();
    assertNotNull("response should not be null", response);
    assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length);
    assertFalse("response 1 should be ok", responses[0].isFailure());
    assertFalse("response 2 should be ok", responses[1].isFailure());
    assertTrue("response 3 should be failed", responses[2].isFailure());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) Item(org.elasticsearch.action.get.MultiGetRequest.Item) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 65 with SearchRequest

use of org.elasticsearch.action.search.SearchRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiSearch.

@Test
public void testMultiSearch() throws Exception {
    //first, INDEX two values
    Map<String, Object> headers = new HashMap<String, Object>();
    node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
    node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
    //now, verify MULTISEARCH
    headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTISEARCH);
    SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx"));
    SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy"));
    SearchRequestBuilder srb3 = node.client().prepareSearch("instagram").setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue"));
    List<SearchRequest> list = new ArrayList<>();
    list.add(srb1.request());
    list.add(srb2.request());
    list.add(srb3.request());
    MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class);
    MultiSearchResponse.Item[] responses = response.getResponses();
    assertNotNull("response should not be null", response);
    assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length);
    assertFalse("response 1 should be ok", responses[0].isFailure());
    assertFalse("response 2 should be ok", responses[1].isFailure());
    assertTrue("response 3 should be failed", responses[2].isFailure());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) Item(org.elasticsearch.action.get.MultiGetRequest.Item) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

SearchRequest (org.elasticsearch.action.search.SearchRequest)69 SearchResponse (org.elasticsearch.action.search.SearchResponse)21 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)8 Test (org.junit.Test)7 MultiSearchRequest (org.elasticsearch.action.search.MultiSearchRequest)6 BytesArray (org.elasticsearch.common.bytes.BytesArray)6 TimeValue (org.elasticsearch.common.unit.TimeValue)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)6 ArrayList (java.util.ArrayList)5 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)5 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 List (java.util.List)4