【发布时间】:2017-06-20 20:58:56
【问题描述】:
在我的应用程序中,我将一个布尔参数传递给一个函数,该函数通过HasChildQuery 在我的弹性索引中搜索某些文档。
如果此布尔值设置为 false,我想排除具有特定字段集的文档,当布尔值设置为 true 时,我不想要第二个条件。
这是我目前的方法:
Query = new HasChildQuery
{
// ...
Query = new CommonTermsQuery
{
// This Query always needs to be there
Field = Nest.Infer.Field<FaqQuestion>(q => q.Content),
Query = content
}
&& (includeAutoLearnedData ? null : +new TermQuery
{
// I only want this Query if includeAutoLearnedData is false
Field = Nest.Infer.Field<FaqQuestion>(q => q.AutoLearned),
Value = false
})
}
我的想法是始终生成这样的请求
has_child
|
|__ ...
|
|__ common_terms
并将其扩展为
has_child
|
|__ ...
|
|__ bool
|
|__must
| |
| |__common_terms
|
|__filter
|
|__term
如果includeAutoLearnedData 为假。
但是如果是真的情况的查询似乎不起作用。
我希望&& (includeAutoLearnedData ? null : +new TermQuery 仅在布尔值为 false 时添加过滤器,而在布尔值为 true 时保持查询不变
那么在 NEST 中包含特定条件下的附加过滤器查询的正确方法是什么?
编辑:
当我从 ElasticClient 获得结果并期望它有类似的东西时,我设置了一个断点
Valid NEST response built from a successful low level call on POST: /faq/_search
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.0770000
# Request:
{
"query": {
"has_child": {
"bool": {
"must": [{
"common_terms": { ... }
}],
"filter": [{
"term": { ... }
}]
}
}
}
}
但实际结果有:
# Request:
{}
【问题讨论】:
标签: c# elasticsearch nest