C# 需要帮助在嵌套查询中实现多个Or条件吗
我试图在Nest ElasticSearch中实现以下SQL伪代码 我没有发现任何类似的StackOverflow问题与此问题或Nest文档相匹配。感谢您提供的任何指导C# 需要帮助在嵌套查询中实现多个Or条件吗,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,我试图在Nest ElasticSearch中实现以下SQL伪代码 我没有发现任何类似的StackOverflow问题与此问题或Nest文档相匹配。感谢您提供的任何指导 select * from curator..published where accountId = 10 and ( (publishStatusId = 3 and scheduledDT > '2015-09-01') or (publishStatusId =
select *
from curator..published
where accountId = 10
and (
(publishStatusId = 3 and scheduledDT > '2015-09-01')
or
(publishStatusId = 4 and publishedDT > '2015-09-01')
)
我已经创建了以下ElasticSearch查询,但无法成功地将其转换为嵌套语法
GET curator/published/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"accountID": 1781
}
}
],
"should": [
{
"bool": {
"must": [
{
"term": {
"publishStatusID": 4
}
},
{
"range": {
"publishedDT": {
"gte": "2015-09-01T00:00:00.000"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"publishStatusID": 3
}
},
{
"range": {
"scheduleDT": {
"gte": "2015-09-01T00:00:00.000"
}
}
}
]
}
}
]
}
}
}
}
}
此嵌套查询通过语法检查,但结果ElasticSearch查询中仅显示最后一个“应该”条件
var results = this.Client.Count<Data>(c => c
.Query(q => q
.Filtered(f1 => f1
.Filter(f2 => f2
.Bool(b => b
.Must(
f => f.Term(FieldName.AccountID, "10")
)
.Should(s => s
.Bool(b1 => b1
.Must(
f => f.Term(FieldName.PublishStatusID, "3"),
f => f.Range(m => m.OnField(FieldName.ScheduleDT).GreaterOrEquals("2015-09-01"))
)
)
)
.Should(s => s
.Bool(b1 => b1
.Must(
f => f.Term(FieldName.PublishStatusID, "4"),
f => f.Range(m => m.OnField(FieldName.PublishedDT).GreaterOrEquals("2015-09-01"))
)
)
)
)
)
)
)
);
var results=this.Client.Count(c=>c
.Query(q=>q
.已筛选(f1=>f1
.Filter(f2=>f2
.Bool(b=>b
.必须(
f=>f.Term(FieldName.AccountID,“10”)
)
.Should(s=>s
.Bool(b1=>b1
.必须(
f=>f.Term(FieldName.PublishStatusID,“3”),
f=>f.Range(m=>m.OnField(FieldName.ScheduleDT).greaterrequals(“2015-09-01”))
)
)
)
.Should(s=>s
.Bool(b1=>b1
.必须(
f=>f.Term(FieldName.PublishStatusID,“4”),
f=>f.Range(m=>m.OnField(FieldName.PublishedDT).greaterrequals(“2015-09-01”))
)
)
)
)
)
)
)
);
此嵌套查询更好地匹配原始ElasticSearch查询,但在第二个Bool上引发以下错误:错误51“Nest.FilterContainer”不包含“Bool”的定义,并且找不到接受“Nest.FilterContainer”类型的第一个参数的扩展方法“Bool”(是否缺少using指令或程序集引用?)
var results=this.Client.Count(c=>c
.Query(q=>q
.已筛选(f1=>f1
.Filter(f2=>f2
.Bool(b=>b
.必须(
f=>f.Term(FieldName.AccountID,AccountID)
)
.Should(s=>s
.Bool(b1=>b1
.必须(
f=>f.Term(FieldName.PublishStatusID,“3”),
f=>f.Range(m=>m.OnField(FieldName.ScheduleDT).greaterrequals(“2015-09-01”))
)
)
.Bool(b2=>b2
.必须(
f=>f.Term(FieldName.PublishStatusID,“4”),
f=>f.Range(m=>m.OnField(FieldName.PublishedDT).greaterrequals(“2015-09-01”))
)
)
)
)
)
)
)
);
您的第一个查询并不遥远,只是传递给第二个Should()
的表达式需要是传递给第一个Should()
的另一个表达式(Should()
采用参数Func[]filters
)
这与您上面的查询DSL相匹配非常感谢Russ!这非常有意义。@ShawnMac-不用担心,很乐意帮助您
var results = this.Client.Count<Data>(c => c
.Query(q => q
.Filtered(f1 => f1
.Filter(f2 => f2
.Bool(b => b
.Must(
f => f.Term(FieldName.AccountID, AccountID)
)
.Should(s => s
.Bool(b1 => b1
.Must(
f => f.Term(FieldName.PublishStatusID, "3"),
f => f.Range(m => m.OnField(FieldName.ScheduleDT).GreaterOrEquals("2015-09-01"))
)
)
.Bool(b2 => b2
.Must(
f => f.Term(FieldName.PublishStatusID, "4"),
f => f.Range(m => m.OnField(FieldName.PublishedDT).GreaterOrEquals("2015-09-01"))
)
)
)
)
)
)
)
);
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(connection: connection);
var docs = client.Count<dynamic>(c => c
.Query(q => q
.Filtered(f1 => f1
.Filter(f2 => f2
.Bool(b => b
.Must(
f => f.Term(FieldName.AccountID, "10")
)
.Should(s => s
.Bool(b1 => b1
.Must(
f => f.Term(FieldName.PublishStatusID, "3"),
f => f.Range(m => m.OnField(FieldName.ScheduleDT).GreaterOrEquals("2015-09-01"))
)
),
s => s
.Bool(b1 => b1
.Must(
f => f.Term(FieldName.PublishStatusID, "4"),
f => f.Range(m => m.OnField(FieldName.PublishedDT).GreaterOrEquals("2015-09-01"))
)
)
)
)
)
)
)
);
Console.WriteLine(Encoding.UTF8.GetString(docs.RequestInformation.Request));
}
public static class FieldName
{
public static string AccountID = "AccountID";
public static string ScheduleDT = "ScheduleDT";
public static string PublishedDT = "PublishedDT";
public static string PublishStatusID = "PublishStatusID";
}
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"AccountID": "10"
}
}
],
"should": [
{
"bool": {
"must": [
{
"term": {
"PublishStatusID": "3"
}
},
{
"range": {
"ScheduleDT": {
"gte": "2015-09-01"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"PublishStatusID": "4"
}
},
{
"range": {
"PublishedDT": {
"gte": "2015-09-01"
}
}
}
]
}
}
]
}
}
}
}
}