Skip to main content
Logo

WebAPI search

The Contensis WebAPI Search is a powerful search mechanism that can allow searching across all content of a website or within specific folder contexts. Searches can be made on standard properties (e.g. path, title, etc), metadata, taxonomies or structured content without needing to do anything different. Composing a search is easy with our Fluent Interface design which allows chaining of filters, conditions, ordering and sub-queries. Full text searches are also supported, allowing fast searches across a large amount content using weightings and inflections.

This article will demonstrate the types of searches you may want to perform with examples, including paging and security enabled searches.

Simple query

This example demonstrates a simple search that looks for any webpage with a Title that contains the text 'news'.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<p>Simple Search</p>
 
@{
    // Standard properties are prefixed with 'Property_'
    var query = Query.Where("Property_Title").Contains("news");
 
    // The NodeFinder.Find() method will by default search from the root of the site.
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Simple Query with metadata

This example demonstrates a simple search that looks for any webpage that has a ShowOnFrontPage metadata item set to true.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Simple Search</h1>
 
@{
    // Metadata fields prefixed with 'MD_'
    var query = Query.Where("MD_ShowOnFrontPage").IsEqualTo("1");
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Simple Query with structured content data

This example demonstrates a simple search that looks for any Jobs webpage that has a type structured content taxonomy value set to contract. Searches for taxonomy use the taxonomy key which is why the taxonomy node has to be accessed by path first.

Finding Taxonomy Paths

You can find a taxonomy path or key by using the Taxonomy Manager within the management console in the Contensis UI

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
  
<h1>Simple Search</h1>
  
@{
     
    // Get the taxonomy node by path first to get hold of the key.
    var taxonomyNode = CurrentContext.Taxonomy.GetByPath("StructuredContent/Jobs/Types/Contract");
  
    var query = Query.Where("type").IsEqualTo(taxonomyNode.Key);
    var nodes = new NodeFinder().Find(query);
}
  
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Chaining queries

Chaining allows more complex searches by using the logical condition operators 'AND' and 'OR' which are expressed as methods.

Chaining conditions - AND

This example demonstrates a simple AND condition.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Search Example</h1>
 
@{
    // Queries can be easily chained.
    var query = Query.Where("MD_ShowOnFrontPage").IsEqualTo("1").And("Property_DateCreated").IsGreaterThan("'12 Oct 2011'");
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Chaining conditions - OR

This example demonstrates an OR condition.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Search Example</h1>
 
@{
    var query = Query.Where("Property_Path").StartsWith("/Products/Books/").Or("Property_Path").StartsWith("/Products/Toys/");
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Ordering results - example 1

This example demonstrates how to order the results in a descending direction.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Search Example</h1>
 
@{
    // The default is 'Ascending'
    var query = Query.Where("Property_Title").Contains("news").OrderBy("Property_DateCreated").Descending;
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Ordering results - example 2

Additional Ordering expressions can be appended.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Search Example</h1>
 
@{
    // Order by DateCreated descending then by Path ascending.
    var query = Query.Where("Property_Title").Contains("news").OrderBy("Property_DateCreated").Descending.ThenBy("Property_Path").Ascending;
    var nodes = new NodeFinder().Find(query);
}
 
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>

Combining queries

Queries can be appended to other queries which can be useful when you require conditional filters.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
 
<h1>Search Example</h1>
 
@{
    var folderNode = @CurrentNode.Parent.Parent;
    var query1 = Query.Where("Property_Title").Contains("news");
    var filterbyuser = Request.QueryString["filterbyuser"].AsBool();
 }
 @{
    // Only add the query if the 'filterbyuser' query string parameter is set
    // and the current user is actually logged in.
    if (filterbyuser == true && CurrentUser != null)
    {
        // Create a 2nd query to filter content to the current user.
        var query2 = Query.Where("Property_AuthorID").IsEqualTo(CurrentUser.ID.ToString());
         
        // Append the query to the 1st.
        query1 = query1.And(query2);
    }
     
    // Restrict the result count to 10.
    var nodes = new NodeFinder().Find(query1, itemCount:10);
}
 
<ul>
@foreach(var node in nodes)
{
    <li>@node.Title</li>
}
</ul>

Sub queries

Sub queries are queries within queries which can be used to build more complex logical conditions. The example below uses the .OrSubQuery() method, but there is also the .AndSubQuery() available to use. Sub queries are defined by using the SubQuery helper type to start the query.

@using Contensis.Framework.Web
@using Contensis.Framework.Web.Search
  
<h1>Simple Search</h1>
  
@{
    var query = Query.Where("Property_Path").StartsWith("/Products/Books/").OrSubQuery(
        SubQuery.Where("Property_Path").StartsWith("/Products/DVDs/").And("MD_ShowInListing").IsEqualTo("1")
    );
 
    var nodes = new NodeFinder().Find(query);
}
  
<ul>
    @foreach(var node in nodes)
    {
        <li>@node.Title</li>
    }
</ul>