NEST/C#: Binary operation on *QueryDescriptor<T>

I am creating dynamic ES query with NEST like this:

var q1 = new MatchQuery() { Field = "similarto.keyword", Query = "55445" };
var q2 = new MatchQuery() { Field = "metatag.keyword", Query = "hot" };
var q3 = q1 || q2;

var response = client.Search<Recommendation>(s => s
	.AllTypes().From(0).Size(10)
	.Query(q => q3));

This works fine.
But doing the same with the MatchQueryDescriptor<T> it isn't working:

var q1x = new MatchQueryDescriptor<Recommendation>();
q1x.Field(f => f.Similarto.Suffix("keyword"));
q1x.Query("55445");

var q2x = new MatchQueryDescriptor<Recommendation>();
q2x.Field(f => f.Metatag.Suffix("keyword"));
q2x.Query("hot");

var q3x = q1x || q2x;

On the last line, the compiler complains about: Operator '||' cannot be applied to operands of type 'MatchQueryDescriptor<Recommendation>' and 'MatchQueryDescriptor<Recommendation>'

I can live with the first version. But the second is nicer because of the strongly typed arguments.
Is it somehow possible to use the binary operators ||, &&, etc. on *QueryDescriptor<T>?
Or is it somehow possible to use binary operators with strongly typed arguments?

1 Like

Got it working:

var qx = new QueryContainerDescriptor<Recommendation>();
var q1 = qx.Match(q => q
	.Field(f => f.Similarto.Suffix("keyword"))
	.Query("55445"));
var q2 = qx.Match(q => q
	.Field(f => f.Metatag.Suffix("keyword"))
	.Query("hot"));
var q3 = q1 || q2;

var response2 = client.Search<Recommendation>(s => s
	.AllTypes().From(0).Size(10)
	.Query(q => q3y));

Produces:

{
	"from": 0,"size": 10,
	"query": {"bool": { "should": [
		{ "match": { "similarto.keyword": { "query": "55445" }}}, 
		{ "match": { "metatag.keyword": { "query": "hot" }}}
	]}}
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.