简体   繁体   English

使用Elasticsearch.Net/NEST基于子属性搜索父文档,其中父/子文档单独存储

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

I would like to use Elasticsearch.Net/NEST to search related documents. 我想使用Elasticsearch.Net/NEST来搜索相关文档。 For example, I have: 例如,我有:

Person:

id name address_id
-- ---- ----------
1  John 1
2  Mary 2

Address:

id city
-- ------
1  Boston
2  Berlin

I'd like to store the Person and Address documents separately, and do queries where I return Person documents based on Address fields. 我想分别存储Person和Address文档,并根据Address字段返回Person文档。 For example, return all documents for people living in Boston. 例如,返回居住在波士顿的人的所有文件。 I've seen some examples in the Elaticsearch documentation using mapping and parent/child directives, but nothing for Elasticsearch.Net/NEST. 我在Elaticsearch文档中看到了一些使用映射和父/子指令的例子,但Elasticsearch.Net/NEST没有。 Any code samples or pointers would be greatly appreciated... 任何代码示例或指针将不胜感激......

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

I think the best place to look is at the NEST Unit Tests. 我认为最好看的地方是NEST单元测试。

Also on the Nest Documentation Site there is a small snippet for running a has_child query. 此外,在Nest文档站点上还有一个用于运行has_child查询的小片段。 (Pretty much the same as the unit test) (与单元测试几乎相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM