In this topic I will cover some of the Search Query capabilities available in the Core Services.
SearchQueryData Class
This class is the base for all the Search Query operation available in the Core Services. The following fields are the most important ones.
Author: The results will return items created by the specified author.
filter.Author = new LinkToUserData() { IdRef="[tcm id]" };
Title: The results will return items containing the specified title or part of it.
filter.Title = "*title*";
Note that I am adding some asterisks at the beginning and at the end. Those asterisks are wild cards, this filter will return items which contains the word "title" at any part of the Title property.
SearchIn: The results will return items located in the specified item, for instance a folder.
filter.SearchIn = new LinkToIdentifiableObjectData() { IdRef="[Folder Tcm Id]" };
FullTextQuery:The results will return any item which xml representation contains the property value. Be careful while using this property since it could affect your search query performance.
filter.FullTextQuery = "Foot Note";
BasedOnSchemas: The results will return items by checking the actual field values based on schemas. For instance if you have an schema called "Note" and it has a field called "TopNote", you can search for items based on the schema "Note" where the "TopNote" field has the value "*First*".
BasedOnSchemaData basedSchemaNote = new BasedOnSchemaData();
basedSchemaNote.Schema = new LinkToSchemaData() { IdRef = "tcm:5-198-8" };
basedSchemaNote.Field = "TopNote";
basedSchemaNote.FieldValue = "*First*";
Note that you can specify wild cards as asterisks.
ItemTypes: The results will return items by checking the acutal ItemType.
filter.ItemTypes = new ItemType[] { ItemType.Component, ItemType.ComponentTemplate };
IsPublished: The results will return items by checking if they have been published.
filter.IsPublished = true;
Note that this property will just specify if the item is published or not. It does not specify where it was published, if you want to get that information you will need to filter the returned items by using a PublishingListFilterData filter or the GetListPublishInfo methods.
FromRepository: Normally used to specify the Publication where the search should take place.
ResultLimit: Restrict the number of returned results.
Sample of a Search Query
CoreServiceClient channel = new CoreServiceClient("basicHttp_2011");
SearchQueryData filter = new SearchQueryData();
filter.FullTextQuery = "Sample";
filter.ItemTypes = new ItemType[] { ItemType.Component };
BasedOnSchemaData basedSchema1 = new BasedOnSchemaData();
basedSchema1.Schema = new LinkToSchemaData() { IdRef = "tcm:5-198-8" };
basedSchema1.Field = "title";
basedSchema1.FieldValue = "*title*";
BasedOnSchemaData basedSchema2 = new BasedOnSchemaData();
basedSchema2.Schema = new LinkToSchemaData() { IdRef = "tcm:5-198-8" };
basedSchema2.Field = "title";
basedSchema2.FieldValue = "FootNote3";
filter.BasedOnSchemas = new BasedOnSchemaData[] { basedSchema1, basedSchema2 };
XElement results = channel.GetSearchResultsXml(filter);
for (IEnumerator<XElement> e = results.Descendants().GetEnumerator(); e.MoveNext(); ) {
Console.WriteLine("{Title: {0}}", e.Current.Attribute(XName.Get("Title")).Value);
}
The sample above will return all the items which have "Sample" as part of their xml representations, also it is checking if the title field contains the text "title" anywhere in the field or the exact "FootNote3" word.