如何在 NEST 查询 Elasticsearch 中使用条件
How to use Conditionals in NEST Queries Elasticsearch
我想根据用户输入的 If Else 语句为 Elasticsearch 构建一个 NEST 查询。目前它只接受 must 部分中的一个条件,而另一个未添加到查询中。
以下代码编译到此 http 请求:
{"from":0,"query":{"bool":{"must":[{"nested":{"path":"customer","query":{"term":{"customer.customerId":{"value":1}}}}}]}},"size":10}
如您所见,SearchPersonId 未添加到 must 条件中。
搜索方式:
private ISearchResponse<Country> GetFilteredResults(ElasticClient client, Query query)
{
var searchRequest = new SearchDescriptor<Country>();
searchRequest.From(0).Size(10).Query(q =>
{
q.Bool(b => b
.Must(mc =>
{
if (query.CustomerId != 0) mc.SearchCustomerId(query.CustomerId);
if (query.PersonId != 0) mc.SearchPersonId(query.PersonId);
return mc;
})
);
return q;
});
return client.Search<Country>(searchRequest);
}
查询方式:
public static class Helpers
{
public static QueryContainer SearchPersonId(this QueryContainerDescriptor<Country> container, string personId)
{
return container
.Nested(n => n
.Path(p => p.Person)
.Query(q => q
.Term(t => t
.Field(f => f.Person.PersonId).Value(personId))));
}
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.CustomerId).Value(customerId))));
}
}
Must
方法的重载之一接受 QueryContainer
的数组,这可以帮助您实现条件逻辑
ISearchResponse<Country> GetFilteredResults(ElasticClient client, Query query)
{
var queryContainers = new List<QueryContainer>();
var descriptor = new QueryContainerDescriptor<Country>();
if (query.CustomerId != 0) queryContainers.Add(descriptor.SearchCustomerId(query.CustomerId));
if (query.PersonId != 0) queryContainers.Add(descriptor.SearchPersonId(query.PersonId));
var searchRequest = new SearchDescriptor<Country>();
searchRequest.From(0).Size(10).Query(q => q.Bool(b => b.Must(queryContainers.ToArray())));
return client.Search<Country>(searchRequest);
}
我想根据用户输入的 If Else 语句为 Elasticsearch 构建一个 NEST 查询。目前它只接受 must 部分中的一个条件,而另一个未添加到查询中。
以下代码编译到此 http 请求:
{"from":0,"query":{"bool":{"must":[{"nested":{"path":"customer","query":{"term":{"customer.customerId":{"value":1}}}}}]}},"size":10}
如您所见,SearchPersonId 未添加到 must 条件中。
搜索方式:
private ISearchResponse<Country> GetFilteredResults(ElasticClient client, Query query)
{
var searchRequest = new SearchDescriptor<Country>();
searchRequest.From(0).Size(10).Query(q =>
{
q.Bool(b => b
.Must(mc =>
{
if (query.CustomerId != 0) mc.SearchCustomerId(query.CustomerId);
if (query.PersonId != 0) mc.SearchPersonId(query.PersonId);
return mc;
})
);
return q;
});
return client.Search<Country>(searchRequest);
}
查询方式:
public static class Helpers
{
public static QueryContainer SearchPersonId(this QueryContainerDescriptor<Country> container, string personId)
{
return container
.Nested(n => n
.Path(p => p.Person)
.Query(q => q
.Term(t => t
.Field(f => f.Person.PersonId).Value(personId))));
}
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.CustomerId).Value(customerId))));
}
}
Must
方法的重载之一接受 QueryContainer
的数组,这可以帮助您实现条件逻辑
ISearchResponse<Country> GetFilteredResults(ElasticClient client, Query query)
{
var queryContainers = new List<QueryContainer>();
var descriptor = new QueryContainerDescriptor<Country>();
if (query.CustomerId != 0) queryContainers.Add(descriptor.SearchCustomerId(query.CustomerId));
if (query.PersonId != 0) queryContainers.Add(descriptor.SearchPersonId(query.PersonId));
var searchRequest = new SearchDescriptor<Country>();
searchRequest.From(0).Size(10).Query(q => q.Bool(b => b.Must(queryContainers.ToArray())));
return client.Search<Country>(searchRequest);
}