c# - Need help implementing multiple Or conditions in Nest query -
i attempting implement following sql pseudo-code in nest elasticsearch.
i haven't found similar stackoverflow questions matching question or in nest documentation. appreciate direction can provide.
select * curator..published accountid = 10 , ( (publishstatusid = 3 , scheduleddt > '2015-09-01') or (publishstatusid = 4 , publisheddt > '2015-09-01') )
i've created following elasticsearch query unable translate nest syntax.
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" } } } ] } } ] } } } } }
this nest query passes syntax check last "should" condition appears in resulting elasticsearch query.
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")) ) ) ) ) ) ) ) );
this nest query better matches original elasticsearch query raises following error on 2nd bool: error 51 'nest.filtercontainer' not contain definition 'bool' , no extension method 'bool' accepting first argument of type 'nest.filtercontainer' found (are missing using directive or assembly reference?)
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")) ) ) ) ) ) ) ) );
your first query not far off, expression passed second should()
needs expression passed first should()
(should()
takes params func[] filters
). here's query (i've used dynamic
here generic type):
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"; }
produces
{ "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" } } } ] } } ] } } } } }
this matches query dsl above
Comments
Post a Comment