Skip to content

Prevent indexing from changing the mapping #1069

@mausch

Description

@mausch

I'm seeing some data in my index that belongs to some property in the object I'm indexing but I didn't define in my mapping. Here's a small repro where I see this unexpected behavior:

using System;
using System.Collections.Generic;
using Nest;
using Nest.DSL.Visitor;

namespace NESTMapping
{
    public class UnwantedObject
    {
        public int DoNotWant { get; set; }
    }

    public class SomeObject
    {
        public UnwantedObject Unwanted { get; set; }

        public string Name;
    }

    class MappingVisitor: IMappingVisitor
    {
        private readonly int id;

        public MappingVisitor(int id)
        {
            this.id = id;
        }

        public int Depth {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public void Visit(AttachmentMapping mapping) {
            throw new NotImplementedException();
        }

        public void Visit(GeoShapeMapping mapping)
        {
            throw new NotImplementedException();
        }

        public void Visit(GeoPointMapping mapping)
        {
            throw new NotImplementedException();
        }

        public void Visit(IPMapping mapping)
        {
            throw new NotImplementedException();
        }

        public void Visit(MultiFieldMapping mapping)
        {
            throw new NotImplementedException();
        }

        public void Visit(NestedObjectMapping mapping)
        {
            throw new NotImplementedException();
        }

        public void Visit(ObjectMapping mapping) {
            throw new Exception("Object mapping detected " + id);
        }

        public void Visit(BinaryMapping mapping){
            throw new NotImplementedException();
        }

        public void Visit(BooleanMapping mapping) {
            throw new NotImplementedException();
        }

        public void Visit(DateMapping mapping) {
            throw new NotImplementedException();
        }

        public void Visit(NumberMapping mapping){
            throw new NotImplementedException();
        }

        public void Visit(StringMapping mapping) {}

        public void Visit(RootObjectMapping mapping){}
    }

    class Program
    {
        static void Main(string[] args)
        {
            const string indexName = "someindex";
            var connectionSettings = new ConnectionSettings(uri: null, defaultIndex: indexName);
            var client = new ElasticClient(connectionSettings);
            client.DeleteIndex(x => x.AllIndices());
            client.CreateIndex(indexName);
            client.Map<SomeObject>(m => m.Type(indexName).Properties(p => p.String(d => d.Name(x => x.Name))));

            var mapping = client.GetMapping<SomeObject>(x => x.Type(indexName));
            mapping.Accept(new MappingVisitor(1));

            client.Index(new SomeObject
            {
                Name = "John Doe",
                Unwanted = new UnwantedObject { DoNotWant = 1000 },
            }, x => x.Type(indexName));

            mapping = client.GetMapping<SomeObject>(x => x.Type(indexName));
            mapping.Accept(new MappingVisitor(2));

        }
    }
}

This throws Object mapping detected 2, i.e. there is no object mapping when I define the mapping, but the mapping is then modified when I index an object.

I can see this too when requesting the mapping directly from Elasticsearch, e.g. after defining the mapping but before indexing, http://localhost:9200/someindex/_mapping shows this:

{"someindex":{"mappings":{"someindex":{"properties":{"name":{"type":"string"}}}}}}

And after indexing http://localhost:9200/someindex/_mapping is:

{"someindex":{"mappings":{"someindex":{"properties":{"name":{"type":"string"},"unwanted":{"properties":{"doNotWant":{"type":"long"}}}}}}}}

Is there any way to tell NEST/Elasticsearch to never ever modify mappings, i.e. only send to the server what has been defined in the mapping?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions