Reputation: 505
I'm using NEST query to filter records from elastic.
Following query will filter records based on articleIds
list. It works fine.
QueryContainer nestedQuery = null;
nestedQuery &= Query<PublishedArticle>.Terms(qs => qs
.Field(f => f.AssignedArticleList.FirstOrDefault().AssignedArticleId)
.Terms(articleIds)
);
But now I want to fetch records which doesn't contain values in prop articleIds
. How do I achieve this? Was checking elastic documents on must_not
clause. How do I build this query?
articleIds
is basically a list of integers.
Any help appreciated! :)
Upvotes: 3
Views: 1524
Reputation: 125488
You need a bool
query with a must_not
clause
var articleIds = new[] { 1, 2, 3, 4 };
client.Search<PublishedArticle>(s => s
.Query(q => q
.Bool(b => b
.MustNot(mn => mn
.Terms(t => t
.Field(f => f.AssignedArticleList.FirstOrDefault().AssignedArticleId)
.Terms(articleIds)
)
)
)
)
);
Using the !
operator, a must_not
can be shortened in NEST using operator overloading to
client.Search<PublishedArticle>(s => s
.Query(q => !q
.Terms(t => t
.Field(f => f.AssignedArticleList.FirstOrDefault().AssignedArticleId)
.Terms(articleIds)
)
)
);
Both produce
{
"query": {
"bool": {
"must_not": [
{
"terms": {
"assignedArticleList.assignedArticleId": [
1,
2,
3,
4
]
}
}
]
}
}
}
Upvotes: 4