Search in sources :

Example 1 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath 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 ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class IndexFieldDataServiceTests method testGetForFieldDefaults.

public void testGetForFieldDefaults() {
    final IndexService indexService = createIndex("test");
    final IndexFieldDataService ifdService = indexService.fieldData();
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType stringMapper = new KeywordFieldMapper.Builder("string").build(ctx).fieldType();
    ifdService.clear();
    IndexFieldData<?> fd = ifdService.getForField(stringMapper);
    assertTrue(fd instanceof SortedSetDVOrdinalsIndexFieldData);
    for (MappedFieldType mapper : Arrays.asList(new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.BYTE).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.SHORT).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.INTEGER).build(ctx).fieldType(), new NumberFieldMapper.Builder("long", NumberFieldMapper.NumberType.LONG).build(ctx).fieldType())) {
        ifdService.clear();
        fd = ifdService.getForField(mapper);
        assertTrue(fd instanceof SortedNumericDVIndexFieldData);
    }
    final MappedFieldType floatMapper = new NumberFieldMapper.Builder("float", NumberFieldMapper.NumberType.FLOAT).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(floatMapper);
    assertTrue(fd instanceof SortedNumericDVIndexFieldData);
    final MappedFieldType doubleMapper = new NumberFieldMapper.Builder("double", NumberFieldMapper.NumberType.DOUBLE).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(doubleMapper);
    assertTrue(fd instanceof SortedNumericDVIndexFieldData);
}
Also used : SortedSetDVOrdinalsIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) IndexService(org.elasticsearch.index.IndexService) SortedNumericDVIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) ContentPath(org.elasticsearch.index.mapper.ContentPath)

Example 3 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class IndexFieldDataServiceTests method testFieldDataCacheListener.

public void testFieldDataCacheListener() throws Exception {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    // copy the ifdService since we can set the listener only once.
    final IndexFieldDataService ifdService = new IndexFieldDataService(indexService.getIndexSettings(), indicesService.getIndicesFieldDataCache(), indicesService.getCircuitBreakerService(), indexService.mapperService());
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType mapper1 = new TextFieldMapper.Builder("s").fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    Document doc = new Document();
    doc.add(new StringField("s", "thisisastring", Store.NO));
    writer.addDocument(doc);
    DirectoryReader open = DirectoryReader.open(writer);
    final boolean wrap = randomBoolean();
    final IndexReader reader = wrap ? ElasticsearchDirectoryReader.wrap(open, new ShardId("test", "_na_", 1)) : open;
    final AtomicInteger onCacheCalled = new AtomicInteger();
    final AtomicInteger onRemovalCalled = new AtomicInteger();
    ifdService.setListener(new IndexFieldDataCache.Listener() {

        @Override
        public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onCacheCalled.incrementAndGet();
        }

        @Override
        public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, long sizeInBytes) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onRemovalCalled.incrementAndGet();
        }
    });
    IndexFieldData<?> ifd = ifdService.getForField(mapper1);
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    AtomicFieldData load = ifd.load(leafReaderContext);
    assertEquals(1, onCacheCalled.get());
    assertEquals(0, onRemovalCalled.get());
    reader.close();
    load.close();
    writer.close();
    assertEquals(1, onCacheCalled.get());
    assertEquals(1, onRemovalCalled.get());
    ifdService.clear();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.elasticsearch.indices.IndicesService) ContentPath(org.elasticsearch.index.mapper.ContentPath) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 4 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class AbstractFieldDataTestCase method getForField.

public <IFD extends IndexFieldData<?>> IFD getForField(String type, String fieldName, boolean docValues) {
    final MappedFieldType fieldType;
    final BuilderContext context = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    if (type.equals("string")) {
        if (docValues) {
            fieldType = new KeywordFieldMapper.Builder(fieldName).build(context).fieldType();
        } else {
            fieldType = new TextFieldMapper.Builder(fieldName).fielddata(true).build(context).fieldType();
        }
    } else if (type.equals("float")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.FLOAT).docValues(docValues).build(context).fieldType();
    } else if (type.equals("double")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.DOUBLE).docValues(docValues).build(context).fieldType();
    } else if (type.equals("long")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.LONG).docValues(docValues).build(context).fieldType();
    } else if (type.equals("int")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.INTEGER).docValues(docValues).build(context).fieldType();
    } else if (type.equals("short")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.SHORT).docValues(docValues).build(context).fieldType();
    } else if (type.equals("byte")) {
        fieldType = new NumberFieldMapper.Builder(fieldName, NumberFieldMapper.NumberType.BYTE).docValues(docValues).build(context).fieldType();
    } else if (type.equals("geo_point")) {
        fieldType = new GeoPointFieldMapper.Builder(fieldName).docValues(docValues).build(context).fieldType();
    } else if (type.equals("_parent")) {
        fieldType = new ParentFieldMapper.Builder("_type").type(fieldName).build(context).fieldType();
    } else if (type.equals("binary")) {
        fieldType = new BinaryFieldMapper.Builder(fieldName).docValues(docValues).build(context).fieldType();
    } else {
        throw new UnsupportedOperationException(type);
    }
    return ifdService.getForField(fieldType);
}
Also used : NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) GeoPointFieldMapper(org.elasticsearch.index.mapper.GeoPointFieldMapper) ContentPath(org.elasticsearch.index.mapper.ContentPath) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) BinaryFieldMapper(org.elasticsearch.index.mapper.BinaryFieldMapper)

Example 5 with ContentPath

use of org.elasticsearch.index.mapper.ContentPath in project elasticsearch by elastic.

the class InternalEngineTests method dynamicUpdate.

private Mapping dynamicUpdate() {
    BuilderContext context = new BuilderContext(Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(), new ContentPath());
    final RootObjectMapper root = new RootObjectMapper.Builder("some_type").build(context);
    return new Mapping(Version.CURRENT, root, new MetadataFieldMapper[0], emptyMap());
}
Also used : RootObjectMapper(org.elasticsearch.index.mapper.RootObjectMapper) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) Mapping(org.elasticsearch.index.mapper.Mapping) ContentPath(org.elasticsearch.index.mapper.ContentPath)

Aggregations

ContentPath (org.elasticsearch.index.mapper.ContentPath)9 BuilderContext (org.elasticsearch.index.mapper.Mapper.BuilderContext)6 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 IndexSettings (org.elasticsearch.index.IndexSettings)3 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)3 Document (org.apache.lucene.document.Document)2 StringField (org.apache.lucene.document.StringField)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Query (org.apache.lucene.search.Query)2 Accountable (org.apache.lucene.util.Accountable)2 Settings (org.elasticsearch.common.settings.Settings)2 Index (org.elasticsearch.index.Index)2 IndexService (org.elasticsearch.index.IndexService)2 NumberFieldMapper (org.elasticsearch.index.mapper.NumberFieldMapper)2 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)2 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)2