Search in sources :

Example 1 with QueryRescoreContext

use of org.elasticsearch.search.rescore.QueryRescorer.QueryRescoreContext 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)

Example 2 with QueryRescoreContext

use of org.elasticsearch.search.rescore.QueryRescorer.QueryRescoreContext in project elasticsearch by elastic.

the class QueryRescorerBuilder method build.

@Override
public QueryRescoreContext build(QueryShardContext context) throws IOException {
    org.elasticsearch.search.rescore.QueryRescorer rescorer = new org.elasticsearch.search.rescore.QueryRescorer();
    QueryRescoreContext queryRescoreContext = new QueryRescoreContext(rescorer);
    queryRescoreContext.setQuery(QueryBuilder.rewriteQuery(this.queryBuilder, context).toQuery(context));
    queryRescoreContext.setQueryWeight(this.queryWeight);
    queryRescoreContext.setRescoreQueryWeight(this.rescoreQueryWeight);
    queryRescoreContext.setScoreMode(this.scoreMode);
    if (this.windowSize != null) {
        queryRescoreContext.setWindowSize(this.windowSize);
    }
    return queryRescoreContext;
}
Also used : QueryRescoreContext(org.elasticsearch.search.rescore.QueryRescorer.QueryRescoreContext)

Aggregations

QueryRescoreContext (org.elasticsearch.search.rescore.QueryRescorer.QueryRescoreContext)2 Query (org.apache.lucene.search.Query)1 Settings (org.elasticsearch.common.settings.Settings)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 IndexSettings (org.elasticsearch.index.IndexSettings)1 ContentPath (org.elasticsearch.index.mapper.ContentPath)1 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)1 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1