0

我已将 2 个单独的表索引到 Elasticsearch - Meetings 和 MeetingAttendees 中。一对多关系 - 一个会议可以有许多与会者。

会议
编号:1

编号:2

会议与会者
MeetingAttendeeID:1
MeetingID:1
名称:“tom”

会议参加者 ID:2 会议 ID
:1
名称:“大卫”

会议参加者 ID:3 会议 ID
: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();

但是,映射不起作用,我没有看到提琴手发出任何请求,而且我不确定此查询是否会返回与 David 作为与会者的会议。

4

1 回答 1

0

我建议您使用嵌套对象Meeting来处理和之间的关系Attendee。这意味着我们会将所有数据存储在一个文档(会议)中。

会议和与会者类:

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"))))));

弹性搜索的结果:

{
   "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"
                  }
               ]
            }
         }
      ]
   }
}

更新:

在您要索引更多相关数据的情况下,查看父子关系可能是值得的

示例类:

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();

现在,我们必须通过他们的孩子找到父母。使用 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"))))));
于 2015-05-30T16:32:41.457 回答