Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiGet ignores _source field #849

Closed
gmarz opened this issue Aug 4, 2014 · 4 comments
Closed

MultiGet ignores _source field #849

gmarz opened this issue Aug 4, 2014 · 4 comments

Comments

@gmarz
Copy link
Contributor

gmarz commented Aug 4, 2014

client.MultiGet(m => m
    .Get<ElasticsearchProject>(g => g.Id(1))
    .SourceEnabled("id", "name")
);

Should serialize to:

{
  "docs": [
    {
      "_index": "elasticsearchprojects",
      "_type": "elasticsearchproject",
      "_id": "1",
      "_source": ["id", "name"]
    }
  ]
}

But instead is:

{
  "docs": [
    {
      "_index": "elasticsearchprojects",
      "_type": "elasticsearchproject",
      "_id": "1"
    }
  ]
}

_source is completely ignored even when calling .SourceEnabled() without parameters and is a bit misleading as a name especially since the default in ES is true. We should consider renaming it to just Source to match the API (NEST 2.0).

Edit: this was originally reported in this SO issue: http://stackoverflow.com/questions/25125247/elasticsearch-nest-multiget-with-source-filtering#25125247

@gmarz gmarz added Bug labels Aug 4, 2014
@lakario
Copy link

lakario commented Aug 4, 2014

I have been digging through the NEST sources and it looks like Get<T> also fails to respect the source fields. The following code returns the complete source:

client.Get<Thing>(x => x.Id(guids[0].ToString()).SourceEnabled("id", "name"))

GET test/thing/16b006aa-416f-42e4-801c-7f61f6d74294?source_enabled=id%2Cname

Likewise, it doesn't look like specifying Fields() on MultiGet() works either (but it does for Get<T>()). The following returns the complete source:

client.MultiGet(s => s
    .GetMany<Thing>(guids.Select(x => x.ToString()))
    .Fields<Thing>(x => x.Name));

GET test/thing/_mget?fields=name 
{
  "docs": [
    {
      "_index": "test",
      "_type": "thing",
      "_id": "16b006aa-416f-42e4-801c-7f61f6d74294"
    },
    {
      "_index": "test",
      "_type": "thing",
      "_id": "eb2e4f76-a697-4690-b339-cf923eda2c92"
    },
    {
      "_index": "test",
      "_type": "thing",
      "_id": "494d59fa-965d-4c84-96e3-ac617a7536ca"
    }
  ]
}

@Mpdreamz
Copy link
Member

Mpdreamz commented Aug 5, 2014

This should be fixed as of: #852

SourceEnabled(string[]) is now deprecated in favor of EnableSource(bool)

If your want to include parts of your source use SourceInclude() and SourceExclude()

@gmarz gmarz removed the 1.0.2 label Aug 5, 2014
@gmarz gmarz closed this as completed Aug 5, 2014
@lakario
Copy link

lakario commented Aug 5, 2014

I just pulled the latest off develop and SourceInclude<T>() is still returning a full source for the above example. Is that expected?

@lakario
Copy link

lakario commented Aug 5, 2014

Here's the test app I am running, includes all logic to build an index, populate it, and query with MultiGet:

class Program
{
    static void Main(string[] args)
    {
        const string serverUrl = "http://localhost:9200";
        const string indexName = "test";
        const bool resetAll = false;

        var guids = new[] {
            new Guid("16b006aa-416f-42e4-801c-7f61f6d74294"),
            new Guid("eb2e4f76-a697-4690-b339-cf923eda2c92"),
            new Guid("494d59fa-965d-4c84-96e3-ac617a7536ca"),
        };

        var client = new ElasticClient(new ConnectionSettings(new Uri(serverUrl))
            .SetTimeout(15000)
            .SetDefaultIndex(indexName));

        if (resetAll || !client.IndexExists(x => x.Index(indexName)).Exists)
        {
            ResetIndex(client, indexName, guids);
        }

        var response = client.MultiGet(s => s
            .GetMany<Thing>(guids.Select(x => x.ToString()))
            .SourceInclude<Thing>(x => x.Id, x => x.Name));

        DisplayThings(response.Documents.Select(x => x.Source).Cast<Thing>());

        Console.ReadKey();
    }

    static void DisplayThings(IEnumerable<Thing> things)
    {
        foreach (var item in things)
        {
            Console.WriteLine("{1}{0}{1}{1}{2}", item, Environment.NewLine, new String('*', 50));
        }
    } 

    static void ResetIndex(IElasticClient client, string indexName, IList<Guid> guids)
    {
        Action fnCreate = () =>
        {
            client.CreateIndex(x => x.Index(indexName));
            client.Map<Thing>(x => x.MapFromAttributes());
        };

        if (client.IndexExists(x => x.Index(indexName)).Exists)
        {
            client.DeleteIndex(x => x.Index(indexName));
            fnCreate();
        }
        else
        {
            fnCreate();
        }

        var items = new[] {
            new Thing() {
                Id = guids[0],
                Name = "Thing 1",
                Description = "This is a description for thing 1"
            },
            new Thing() {
                Id = guids[1],
                Name = "Thing 2",
                Description = "This is a description for thing 2"
            },
            new Thing() {
                Id = guids[2],
                Name = "Thing 3",
                Description = "This is a description for thing 3"
            },
        };

        client.IndexMany(items);
    }

    [ElasticType(IdProperty = "Id", Name = "thing")]
    public class Thing
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public override string ToString()
        {
            return String.Format("Id: {0}{3}" +
                                 "Name: {1}{3}" +
                                 "Description: {2}",
                                 Name, Id, Description ?? "<null>", Environment.NewLine);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants