[英]Nest Elasticsearch search for null value
使用NEST(1.7.1),我進行了一次特定的搜索,其中一個字段應該與某個值的集合匹配,或者該字段應該為null。 看起來很瑣碎,但我無法創建此查詢,因此結果與不按此字段過濾文檔時的結果相同。
文獻:
public class Document
{
...
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string Field{ get; set; }
}
查詢以匹配給定集合中的任何值:
Filter<Document>.Query(q => q.Terms(p=> p.Field, matchingCollection));
為了也匹配那些設置為NULL的文檔,我試圖添加:
matchingCollection.Add(string.Empty);
matchingCollection.Add("NULL");
但是沒有任何成功。 有任何想法嗎 ? 謝謝 :)
對於NEST 1.x,類似於以下內容
client.Search<Document>(x => x
.Query(q => q
.Terms(f => f.Field, new [] { "term1", "term2", "term3" }) || q
.Filtered(fq => fq
.Filter(fqf => fqf
.Missing(f => f.Field)
)
)
)
);
產生以下查詢
{
"query": {
"bool": {
"should": [
{
"terms": {
"field": [
"term1",
"term2",
"term3"
]
}
},
{
"filtered": {
"filter": {
"missing": {
"field": "Field"
}
}
}
}
]
}
}
}
從NEST 2.x開始,類似
client.Search<Document>(x => x
.Query(q => q
.Terms(t => t
.Field(f => f.Field)
.Terms("term1","term2","term3")
) || !q
.Exists(e => e
.Field(f => f.Field)
)
)
);
產生以下查詢
{
"query": {
"bool": {
"should": [
{
"terms": {
"field": [
"term1",
"term2",
"term3"
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "field"
}
}
]
}
}
]
}
}
}
這是我能找到的最好的
Filter<Document>.Query(q1 =>q1.Bool(q2 => q2
.MustNot(q3 => q3
.Exists(q4 => q4
.Field(q5 => q5.Field)))));
希望有更好的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.