Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch and Nest: Why amd I missing the id field on a query?

I created a simple object that represents a Meeting that has elements such as time, location, name, topic, etc and indexed it in ElasticSearch via Nest. It has an Id field that I leave blank so that ES can generate them.

Later on I retrieved all the documents that are missing GEO coordinates so that I may update them. All my returned elements still have null for the id field and when I update them back to ES it creates new documents for them.

What am I missing here that makes all my id's null?

Thank you

Here is the Meeting class (the id prop is redundant but I tried it anyway)

[ElasticType(IdProperty = "Id")]
    public class Meeting
    {
        public string Id { get; set; }
        public string Code { get; set; }
        public string Day { get; set; }
        public string Town { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public string OriginalTime { get; set; }
        public string OriginalTimeCleaned { get; set; }
        public string Handicap { get; set; }
        public string FormattedAddress { get; set; }
        public Coordinates Coordinates { get; set; }
        public List<MeetingTime> Times = new List<MeetingTime>();
        public bool IsProcessed { get; set; }    
    }

Here is how I retrieve meetings

 public static List<Meeting> GetAddressesWithMissingCoordinates()
        {

            var result = Client.Search<Meeting>(s => s
                .Index("meetings")
                .AllTypes()
                .Query(p => p.Filtered(f => f.Filter(x => x.Missing(c => c.Coordinates)))));


            return result.Documents.ToList();
        }

Here is my update statement, Id is null

 public static void UpdateMeetingCoordinates(Meeting meeting, Coordinates coordinates)
        {
            meeting.Coordinates = coordinates;

            var response = Client.Index(meeting, u => u
                .Index("meetings")
                .Type("meeting")
                //.Id(meeting.Id.ToString())
                .Refresh()
                );

            Console.WriteLine(response);
        }

I've tried partial updates as well with no luck.

like image 600
Matt Avatar asked Nov 20 '15 19:11

Matt


1 Answers

There is a way to get the internal id, as described in this issue requesting this very feature.

Rather than using response.Documents, do this instead:

var results = response.Hits.Select(hit =>
    {
        var result = hit.Source;
        result.Id = hit.Id;
        return result;
    });
like image 62
Holf Avatar answered Oct 15 '22 15:10

Holf