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

前端 未结 2 941
情话喂你
情话喂你 2021-01-01 06:49

I would like to use Elasticsearch.Net/NEST to search related documents. For example, I have:

Person:

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


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-01 07:01

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

    EDIT: Create the index:

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

    Index documents:

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

    And search by parent

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

提交回复
热议问题