Search in sources :

Example 6 with QueryShardContext

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

the class ValuesSourceConfigTests method testKeyword.

public void testKeyword() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bytes", "type=keyword");
    client().prepareIndex("index", "type", "1").setSource("bytes", "abc").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve(context, null, "bytes", null, null, null, null);
        ValuesSource.Bytes valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(new BytesRef("abc"), values.valueAt(0));
    }
}
Also used : IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 7 with QueryShardContext

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

the class CollapseBuilderTests method mockSearchContext.

private SearchContext mockSearchContext() {
    SearchContext context = mock(SearchContext.class);
    QueryShardContext shardContext = mock(QueryShardContext.class);
    when(context.getQueryShardContext()).thenReturn(shardContext);
    when(context.scrollContext()).thenReturn(null);
    when(context.rescore()).thenReturn(null);
    when(context.searchAfter()).thenReturn(null);
    return context;
}
Also used : SearchContext(org.elasticsearch.search.internal.SearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext)

Example 8 with QueryShardContext

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

the class ValuesSourceConfigTests method testBoolean.

public void testBoolean() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bool", "type=boolean");
    client().prepareIndex("index", "type", "1").setSource("bool", true).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "bool", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(1, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 9 with QueryShardContext

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

the class ValuesSourceConfigTests method testUnmappedKeyword.

public void testUnmappedKeyword() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type");
    client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Bytes> config = ValuesSourceConfig.resolve(context, ValueType.STRING, "bytes", null, null, null, null);
        ValuesSource.Bytes valuesSource = config.toValuesSource(context);
        assertNull(valuesSource);
        config = ValuesSourceConfig.resolve(context, ValueType.STRING, "bytes", null, "abc", null, null);
        valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(new BytesRef("abc"), values.valueAt(0));
    }
}
Also used : IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues)

Example 10 with QueryShardContext

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

the class QueryRescoreBuilderTests method testBuildRescoreSearchContext.

/**
     * test that build() outputs a {@link RescoreSearchContext} that has the same properties
     * than the test builder
     */
public void testBuildRescoreSearchContext() throws ElasticsearchParseException, IOException {
    final long nowInMillis = randomNonNegativeLong();
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAsciiOfLengthBetween(1, 10), indexSettings);
    // shard context will only need indicesQueriesRegistry for building Query objects nested in query rescorer
    QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name);
            return builder.build(new Mapper.BuilderContext(idxSettings.getSettings(), new ContentPath(1))).fieldType();
        }
    };
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        QueryRescorerBuilder rescoreBuilder = randomRescoreBuilder();
        QueryRescoreContext rescoreContext = rescoreBuilder.build(mockShardContext);
        int expectedWindowSize = rescoreBuilder.windowSize() == null ? QueryRescoreContext.DEFAULT_WINDOW_SIZE : rescoreBuilder.windowSize().intValue();
        assertEquals(expectedWindowSize, rescoreContext.window());
        Query expectedQuery = QueryBuilder.rewriteQuery(rescoreBuilder.getRescoreQuery(), mockShardContext).toQuery(mockShardContext);
        assertEquals(expectedQuery, rescoreContext.query());
        assertEquals(rescoreBuilder.getQueryWeight(), rescoreContext.queryWeight(), Float.MIN_VALUE);
        assertEquals(rescoreBuilder.getRescoreQueryWeight(), rescoreContext.rescoreQueryWeight(), Float.MIN_VALUE);
        assertEquals(rescoreBuilder.getScoreMode(), rescoreContext.scoreMode());
    }
}
Also used : Query(org.apache.lucene.search.Query) IndexSettings(org.elasticsearch.index.IndexSettings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) ContentPath(org.elasticsearch.index.mapper.ContentPath) QueryRescoreContext(org.elasticsearch.search.rescore.QueryRescorer.QueryRescoreContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper)

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