Skip to content

Instantly share code, notes, and snippets.

@antonydenyer
Created February 18, 2014 16:20
Show Gist options
  • Save antonydenyer/9074159 to your computer and use it in GitHub Desktop.
Save antonydenyer/9074159 to your computer and use it in GitHub Desktop.
Automatic id generation and mapping _id NEST
using System;
using System.Linq;
using NUnit.Framework;
namespace Nest.AutoId
{
[ElasticType(Name = "data", IdProperty = "Id")]
public class DataForGet : DataForIndex
{
public string Id { get; set; }
}
[ElasticType(Name = "data")]
public class DataForIndex
{
public string Name { get; set; }
// some other fields...
}
[TestFixture]
public class ElasticsearchTests
{
readonly ElasticClient client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")));
[Test]
public void index_an_item()
{
var data = new DataForIndex
{
Name = "Dave"
};
client.Index(data, "index");
}
[Test]
public void get_an_item()
{
var item = client.Search<DataForGet>(x => x.Index("index"));
Console.WriteLine(string.Join(Environment.NewLine, item.Documents.Select(x => x.Id + " : " + x.Name)));
}
}
}
@dqduc
Copy link

dqduc commented Feb 20, 2014

Posible workaround:

var item = client.Search< DataForGet >(x => x.Index("index").MatchAll());
var list = item.DocumentsWithMetaData.Select(x => { x.Source.Id = x.Id; return x.Source; }).ToList();

@Mpdreamz
Copy link

@TinTin04 you are correct here.

When working with autoassigned id's you will have to use the full IHit<T> object in DocumentsWithMetaData.

@irfansharif
Copy link

this is not testable in it's entirety given .Select is an extension method (i'm constrained to using just Moq). Is there an alternative? possibly with the mappings itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment