Search in sources :

Example 11 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class RemoteRequestBuildersTests method testInitialSearchEntity.

public void testInitialSearchEntity() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.source(new SearchSourceBuilder());
    String query = "{\"match_all\":{}}";
    HttpEntity entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":true}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Source filtering is included if set up
    searchRequest.source().fetchSource(new String[] { "in1", "in2" }, new String[] { "out" });
    entity = initialSearchEntity(searchRequest, new BytesArray(query));
    assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
    assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
    // Invalid XContent fails
    RuntimeException e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{}, \"trailing\": {}")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))"));
    e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{")));
    assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input"));
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BytesArray(org.elasticsearch.common.bytes.BytesArray) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 12 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class RemoteRequestBuildersTests method testIntialSearchPath.

public void testIntialSearchPath() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());
    assertEquals("/_search", initialSearchPath(searchRequest));
    searchRequest.indices("a");
    searchRequest.types("b");
    assertEquals("/a/b/_search", initialSearchPath(searchRequest));
    searchRequest.indices("a", "b");
    searchRequest.types("c", "d");
    assertEquals("/a,b/c,d/_search", initialSearchPath(searchRequest));
    searchRequest.indices("cat,");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("cat,", "dog");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("dog", "cat,");
    expectBadStartRequest(searchRequest, "Index", ",", "cat,");
    searchRequest.indices("cat/");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("cat/", "dog");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("dog", "cat/");
    expectBadStartRequest(searchRequest, "Index", "/", "cat/");
    searchRequest.indices("ok");
    searchRequest.types("cat,");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("cat,", "dog");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("dog", "cat,");
    expectBadStartRequest(searchRequest, "Type", ",", "cat,");
    searchRequest.types("cat/");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
    searchRequest.types("cat/", "dog");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
    searchRequest.types("dog", "cat/");
    expectBadStartRequest(searchRequest, "Type", "/", "cat/");
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 13 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class SearchSlowLogTests method testSlowLogSearchContextPrinterToLog.

public void testSlowLogSearchContextPrinterToLog() throws IOException {
    IndexService index = createIndex("foo");
    SearchContext searchContext = createSearchContext(index);
    SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
    searchContext.request().source(source);
    SearchSlowLog.SlowLogSearchContextPrinter p = new SearchSlowLog.SlowLogSearchContextPrinter(searchContext, 10);
    assertThat(p.toString(), startsWith("[foo][0]"));
    // Makes sure that output doesn't contain any new lines
    assertThat(p.toString(), not(containsString("\n")));
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 14 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class TransportSearchAction method resolveIndexBoosts.

private Map<String, Float> resolveIndexBoosts(SearchRequest searchRequest, ClusterState clusterState) {
    if (searchRequest.source() == null) {
        return Collections.emptyMap();
    }
    SearchSourceBuilder source = searchRequest.source();
    if (source.indexBoosts() == null) {
        return Collections.emptyMap();
    }
    Map<String, Float> concreteIndexBoosts = new HashMap<>();
    for (SearchSourceBuilder.IndexBoost ib : source.indexBoosts()) {
        Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(clusterState, searchRequest.indicesOptions(), ib.getIndex());
        for (Index concreteIndex : concreteIndices) {
            concreteIndexBoosts.putIfAbsent(concreteIndex.getUUID(), ib.getBoost());
        }
    }
    return Collections.unmodifiableMap(concreteIndexBoosts);
}
Also used : HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 15 with SearchSourceBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.

the class ExpandSearchPhase method buildExpandSearchSourceBuilder.

private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options) {
    SearchSourceBuilder groupSource = new SearchSourceBuilder();
    groupSource.from(options.getFrom());
    groupSource.size(options.getSize());
    if (options.getSorts() != null) {
        options.getSorts().forEach(groupSource::sort);
    }
    if (options.getFetchSourceContext() != null) {
        if (options.getFetchSourceContext().includes() == null && options.getFetchSourceContext().excludes() == null) {
            groupSource.fetchSource(options.getFetchSourceContext().fetchSource());
        } else {
            groupSource.fetchSource(options.getFetchSourceContext().includes(), options.getFetchSourceContext().excludes());
        }
    }
    if (options.getDocValueFields() != null) {
        options.getDocValueFields().forEach(groupSource::docValueField);
    }
    if (options.getStoredFieldsContext() != null && options.getStoredFieldsContext().fieldNames() != null) {
        options.getStoredFieldsContext().fieldNames().forEach(groupSource::storedField);
    }
    if (options.getScriptFields() != null) {
        for (SearchSourceBuilder.ScriptField field : options.getScriptFields()) {
            groupSource.scriptField(field.fieldName(), field.script());
        }
    }
    if (options.getHighlightBuilder() != null) {
        groupSource.highlighter(options.getHighlightBuilder());
    }
    groupSource.explain(options.isExplain());
    groupSource.trackScores(options.isTrackScores());
    return groupSource;
}
Also used : SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

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