Reputation: 110
I'm having trouble getting the correct elasticsearch query string to be output from Elasticsearch.NET api. This is the query I want to be generated:
{
"size": 5000,
"sort": [
{
"id": {
"order": "desc"
}
}
],
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match_all": {}
},
{
"term": {
"stateType": {
"value": 2
}
}
}
]
}
},
"filter": {
"or": [
{
"term": {
"concealed": true
}
},
{
"term": {
"govt": true
}
}
]
}
}
}
}
The C# code I used to get that is as follows:
FilterContainer filterContainer = new FilterContainer();
filterContainer &= Filter<ElasticsearchLoad>.Or(f => f.Term("concealed", true));
filterContainer &= Filter<ElasticsearchLoad>.Or(f => f.Term("govt", true));
QueryContainer container = new MatchAllQuery();
container &= Query<ElasticsearchLoad>.Filtered(fq => Query<ElasticsearchLoad>.Term(l => l.StateType, 2));
SearchDescriptor<ElasticsearchLoad> searchDescriptor = new SearchDescriptor<ElasticsearchLoad>();
searchDescriptor.Query(container).Filter(filterContainer);
searchDescriptor.Size(5000);
var searchResponse = Client.Search<ElasticsearchMyDoc>(s => searchDescriptor);
As you can see, I'm not getting the "query" : { "filtered" : { ...}} syntax to be generated, and, for the life of me, I can't figure out how. Could someone please help me to determine the C# syntax needed to get this to work??
Thanks!
Upvotes: 0
Views: 1957
Reputation: 6357
What you're missing is how to construct a filtered query using Nest. See my solution below. In addition to that, I would suggest you use boolean
filters (like I've done below) instead of and
/or
filters as the former are usually cached and hence execute very fast. Read this.
FilterContainer filterContainer = null;
filterContainer |= Filter<ElasticsearchMyDoc>.Term("concealed", true);
filterContainer |= Filter<ElasticsearchMyDoc>.Term("govt", true);
QueryContainer queryContainer = new MatchAllQuery();
queryContainer &= Query<ElasticsearchMyDoc>.Term(l => l.StateType, 2);
SearchDescriptor<ElasticsearchMyDoc> searchDescriptor = new SearchDescriptor<ElasticsearchMyDoc>();
searchDescriptor.Query(qd => qd.Filtered(fq =>
{
fq.Filter(f => filterContainer);
fq.Query(q => queryContainer);
}));
searchDescriptor.Size(5000);
var searchResponse = Client.Search<ElasticsearchMyDoc>(s => searchDescriptor);
Upvotes: 3
Reputation: 9979
Hope you don't mind I've used fluent syntax.
var searchResponse = client.Search<Person>(s => s
.Size(5000)
.Sort(x => x.OnField(f => f.Id).Descending())
.Query(query => query
.Filtered(filtered => filtered
.Query(q => q.Bool(b => b.Must(
m => m.MatchAll(),
m => m.Term(t => t.OnField(f => f.StateType).Value("2")))))
.Filter(filter => filter.Or(
f => f.Term(t => t.Concealed, true),
f => f.Term(t => t.Govt, true))))));
Hope it helps.
Upvotes: 3