I have a query that I want to build with combination of AND and OR criteria. The obvious example is
- PublicationCriteria is always AND
- user specified criteria can be AND or OR
I have written the code below which works fine for
- no user criteria (only publication)
- single user criteria
But I cannot get the AND or OR to work when submitting multiple criteria
What am I doing wrong?
///PUBLIC API function that parses keywords and then calls private ExecuteQuery
public IEnumerable<string> FindBySubjectOrSyllabusKeyword(int publicationId, string keywords)
{
//TODO - Add logic to constrain to MAXIMUM two words, of which one can be parsed to INT
//Create search array and add wildcards
var searchArray = keywords.Split(',');
searchArray = Array.ConvertAll(searchArray, x => "%" + x.Trim() + "%");
var criteria = new List<Criteria>
{
new KeywordCriteria(_subjectCategory, searchArray, Criteria.Like),
new KeywordCriteria(_syllabusCategory, searchArray, Criteria.Like)
};
var sortParameter = new SortParameter(new ItemTitleColumn(), SortParameter.Ascending);
return _queryService.ExecuteQuery(publicationId, criteria, sortParameter, _resultFilterMax, false);
}
private global::Tridion.ContentDelivery.DynamicContent.Query.Query PrepareQueryCriteria(int publicationId, List<Criteria> criteria, bool isAndQuery)
{
Criteria allCriteria;
// Publication is always present as AND criteria
PublicationCriteria pubCriteria = new PublicationCriteria(publicationId);
if (criteria != null)
{
Criteria userCriteria;
if (isAndQuery)
{
userCriteria = CriteriaFactory.And(criteria.ToArray());
}
else
{
userCriteria = CriteriaFactory.Or(criteria.ToArray());
}
allCriteria = CriteriaFactory.And(new[] { pubCriteria, userCriteria });
}
else
{
allCriteria = pubCriteria;
}
var query = new global::Tridion.ContentDelivery.DynamicContent.Query.Query()
{
Criteria = allCriteria
};
return query;
}
EDIT The above is based on the approach illustrated here.
In response to the comment by @PankajGaur
The Tridion categories are used to classify exam resources. So we have various categories such as: SUBJECT = Physics, Chemistry, History ; COURSECODE = 0625, 0657, 0879. Each SUBJECT can have multiple COURSECODE values, but the latter are unique across all subjects
The users can search for either SUBJECT and/or COURSECODE
The query returns data if user enters PHYSICS, or 0625
The query returns nothing work if the user enters PHYSICS, 0625. This should return the single record for the specific course.
In all cases, the PUBLICATION criteria has to be included as an AND condition to limit query to a specific Tridion publication
The only thing I can think is that for each KeywordCriteria, the match to my searchArray terms is doing an AND rather than an OR match
P.S. As a T-SQL developer I would expect this to be simple to implement no?