If I understand you correctly I think a query like this might do the trick. It's written here in the editor, and not compiled, so small syntax adjustments might be necessary
TopicQuery tq = new TopicQuery();
// Initialize text and subject criteria with wildcard filtering
tq.Subject = new StringCriterion();
tq.Subject.WildCardType = WildCardType.Both;
tq.Subject.Value = "subject";
tq.Text = new StringCriterion();
tq.Text.WildCardType = WildCardType.Both;
tq.Text.Value = "text";
// Specify that the topic must contain a reply with the same wildcard filter as obove
tq.Replies = new ReplyCollectionCriterion();
tq.Replies.Containing = new ReplyCriterion();
tq.Replies.Containing.Subject = new StringCriterion();
tq.Replies.Containing.Subject.WildCardType = WildCardType.Both;
tq.Replies.Containing.Subject.Value = "subject";
tq.Replies.Containing.Text = new StringCriterion();
tq.Replies.Containing.Text.WildCardType = WildCardType.Both;
tq.Replies.Containing.Text.Value = "text";
// Group subject, text and "replies-subquery" on the topic level
CriteriaGroup group1 = new CriteriaGroup();
group1.AddCriterion(tq.Subject);
group1.AddCriterion(LogicalOperator.Or, tq.Text);
group1.AddCriterion(LogicalOperator.Or, tq.Replies);
tq.AddCriteriaGroup(group1);
// Group subject and text on the "Containing" level
CriteriaGroup group2 = new CriteriaGroup();
group2.AddCriterion(tq.Replies.Containing.Subject);
group2.AddCriterion(LogicalOperator.Or, tq.Replies.Containing.Text);
tq.Replies.Containing.AddCriteriaGroup(group2);
Thank you Mattias! It looks like it would do what I intend it to do, but it does not work entirely. The ReplyCriterion part seems to match any topic. I isolated that part, the following query returns all topics:
TopicQuery tq = new TopicQuery();
// Only one reply in one topic contains this string
string keyword = "asdfasdfasdf1234";
tq.Replies = new ReplyCollectionCriterion();
tq.Replies.Containing = new ReplyCriterion();
tq.Replies.Containing.Subject = new StringCriterion();
tq.Replies.Containing.Subject.WildCardType = WildCardType.Both;
tq.Replies.Containing.Subject.Value = keyword;
tq.Replies.Containing.Text = new StringCriterion();
tq.Replies.Containing.Text.WildCardType = WildCardType.Both;
tq.Replies.Containing.Text.Value = keyword;
// Group subject and text on the "Containing" level
CriteriaGroup group2 = new CriteriaGroup();
group2.AddCriterion(tq.Replies.Containing.Subject);
group2.AddCriterion(LogicalOperator.Or, tq.Replies.Containing.Text);
tq.Replies.Containing.AddCriteriaGroup(group2);
Hi,
I've been looking at this same subject recently. I combined results from a TopicQuery with results from a ReplyQuery - possibly not the most elegant way to proceed but it seems to work fine. It searches both subject and text of all topics and replies. I've used the WildcardType.Both so it searches for "WildCard on both ends of the value" ("%Word1%", as mentioned above). Here's my SearchTopic function:
public MessageCollection SearchTopics(string searchText) { MessageCollection searchResults = new MessageCollection();
// The topic query
TopicQuery tq = new TopicQuery();
// Create subject criteria
StringCriterion subjectCriterion = new StringCriterion(); subjectCriterion.Value = searchText; subjectCriterion.WildCardType = WildCardType.Both; tq.Subject = subjectCriterion;
// Create text criteria
StringCriterion textCriterion = new StringCriterion(); textCriterion.Value = searchText; textCriterion.WildCardType = WildCardType.Both; tq.Text = textCriterion;
// Create group for criterion relating to topic subject, text
CriteriaGroup topicCriteriaGroup = new CriteriaGroup();
// Add criterion to criteriaGroup
topicCriteriaGroup.AddCriterion(tq.Subject); topicCriteriaGroup.AddCriterion(LogicalOperator.Or, tq.Text);
// Add this group to tq
tq.AddCriteriaGroup(topicCriteriaGroup);
// Get searched for topics
int totalTopicItems = 0; searchResults = QueryHandler.GetQueryResult<Community.Forum.Topic, MessageCollection>(tq, Utils.CacheTimeOut.Search, 1, 12, out totalTopicItems);
// The reply query
ReplyQuery rq = new ReplyQuery();
// Create subject criteria for reply
StringCriterion replySubjectCriterion = new StringCriterion(); replySubjectCriterion.Value = searchText; replySubjectCriterion.WildCardType = WildCardType.Both; rq.Subject = replySubjectCriterion;
// Create text criteria for reply
StringCriterion replyTextCriterion = new StringCriterion(); replyTextCriterion.Value = searchText; replyTextCriterion.WildCardType = WildCardType.Both; rq.Text = replyTextCriterion;
// Create group for criterion relating to reply subject, text
CriteriaGroup replyCriteriaGroup = new CriteriaGroup();
// Add criterion to criteriaGroup
replyCriteriaGroup.AddCriterion(rq.Subject); replyCriteriaGroup.AddCriterion(LogicalOperator.Or, rq.Text);
// Add this group to rq
rq.AddCriteriaGroup(replyCriteriaGroup);
// Get searched for replys
int totalReplyItems = 0; MessageCollection replyResults = QueryHandler.GetQueryResult<Community.Forum.Reply, MessageCollection>(rq, Utils.CacheTimeOut.Search, 1, 12, out totalReplyItems);
// Combine search results from replies with search results from topics
foreach (Message reply in replyResults) { searchResults.Add(reply); } return searchResults; }
I want my topic query to return topics where the topic subject, text or text of any of the replies match the search text. Is this possible? The first two I can manage, but what about the replies?