Search in sources :

Example 31 with QueryShardContext

use of org.elasticsearch.index.query.QueryShardContext in project elasticsearch by elastic.

the class AbstractSortTestCase method testBuildSortField.

/**
     * test that build() outputs a {@link SortField} that is similar to the one
     * we would get when parsing the xContent the sort builder is rendering out
     */
public void testBuildSortField() throws IOException {
    QueryShardContext mockShardContext = createMockShardContext();
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        T sortBuilder = createTestItem();
        SortFieldAndFormat sortField = sortBuilder.build(mockShardContext);
        sortFieldAssertions(sortBuilder, sortField.field, sortField.format);
    }
}
Also used : QueryShardContext(org.elasticsearch.index.query.QueryShardContext)

Example 32 with QueryShardContext

use of org.elasticsearch.index.query.QueryShardContext in project elasticsearch by elastic.

the class GeoDistanceSortBuilderTests method testCommonCaseIsOptimized.

public void testCommonCaseIsOptimized() throws IOException {
    // make sure the below tests test something...
    assertFalse(SortField.class.equals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass()));
    QueryShardContext context = createMockShardContext();
    // The common case should use LatLonDocValuesField.newDistanceSort
    GeoDistanceSortBuilder builder = new GeoDistanceSortBuilder("", new GeoPoint(3.5, 2.1));
    SortFieldAndFormat sort = builder.build(context);
    assertEquals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass(), sort.field.getClass());
    // however this might be disabled by fancy options
    builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1), new GeoPoint(3.0, 4));
    sort = builder.build(context);
    // 2 points -> plain SortField with a custom comparator
    assertEquals(SortField.class, sort.field.getClass());
    builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
    builder.unit(DistanceUnit.KILOMETERS);
    sort = builder.build(context);
    // km rather than m -> plain SortField with a custom comparator
    assertEquals(SortField.class, sort.field.getClass());
    builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
    builder.order(SortOrder.DESC);
    sort = builder.build(context);
    // descending means the max value should be considered rather than min
    assertEquals(SortField.class, sort.field.getClass());
    builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
    builder.setNestedPath("some_nested_path");
    sort = builder.build(context);
    // can't use LatLon optimized sorting with nested fields
    assertEquals(SortField.class, sort.field.getClass());
    builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
    builder.order(SortOrder.DESC);
    sort = builder.build(context);
    // can't use LatLon optimized sorting with DESC sorting
    assertEquals(SortField.class, sort.field.getClass());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField)

Example 33 with QueryShardContext

use of org.elasticsearch.index.query.QueryShardContext in project elasticsearch by elastic.

the class ShardSearchTransportRequestTests method testSerialize50Request.

// BWC test for changes from #20916
public void testSerialize50Request() throws IOException {
    BytesArray requestBytes = new BytesArray(Base64.getDecoder().decode("AAh4cXptdEhJcgdnT0d1ZldWyfL/sgQBJAHkDAMBAAIBAQ4TWlljWlZ5TkVmRU5xQnFQVHBjVBRZbUpod2pRV2dDSXVxRXpRaEdGVBRFZWFJY0plT2hn" + "UEpISFhmSXR6Qw5XZ1hQcmFidWhWalFSQghuUWNwZ2JjQxBtZldRREJPaGF3UnlQSE56EVhQSUtRa25Iekh3bU5kbGVECWlFT2NIeEh3RgZIYXpMTWgUeGJq" + "VU9Tdkdua3RORU5QZkNrb1EOalRyWGh5WXhvZ3plV2UUcWlXZFl2eUFUSXdPVGdMUUtYTHAJU3RKR3JxQkVJEkdEQ01xUHpnWWNaT3N3U3prSRIUeURlVFpM" + "Q1lBZERZcWpDb3NOVWIST1NyQlZtdUNrd0F1UXRvdVRjEGp6RlVMd1dqc3VtUVNaTk0JT3N2cnpLQ3ZLBmRpS1J6cgdYbmVhZnBxBUlTUU9pEEJMcm1ERXVs" + "eXhESlBoVkgTaWdUUmtVZGh4d0FFc2ZKRm9ZahNrb01XTnFFd2NWSVVDU3pWS2xBC3JVTWV3V2tUUWJUE3VGQU1Hd21CYUFMTmNQZkxobXUIZ3dxWHBxWXcF" + "bmNDZUEOTFBSTEpYZVF6Z3d2eE0PV1BucUFacll6WWRxa1hCDGxkbXNMaVRzcUZXbAtSY0NsY3FNdlJQcv8BAP////8PAQAAARQAAQp5THlIcHdQeGtMAAAB" + "AQAAAAEDbkVLAQMBCgACAAADAQABAAAAAQhIc25wRGxQbwEBQgABAAACAQMAAAEIAAAJMF9OSG9kSmh2HwABAwljRW5MVWxFbVQFemlxWG8KcXZQTkRUUGJk" + "bgECCkpMbXVMT1dtVnkISEdUUHhsd0cBAAEJAAABA2lkcz+rKsUAAAAAAAAAAAECAQYAAgwxX0ZlRWxSQkhzQ07/////DwABAAEDCnRyYXFHR1hjVHkKTERY" + "aE1HRWVySghuSWtzbEtXUwABCgEHSlRwQnhwdwAAAQECAgAAAAAAAQcyX3FlYmNDGQEEBklxZU9iUQdTc01Gek5YCWlMd2xuamNRQwNiVncAAUHt61kAAQR0" + "ZXJtP4AAAAANbUtDSnpHU3lidm5KUBUMaVpqeG9vcm5QSFlvAAEBLGdtcWxuRWpWTXdvTlhMSHh0RWlFdHBnbEF1cUNmVmhoUVlwRFZxVllnWWV1A2ZvbwEA" + "AQhwYWlubGVzc/8AALk4AAAAAAABAAAAAAAAAwpKU09PU0ZmWnhFClVqTGxMa2p3V2gKdUJwZ3R3dXFER5Hg97uT7MOmPgEADw"));
    try (StreamInput in = new NamedWriteableAwareStreamInput(requestBytes.streamInput(), namedWriteableRegistry)) {
        in.setVersion(Version.V_5_0_0);
        ShardSearchTransportRequest readRequest = new ShardSearchTransportRequest();
        readRequest.readFrom(in);
        assertEquals(0, in.available());
        IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, () -> readRequest.filteringAliases());
        assertEquals("alias filter for aliases: [JSOOSFfZxE, UjLlLkjwWh, uBpgtwuqDG] must be rewritten first", illegalStateException.getMessage());
        IndexMetaData.Builder indexMetadata = new IndexMetaData.Builder(baseMetaData).putAlias(AliasMetaData.newAliasMetaDataBuilder("JSOOSFfZxE").filter("{\"term\" : {\"foo\" : \"bar\"}}")).putAlias(AliasMetaData.newAliasMetaDataBuilder("UjLlLkjwWh").filter("{\"term\" : {\"foo\" : \"bar1\"}}")).putAlias(AliasMetaData.newAliasMetaDataBuilder("uBpgtwuqDG").filter("{\"term\" : {\"foo\" : \"bar2\"}}"));
        IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY);
        final long nowInMillis = randomNonNegativeLong();
        QueryShardContext context = new QueryShardContext(0, indexSettings, null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
        readRequest.rewrite(context);
        QueryBuilder queryBuilder = readRequest.filteringAliases();
        assertEquals(queryBuilder, QueryBuilders.boolQuery().should(QueryBuilders.termQuery("foo", "bar")).should(QueryBuilders.termQuery("foo", "bar1")).should(QueryBuilders.termQuery("foo", "bar2")));
        BytesStreamOutput output = new BytesStreamOutput();
        output.setVersion(Version.V_5_0_0);
        readRequest.writeTo(output);
        assertEquals(output.bytes().toBytesRef(), requestBytes.toBytesRef());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexSettings(org.elasticsearch.index.IndexSettings) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RandomQueryBuilder(org.elasticsearch.index.query.RandomQueryBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 34 with QueryShardContext

use of org.elasticsearch.index.query.QueryShardContext in project elasticsearch by elastic.

the class ExtendedBoundsTests method testParseAndValidate.

public void testParseAndValidate() {
    long now = randomLong();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
    SearchContext context = mock(SearchContext.class);
    QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> now);
    when(context.getQueryShardContext()).thenReturn(qsc);
    FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
    DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC);
    ExtendedBounds expected = randomParsedExtendedBounds();
    ExtendedBounds parsed = unparsed(expected).parseAndValidate("test", context, format);
    // parsed won't *equal* expected because equal includes the String parts
    assertEquals(expected.getMin(), parsed.getMin());
    assertEquals(expected.getMax(), parsed.getMax());
    parsed = new ExtendedBounds("now", null).parseAndValidate("test", context, format);
    assertEquals(now, (long) parsed.getMin());
    assertNull(parsed.getMax());
    parsed = new ExtendedBounds(null, "now").parseAndValidate("test", context, format);
    assertNull(parsed.getMin());
    assertEquals(now, (long) parsed.getMax());
    SearchParseException e = expectThrows(SearchParseException.class, () -> new ExtendedBounds(100L, 90L).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
    e = expectThrows(SearchParseException.class, () -> unparsed(new ExtendedBounds(100L, 90L)).parseAndValidate("test", context, format));
    assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
Also used : SearchParseException(org.elasticsearch.search.SearchParseException) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DocValueFormat(org.elasticsearch.search.DocValueFormat) IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 35 with QueryShardContext

use of org.elasticsearch.index.query.QueryShardContext in project elasticsearch by elastic.

the class PercolatorFieldMapper method parse.

@Override
public Mapper parse(ParseContext context) throws IOException {
    QueryShardContext queryShardContext = this.queryShardContext.get();
    if (context.doc().getField(queryBuilderField.name()) != null) {
        // for the current document.
        throw new IllegalArgumentException("a document can only contain one percolator query");
    }
    XContentParser parser = context.parser();
    QueryBuilder queryBuilder = parseQueryBuilder(queryShardContext.newParseContext(parser), parser.getTokenLocation());
    verifyQuery(queryBuilder);
    // Fetching of terms, shapes and indexed scripts happen during this rewrite:
    queryBuilder = queryBuilder.rewrite(queryShardContext);
    try (XContentBuilder builder = XContentFactory.contentBuilder(QUERY_BUILDER_CONTENT_TYPE)) {
        queryBuilder.toXContent(builder, new MapParams(Collections.emptyMap()));
        builder.flush();
        byte[] queryBuilderAsBytes = BytesReference.toBytes(builder.bytes());
        context.doc().add(new Field(queryBuilderField.name(), queryBuilderAsBytes, queryBuilderField.fieldType()));
    }
    Query query = toQuery(queryShardContext, mapUnmappedFieldAsString, queryBuilder);
    processQuery(query, context);
    return null;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

QueryShardContext (org.elasticsearch.index.query.QueryShardContext)49 IndexService (org.elasticsearch.index.IndexService)17 Query (org.apache.lucene.search.Query)13 Settings (org.elasticsearch.common.settings.Settings)12 IndexSettings (org.elasticsearch.index.IndexSettings)11 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)9 Searcher (org.elasticsearch.index.engine.Engine.Searcher)9 TermQuery (org.apache.lucene.search.TermQuery)8 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 BytesRef (org.apache.lucene.util.BytesRef)7 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)6 BoostQuery (org.apache.lucene.search.BoostQuery)6 Version (org.elasticsearch.Version)6 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Term (org.apache.lucene.index.Term)5 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)5