I am forming an elastic search query as below. I would like to loop through the values array dynamically and construct the query rather than hardcoding the array members.
var myQuery = q.Bool(bb => bb.Must(fi => fi.Term("_type", "myValue"),
fi => fi.Term("tags", values[0]),
fi => fi.Term("tags", values[1]),
fi => fi.Term("tags", values[2]),
fi => fi.Term("tags", values[3]),
fi => fi.QueryString(ques => ques.Query(query))));
I want to do something like the below, since the number of items in the values array may differ from time to time
var myQuery = q.Bool(bb => bb.Must(fi => fi.Term("_type", "myValue"),
foreach(item in Values)
{
fi => fi.Term("tags", item),
}
fi => fi.QueryString(ques => ques.Query(query))));
You can do something like this,
List<QueryContainer> myDynamicTermQuery = new List<QueryContainer>();
// Start of Logic
myDynamicTermQuery.Add(Query<YourType>.Term("_type", myValue))
foreach(item in Values)
{
myDynamicTermQuery.Add(Query<YourType>.Term("tags", item))
}
myDynamicTermQuery.Add(Query<YourType>.QueryString(ques => ques.Query(query)))
// End of Logic
var myQuery = q.Bool(bb => myDynamicTermQuery)
Got it working.
var myQuery = q.Bool(bb =>bb.Must(fi =>
{
var ff = fi.Term("_type", myValue);
ff = ssTerms.Aggregate(ff, (current, term) => current & fi.Term("tags", term));
ff &= fi.QueryString(ques => ques.Query(query));
return ff;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.