我想像这样做一个ElasticSearch查询:
{
"query" :
{
"bool" :
{
"filter" : [
{
"terms" :
{
"name" : ["name1", "name2"]
}
},
{
"terms" :
{
"color" : ["orange", "red"]
}
}
]
}
}
}
我试图在NEST中实现它,如下所示:
_elasticClient
.SearchAsync<MyDocument>(s =>
s.Index("myindex")
.Query(q => q
.Bool(bq => bq
.Filter(fq =>
{
QueryContainer query = null;
if (nameList.Any()) {
query &= fq.Terms(t => t.Field(f => f.Name).Terms(nameList));
}
if (colorList.Any()) {
query &= fq.Terms(t => t.Field(f => f.Color).Terms(colorList));
}
return query;
})
)
)
);
但是这给了我一个这样的查询,其中过滤器包含在 bool必须中:
{
"query" :
{
"bool" :
{
"filter" : [
{
"bool" :
{
"must" : [
{
"terms" :
{
"name" : ["name1", "name2"]
}
},
{
"terms" :
{
"color" : ["orange", "red"]
}
}
]
}
}
]
}
}
}
我应该如何更改NEST代码以便为我提供正确的查询?是否必须将我的术语添加到 QueryContainer 之外的其他内容中?
答案 0 :(得分:18)
如果要检查条件过滤器,可以在进行查询之前创建过滤器列表,如下所示:
var nameList = new[] {"a", "b"};
var colorList = new[] {1, 2};
var filters = new List<Func<QueryContainerDescriptor<MyDocument>, QueryContainer>>();
if (nameList.Any())
{
filters.Add(fq=> fq.Terms(t => t.Field(f => f.Name).Terms(nameList)));
}
if (colorList.Any())
{
filters.Add(fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList)));
}
ISearchResponse<Property> searchResponse =
elasticClient.Search<MyDocument>(x => x.Query(q => q
.Bool(bq => bq.Filter(filters))));
如果您在进行过滤查询之前不需要检查任何条件,那么您可以拥有类似的内容:
ISearchResponse<MyDocument> searchResponse =
elasticClient.Search<MyDocument>(x => x.Query(q => q
.Bool(bq => bq
.Filter(
fq => fq.Terms(t => t.Field(f => f.Name).Terms(nameList)),
fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList))
))));
答案 1 :(得分:11)
bool查询的Filter
方法需要params Func<QueryContainerDescriptor<T>, QueryContainer>[]
,以便您可以将多个表达式传递给多个过滤器
var nameList = new string[] { "name1", "name2" };
var colorList = new string[] { "orange", "red" };
client.SearchAsync<MyDocument>(s => s
.Index("myindex")
.Query(q => q
.Bool(bq => bq
.Filter(
fq => fq.Terms(t => t.Field(f => f.Name).Terms(nameList)),
fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList))
)
)
)
);
导致
{
"query": {
"bool": {
"filter": [
{
"terms": {
"name": [
"name1",
"name2"
]
}
},
{
"terms": {
"color": [
"orange",
"red"
]
}
}
]
}
}
}
NEST also has the concept of conditionless queries,也就是说,如果确定某个查询是无条件的,那么它将不序列化为请求的一部分。
无条件意味着什么?那么,这取决于查询;例如,在terms
查询的情况下,如果满足以下任何条件,则视为无条件
field
没有值null
null
或空字符串演示
var emptyNames = new string[] {};
string[] nullColors = null;
client.SearchAsync<MyDocument>(s =>
s.Index("myindex")
.Query(q => q
.Bool(bq => bq
.Filter(
fq => fq.Terms(t => t.Field(f => f.Name).Terms(emptyNames)),
fq => fq.Terms(t => t.Field(f => f.Color).Terms(nullColors)))
)
)
);
结果
{}
无条件查询可以使编写NEST查询更容易,因为在构造查询之前不需要检查集合是否具有值。您可以基于每个查询更改无条件语义using .Strict()
and .Verbatim()
。
答案 2 :(得分:0)
var searchResponse = client.Search<EventData>(s => s
.From(0)
.Query(q => q
.Bool(bq => bq
.Filter(
fq => fq.Terms(t => t.Field(f => f.Client.Id).Terms(17)),
fq => fq.Terms(t => t.Field(f => f.Item.Id).Terms(**new[] { 34983, 35430, 35339, 35300 }**)),
fq => fq.Terms(t=>t.Field(f=>f.Event).Terms("Control de Panico")),
fq => fq.DateRange(dr => dr.Field(f => f.DateTime)
.GreaterThanOrEquals(new DateTime(2018, 07, 01))
.LessThanOrEquals(new DateTime(2018, 10, 02)))
)
))
.Size(2000)
.Sort(g => sortDescriptor)
);