Using Elasticsearch.Net/NEST to search parent documents based on child attributes, where parent/child documents are stored separately

故事扮演 提交于 2019-11-30 10:25:06

Here's a small snippet where the address is the parent

EDIT: Create the index:

var indicesOperationResponse = _client.CreateIndex(ci => ci.Index("test")
            .AddMapping<Address>(m => m.MapFromAttributes())
            .AddMapping<Person>(m => m.MapFromAttributes().SetParent<Address>()));

Index documents:

var bulkResponse = _client.Bulk(b => b
            .Index<Address>(bd => bd.Object(new Address { Name = "Tel Aviv", Id = 1 }).Index("test"))
            .Index<Person>(bd => bd.Index("test").Object(new Person {Id = 5, Address = 1, Name = "Me"}).Parent(1)));

And search by parent

var searchResponse = _client.Search<Person>(s => s
        .Query(q=>q.MatchAll())
        .Filter(q => q
            .HasParent<Address>(c => c
                .Query(cq => cq.Match(m=>m.OnField(t => t.Name).Query("Tel Aviv"))))));
Paige Cook

I think the best place to look is at the NEST Unit Tests.

Also on the Nest Documentation Site there is a small snippet for running a has_child query. (Pretty much the same as the unit test)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!