elasticsearch嵌套索引关系数据库

时间:2015-05-29 18:22:02

标签: elasticsearch nest elasticsearch-net

我已将2个单独的表编入Elasticsearch - Meetings和MeetingAttendees。 一对多关系 - 会议可以有很多与会者。

会议
ID:1

ID:2

与会与会者
MeetingAttendeeID:1
MeetingID:1
姓名:"汤姆"

MeetingAttendeeID:2
MeetingID:1
姓名:"大卫"

MeetingAttendeeID:3
MeetingID:2
姓名:"大卫"

我试图创建这样的关系,但我没有看到ES的任何差异

client.CreateIndex(ci => ci.Index("testmappingindex")
                .AddMapping<Meeting>(m => m.MapFromAttributes())
                .AddMapping<MeetingAttendee>(m => m.MapFromAttributes().SetParent<Meeting>()));

我希望能够像这样查询:

result = client.Search<Meeting>(s => s
                .Type("Meeting")
                .From(0)
                .Size(10)
                .Query(q => q.MeetingAttendees(ma => ma.Terms(t => t.Name == "david")))
                    )
            ).Documents.ToList();

然而,映射工作并不起作用,我也没有看到任何请求在fiddler中发出,我不确定这个查询是否能够恢复与David的会议与会者。

1 个答案:

答案 0 :(得分:0)

我建议您Nested Object处理MeetingAttendee之间的关系。这意味着我们将把所有数据存储在一个文档中(会议)。

会议和参加者课程:

public class Meeting
{
    public int Id { get; set; }
    public string Name { get; set; }
    [ElasticProperty(Type = FieldType.Nested)]
    public List<Attendee> MeetingAttendees { get; set; }
}

public class Attendee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

创建索引:

var indicesOperationResponse = client.CreateIndex(descriptor => descriptor
    .Index(indexName)
    .AddMapping<Meeting>(m => m.MapFromAttributes()));

索引一些数据:

var david = new Attendee {Id = 1, Name = "David"};
var carl = new Attendee {Id = 2, Name = "Carl"};
var jason = new Attendee {Id = 3, Name = "Jason"};

client.Index(new Meeting {Id = 1, Name = "Meeting1", MeetingAttendees = new List<Attendee>{david, carl}});
client.Index(new Meeting {Id = 2, Name = "Meeting2", MeetingAttendees = new List<Attendee>{jason}});
client.Index(new Meeting {Id = 3, Name = "Meeting3", MeetingAttendees = new List<Attendee>{jason, david}});

client.Refresh();

我们应该稍微修改一下您的查询:

var result = client.Search<Meeting>(s => s
    .From(0)
    .Size(10)
    .Query(q => q.Nested(n => n
        .Path(p => p.MeetingAttendees.First())
        .Query(qq => qq
            .Term(meeting => meeting.OnField(f => f.MeetingAttendees.First().Name).Value("david"))))));

elasticsearch的结果:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1.4054651,
      "hits": [
         {
            "_index": "my_index",
            "_type": "meeting",
            "_id": "1",
            "_score": 1.4054651,
            "_source": {
               "id": 1,
               "name": "Meeting1",
               "meetingAttendees": [
                  {
                     "id": 1,
                     "name": "David"
                  },
                  {
                     "id": 2,
                     "name": "Carl"
                  }
               ]
            }
         },
         {
            "_index": "my_index",
            "_type": "meeting",
            "_id": "3",
            "_score": 1.4054651,
            "_source": {
               "id": 3,
               "name": "Meeting3",
               "meetingAttendees": [
                  {
                     "id": 3,
                     "name": "Jason"
                  },
                  {
                     "id": 1,
                     "name": "David"
                  }
               ]
            }
         }
      ]
   }
}

<强>更新

在您要为更多相关数据编制索引的情况下,可以了解parent-child关系

示例类:

public class Meeting
{
    public int Id { get; set; }
    public string Name { get; set; } 
}

public class Attendee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

映射:

var indicesOperationResponse = client.CreateIndex(descriptor => descriptor
    .Index(indexName)
    .AddMapping<Meeting>(m => m.MapFromAttributes())
    .AddMapping<Attendee>(m => m.MapFromAttributes().SetParent<Meeting>()));

示例数据:

var david = new Attendee { Id = 1, Name = "David"};
var carl = new Attendee { Id = 2, Name = "Carl"};
var jason = new Attendee {Id = 3, Name = "Jason"};

client.Index(new Meeting {Id = 1, Name = "Meeting1"});
client.Index(new Meeting {Id = 2, Name = "Meeting2"});
client.Index(new Meeting {Id = 3, Name = "Meeting3"});

client.Index(david, descriptor => descriptor.Parent("1"));
client.Index(carl, descriptor => descriptor.Parent("1"));
client.Index(jason, descriptor => descriptor.Parent("2"));

client.Refresh();

现在,我们必须find parent by their children。使用NEST,您可以通过此查询执行此操作:

var searchResponse = client.Search<Meeting>(s => s
    .Query(q => q
        .HasChild<Attendee>(c => c
            .Query(query => query.Term(t => t.OnField(f => f.Name).Value("david"))))));