因此,假設我使用 NEST 在 C# 中有以下代碼:
private ISearchResponse<Country> GetFilteredResultsByIds(ElasticClient client,
Query query)
{
return client.Search<Country>(s => s
.From(0)
.Size(10)
.Query(q => q
.Nested(n => n
.Path(p => p.Customer)
.Query(q => q
.Term(t => t
.Field(f => f.Customer.CustomerId).Value(query.CustomerId)))
) && q
.Nested(n => n
.Path(p => p.Person)
.Query(q => q
.Term(t => t
.Field(f => f.Person.ServiceId).Value(query.ServiceId))))));
}
是否可以將部分搜索查詢外包給外部類/引數?例如,客戶和人員部分能夠在另一個查詢中重用它。
uj5u.com熱心網友回復:
您可以將這些部分提取到靜態類并使用如下
client.Search<Country>(s => s
.From(0)
.Size(10)
.Query(q => q.SearchCustomerId(customerId) && q.SearchServiceId(serviceId)));
提取部分的類
public static class Helpers
{
public static QueryContainer SearchServiceId(this QueryContainerDescriptor<Country> container, string serviceId)
{
return container
.Nested(n => n
.Path(p => p.Person)
.Query(q => q
.Term(t => t
.Field(f => f.Person.First().ServiceId).Value(serviceId))));
}
public static QueryContainer SearchCustomerId(this QueryContainerDescriptor<Country> container, string customerId)
{
return container
.Nested(n => n
.Path(p => p.Customer)
.Query(q => q
.Term(t => t
.Field(f => f.Customer.First().CustomerId).Value(customerId))));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444279.html
