Search in sources :

Example 81 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder 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 82 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder 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)

Example 83 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class ComponentIndex method search.

@VisibleForTesting
List<ComponentsPerQualifier> search(ComponentIndexQuery query, ComponentTextSearchFeature... features) {
    Collection<String> qualifiers = query.getQualifiers();
    if (qualifiers.isEmpty()) {
        return Collections.emptyList();
    }
    SearchRequestBuilder request = client.prepareSearch(INDEX_TYPE_COMPONENT).setQuery(createQuery(query, features)).addAggregation(createAggregation(query)).setSize(0);
    SearchResponse response = request.get();
    return aggregationsToQualifiers(response);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 84 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class IssueIndex method listTermsMatching.

private Terms listTermsMatching(String fieldName, IssueQuery query, @Nullable String textQuery, Terms.Order termsOrder, int maxNumberOfTags) {
    SearchRequestBuilder requestBuilder = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSize(0);
    requestBuilder.setQuery(boolQuery().must(QueryBuilders.matchAllQuery()).filter(createBoolFilter(query)));
    TermsBuilder aggreg = AggregationBuilders.terms("_ref").field(fieldName).size(maxNumberOfTags).order(termsOrder).minDocCount(1L);
    if (textQuery != null) {
        aggreg.include(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery)));
    }
    SearchResponse searchResponse = requestBuilder.addAggregation(aggreg).get();
    return searchResponse.getAggregations().get("_ref");
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 85 with SearchRequestBuilder

use of org.elasticsearch.action.search.SearchRequestBuilder in project sonarqube by SonarSource.

the class IssueIndex method selectIssuesForBatch.

/**
   * Return non closed issues for a given project, module, or file. Other kind of components are not allowed.
   * Only fields needed for the batch are returned.
   */
public Iterator<IssueDoc> selectIssuesForBatch(ComponentDto component) {
    BoolQueryBuilder filter = boolQuery().must(createAuthorizationFilter(true)).mustNot(termsQuery(IssueIndexDefinition.FIELD_ISSUE_STATUS, Issue.STATUS_CLOSED));
    switch(component.scope()) {
        case Scopes.PROJECT:
            filter.must(termsQuery(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH, component.uuid()));
            break;
        case Scopes.FILE:
            filter.must(termsQuery(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID, component.uuid()));
            break;
        default:
            throw new IllegalStateException(format("Component of scope '%s' is not allowed", component.scope()));
    }
    SearchRequestBuilder requestBuilder = getClient().prepareSearch(IssueIndexDefinition.INDEX_TYPE_ISSUE).setSearchType(SearchType.SCAN).setScroll(TimeValue.timeValueMinutes(EsUtils.SCROLL_TIME_IN_MINUTES)).setSize(10_000).setFetchSource(new String[] { IssueIndexDefinition.FIELD_ISSUE_KEY, IssueIndexDefinition.FIELD_ISSUE_RULE_KEY, IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID, IssueIndexDefinition.FIELD_ISSUE_FILE_PATH, IssueIndexDefinition.FIELD_ISSUE_SEVERITY, IssueIndexDefinition.FIELD_ISSUE_MANUAL_SEVERITY, IssueIndexDefinition.FIELD_ISSUE_RESOLUTION, IssueIndexDefinition.FIELD_ISSUE_STATUS, IssueIndexDefinition.FIELD_ISSUE_ASSIGNEE, IssueIndexDefinition.FIELD_ISSUE_LINE, IssueIndexDefinition.FIELD_ISSUE_MESSAGE, IssueIndexDefinition.FIELD_ISSUE_CHECKSUM, IssueIndexDefinition.FIELD_ISSUE_FUNC_CREATED_AT }, null).setQuery(boolQuery().must(matchAllQuery()).filter(filter));
    SearchResponse response = requestBuilder.get();
    return EsUtils.scroll(getClient(), response.getScrollId(), DOC_CONVERTER);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)110 SearchResponse (org.elasticsearch.action.search.SearchResponse)66 SearchHit (org.elasticsearch.search.SearchHit)23 SearchHits (org.elasticsearch.search.SearchHits)18 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)17 ArrayList (java.util.ArrayList)14 Client (org.elasticsearch.client.Client)13 HashMap (java.util.HashMap)12 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)12 Test (org.junit.Test)12 SearchRequest (org.elasticsearch.action.search.SearchRequest)11 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)9 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)9 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)9 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)8 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)8 Map (java.util.Map)7 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)7 MultiSearchResponse (org.elasticsearch.action.search.MultiSearchResponse)7