Search in sources :

Example 1 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext 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 2 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext 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 3 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext 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 4 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext 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)

Example 5 with BuilderContext

use of org.elasticsearch.index.mapper.Mapper.BuilderContext in project elasticsearch by elastic.

the class MapperService method unmappedFieldType.

/**
     * Given a type (eg. long, string, ...), return an anonymous field mapper that can be used for search operations.
     */
public MappedFieldType unmappedFieldType(String type) {
    if (type.equals("string")) {
        deprecationLogger.deprecated("[unmapped_type:string] should be replaced with [unmapped_type:keyword]");
        type = "keyword";
    }
    MappedFieldType fieldType = unmappedFieldTypes.get(type);
    if (fieldType == null) {
        final Mapper.TypeParser.ParserContext parserContext = documentMapperParser().parserContext(type);
        Mapper.TypeParser typeParser = parserContext.typeParser(type);
        if (typeParser == null) {
            throw new IllegalArgumentException("No mapper found for type [" + type + "]");
        }
        final Mapper.Builder<?, ?> builder = typeParser.parse("__anonymous_" + type, emptyMap(), parserContext);
        final BuilderContext builderContext = new BuilderContext(indexSettings.getSettings(), new ContentPath(1));
        fieldType = ((FieldMapper) builder.build(builderContext)).fieldType();
        // There is no need to synchronize writes here. In the case of concurrent access, we could just
        // compute some mappers several times, which is not a big deal
        Map<String, MappedFieldType> newUnmappedFieldTypes = new HashMap<>();
        newUnmappedFieldTypes.putAll(unmappedFieldTypes);
        newUnmappedFieldTypes.put(type, fieldType);
        unmappedFieldTypes = unmodifiableMap(newUnmappedFieldTypes);
    }
    return fieldType;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext)

Aggregations

BuilderContext (org.elasticsearch.index.mapper.Mapper.BuilderContext)7 ContentPath (org.elasticsearch.index.mapper.ContentPath)6 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)4 Document (org.apache.lucene.document.Document)2 StringField (org.apache.lucene.document.StringField)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 Accountable (org.apache.lucene.util.Accountable)2 IndexService (org.elasticsearch.index.IndexService)2 NumberFieldMapper (org.elasticsearch.index.mapper.NumberFieldMapper)2 TextFieldMapper (org.elasticsearch.index.mapper.TextFieldMapper)2 ShardId (org.elasticsearch.index.shard.ShardId)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1