Search in sources :

Example 36 with QueryShardContext

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

the class ExternalFieldMapperTests method testExternalValuesWithMultifield.

public void testExternalValuesWithMultifield() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    IndexService indexService = createIndex("test", settings);
    Map<String, Mapper.TypeParser> mapperParsers = new HashMap<>();
    mapperParsers.put(ExternalMapperPlugin.EXTERNAL, new ExternalMapper.TypeParser(ExternalMapperPlugin.EXTERNAL, "foo"));
    mapperParsers.put(TextFieldMapper.CONTENT_TYPE, new TextFieldMapper.TypeParser());
    mapperParsers.put(KeywordFieldMapper.CONTENT_TYPE, new KeywordFieldMapper.TypeParser());
    MapperRegistry mapperRegistry = new MapperRegistry(mapperParsers, Collections.emptyMap());
    Supplier<QueryShardContext> queryShardContext = () -> {
        return indexService.newQueryShardContext(0, null, () -> {
            throw new UnsupportedOperationException();
        });
    };
    DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext);
    DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", ExternalMapperPlugin.EXTERNAL).startObject("fields").startObject("field").field("type", "text").field("store", true).startObject("fields").startObject("raw").field("type", "keyword").field("store", true).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().string()));
    ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "1234").endObject().bytes());
    assertThat(doc.rootDoc().getField("field.bool"), notNullValue());
    assertThat(doc.rootDoc().getField("field.bool").stringValue(), is("T"));
    assertThat(doc.rootDoc().getField("field.point"), notNullValue());
    GeoPoint point = new GeoPoint().resetFromIndexableField(doc.rootDoc().getField("field.point"));
    assertThat(point.lat(), closeTo(42.0, 1E-5));
    assertThat(point.lon(), closeTo(51.0, 1E-5));
    IndexableField shape = doc.rootDoc().getField("field.shape");
    assertThat(shape, notNullValue());
    IndexableField field = doc.rootDoc().getField("field.field");
    assertThat(field, notNullValue());
    assertThat(field.stringValue(), is("foo"));
    IndexableField raw = doc.rootDoc().getField("field.field.raw");
    assertThat(raw, notNullValue());
    assertThat(raw.binaryValue(), is(new BytesRef("foo")));
}
Also used : IndexService(org.elasticsearch.index.IndexService) HashMap(java.util.HashMap) IndexableField(org.apache.lucene.index.IndexableField) GeoPoint(org.elasticsearch.common.geo.GeoPoint) Version(org.elasticsearch.Version) MapperRegistry(org.elasticsearch.indices.mapper.MapperRegistry) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings) BytesRef(org.apache.lucene.util.BytesRef)

Example 37 with QueryShardContext

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

the class ExternalFieldMapperTests method testExternalValues.

public void testExternalValues() throws Exception {
    Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    IndexService indexService = createIndex("test", settings);
    MapperRegistry mapperRegistry = new MapperRegistry(Collections.singletonMap(ExternalMapperPlugin.EXTERNAL, new ExternalMapper.TypeParser(ExternalMapperPlugin.EXTERNAL, "foo")), Collections.singletonMap(ExternalMetadataMapper.CONTENT_TYPE, new ExternalMetadataMapper.TypeParser()));
    Supplier<QueryShardContext> queryShardContext = () -> {
        return indexService.newQueryShardContext(0, null, () -> {
            throw new UnsupportedOperationException();
        });
    };
    DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), indexService.getIndexAnalyzers(), indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext);
    DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("type").startObject(ExternalMetadataMapper.CONTENT_TYPE).endObject().startObject("properties").startObject("field").field("type", "external").endObject().endObject().endObject().endObject().string()));
    ParsedDocument doc = documentMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", "1234").endObject().bytes());
    assertThat(doc.rootDoc().getField("field.bool"), notNullValue());
    assertThat(doc.rootDoc().getField("field.bool").stringValue(), is("T"));
    assertThat(doc.rootDoc().getField("field.point"), notNullValue());
    GeoPoint point = new GeoPoint().resetFromIndexableField(doc.rootDoc().getField("field.point"));
    assertThat(point.lat(), closeTo(42.0, 1e-5));
    assertThat(point.lon(), closeTo(51.0, 1e-5));
    assertThat(doc.rootDoc().getField("field.shape"), notNullValue());
    assertThat(doc.rootDoc().getField("field.field"), notNullValue());
    assertThat(doc.rootDoc().getField("field.field").stringValue(), is("foo"));
    assertThat(doc.rootDoc().getField(ExternalMetadataMapper.FIELD_NAME).stringValue(), is(ExternalMetadataMapper.FIELD_VALUE));
}
Also used : IndexService(org.elasticsearch.index.IndexService) GeoPoint(org.elasticsearch.common.geo.GeoPoint) Version(org.elasticsearch.Version) MapperRegistry(org.elasticsearch.indices.mapper.MapperRegistry) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Settings(org.elasticsearch.common.settings.Settings)

Example 38 with QueryShardContext

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

the class MultiMatchQueryTests method testBlendTermsUnsupportedValue.

public void testBlendTermsUnsupportedValue() {
    FakeFieldType ft1 = new FakeFieldType();
    ft1.setName("foo");
    FakeFieldType ft2 = new FakeFieldType() {

        @Override
        public Query termQuery(Object value, QueryShardContext context) {
            throw new IllegalArgumentException();
        }
    };
    ft2.setName("bar");
    Term[] terms = new Term[] { new Term("foo", "baz") };
    float[] boosts = new float[] { 2 };
    Query expected = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false);
    Query actual = MultiMatchQuery.blendTerm(indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    }), new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3));
    assertEquals(expected, actual);
}
Also used : FakeFieldType(org.elasticsearch.index.mapper.MockFieldMapper.FakeFieldType) Query(org.apache.lucene.search.Query) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilders.multiMatchQuery(org.elasticsearch.index.query.QueryBuilders.multiMatchQuery) FieldAndFieldType(org.elasticsearch.index.search.MultiMatchQuery.FieldAndFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef)

Example 39 with QueryShardContext

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

the class MultiMatchQueryTests method testBlendNoTermQuery.

public void testBlendNoTermQuery() {
    FakeFieldType ft1 = new FakeFieldType();
    ft1.setName("foo");
    FakeFieldType ft2 = new FakeFieldType() {

        @Override
        public Query termQuery(Object value, QueryShardContext context) {
            return new MatchAllDocsQuery();
        }
    };
    ft2.setName("bar");
    Term[] terms = new Term[] { new Term("foo", "baz") };
    float[] boosts = new float[] { 2 };
    Query expectedClause1 = BlendedTermQuery.booleanBlendedQuery(terms, boosts, false);
    Query expectedClause2 = new BoostQuery(new MatchAllDocsQuery(), 3);
    Query expected = new BooleanQuery.Builder().setDisableCoord(true).add(expectedClause1, Occur.SHOULD).add(expectedClause2, Occur.SHOULD).build();
    Query actual = MultiMatchQuery.blendTerm(indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    }), new BytesRef("baz"), null, 1f, new FieldAndFieldType(ft1, 2), new FieldAndFieldType(ft2, 3));
    assertEquals(expected, actual);
}
Also used : Query(org.apache.lucene.search.Query) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilders.multiMatchQuery(org.elasticsearch.index.query.QueryBuilders.multiMatchQuery) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FakeFieldType(org.elasticsearch.index.mapper.MockFieldMapper.FakeFieldType) FieldAndFieldType(org.elasticsearch.index.search.MultiMatchQuery.FieldAndFieldType) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) BytesRef(org.apache.lucene.util.BytesRef)

Example 40 with QueryShardContext

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

the class ScriptedMetricAggregatorTests method queryShardContextMock.

/**
     * We cannot use Mockito for mocking QueryShardContext in this case because
     * script-related methods (e.g. QueryShardContext#getLazyExecutableScript)
     * is final and cannot be mocked
     */
@Override
protected QueryShardContext queryShardContextMock(final MappedFieldType[] fieldTypes, IndexSettings idxSettings, CircuitBreakerService circuitBreakerService) {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").build();
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, SCRIPTS);
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService;
    try {
        scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    } catch (IOException e) {
        throw new ElasticsearchException(e);
    }
    return new QueryShardContext(0, idxSettings, null, null, null, null, scriptService, xContentRegistry(), null, null, System::currentTimeMillis);
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Environment(org.elasticsearch.env.Environment) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ScriptSettings(org.elasticsearch.script.ScriptSettings) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry)

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