我正在尝试在Nest ElasticSearch中实现以下SQL伪代码。
我没有找到与此问题相匹配的任何类似StackOverflow问题或在Nest文档中。欣赏您可以提供的任何方向。
select *
from curator..published
where accountId = 10
and (
(publishStatusId = 3 and scheduledDT > '2015-09-01')
or
(publishStatusId = 4 and publishedDT > '2015-09-01')
)
我创建了以下ElasticSearch查询,但无法将其成功转换为Nest语法。
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"
}
}
}
]
}
}
]
}
}
}
}
}
此Nest查询通过语法检查,但只有最后一个“应该”条件出现在生成的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"))
)
)
)
)
)
)
)
);
此Nest查询更好地匹配原始ElasticSearch查询但在第二个Bool上引发以下错误:错误51'Nock.FilterContainer'不包含'Bool'的定义,并且没有扩展方法'Bool'接受第一个参数可以找到类型'Nest.FilterContainer'(您是否缺少using指令或程序集引用?)
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"))
)
)
)
)
)
)
)
);
答案 0 :(得分:1)
您的第一个查询并不遥远,只是传递给第二个Should()
的表达式需要是传递给第一个Should()
的另一个表达式(Should()
需要params Func[] filters
)。这是查询(我在这里使用dynamic
作为通用类型):
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"
}
}
}
]
}
}
]
}
}
}
}
}
这与您上面的查询DSL匹配