Developer forum

Forum » Dynamicweb 10 » DW9 -> 10 content search

DW9 -> 10 content search

Harry
Reply

Hi team,

I've recently inherited a DW9 project built using Razor templates, that we are in the midst of upgrading to DW10. I'm coming into this with no DynamicWeb experience at all, and am struggling to implement a full content search in the new version of our sites.

The sites were previously using the searchv1 module (weighted search?), and I've read both the v9 and v10 documentation about implementing seach using repositories to no avail. Could you point me in the right direction?

Cheers


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Setting Up Content Search in Dynamicweb 10 CMS

This guide will walk you through the process of setting up content search in Dynamicweb 10 CMS. You'll learn how to create a repository, add an index using the Content Index Builder, create a query on the index, and set up a summary field for free-text search. We'll then implement the search on a page using the Query Publisher with a Razor template that includes a search box and a list of search results. Finally, we'll explore next steps to enhance the search functionality, such as setting up spell/term suggestions and introducing filtering using facets.


1. Creating a Repository

A repository in Dynamicweb is a container that holds indexes and queries. Here's how to create one:

  1. Access the Backend: Log in to your Dynamicweb administration interface.
  2. Navigate to Repositories: Go to Settings > Repositories.
  3. Add a New Repository:
    • Click on Add Repository.
    • Enter a Name for your repository (e.g., ContentSearchRepo).
    • Optionally, add a Description.
    • Click Save.

2. Adding an Index Using the Content Index Builder

Indexes enable efficient searching by organizing content data. To add an index:

  1. Select Your Repository: Click on the repository you just created.
  2. Add a New Index:
    • Click on Add Index.
    • Choose Content Index Builder from the list of available index builders.
  3. Configure the Index:
    • Name: Enter a name for the index (e.g., ContentIndex).
    • Areas to Index: Select the content areas to include (e.g., pages, paragraphs).
    • Fields: Define which content fields to index (e.g., Title, Body, Summary).
    • Filters: Apply any necessary filters to exclude unwanted content.
  4. Build the Index:
    • Save your settings.
    • Click Build Index Now to generate the index.

3. Creating a Query on the Index

Queries define how you retrieve and display data from the index.

  1. Navigate to Queries:
    • Within your repository, go to the Queries tab.
  2. Add a New Query:
    • Click Add Query.
    • Provide a Name for the query (e.g., ContentSearchQuery).
  3. Configure Query Criteria:
    • Criteria: Set up conditions for filtering search results based on fields.
    • Sorting: Define how results should be sorted (e.g., by relevance or date).
    • Parameters: Enable user input parameters for dynamic searching (e.g., FreeText for search terms).
  4. Save the Query.

4. Setting Up a Summary Field for Free-Text Search

A summary field allows you to combine multiple fields for full-text search.

  1. Edit the Index Fields:
    • Go back to your ContentIndex.
    • Navigate to the Fields tab.
  2. Add a Summary Field:
    • Click Add Field.
    • Choose Summary Field.
  3. Configure the Summary Field:
    • Name: Enter a name (e.g., CombinedContent).
    • Included Fields: Select the fields to include (e.g., Title, Body).
    • Analyzer: Choose an appropriate text analyzer for processing (e.g., StandardAnalyzer).
  4. Save and Rebuild:
    • Save the summary field.
    • Rebuild the index to include the new field.

5. Setting Up the Search on a Page

Using the Query Publisher

The Query Publisher module displays search results based on your query.

  1. Navigate to Content:
    • Go to Content > Pages.
  2. Select or Create a Page:
    • Choose the page where you want the search feature or create a new one.
  3. Add the Query Publisher Module:
    • In the page content area, click Add Paragraph.
    • Add the Query Publisher module to the paragraph.
  4. Configure the Module:
    • Repository: Select ContentSearchRepo.
    • Query: Choose ContentSearchQuery.
    • Template: Assign a Razor template (we'll create one next).
    • Parameters: Map the FreeText parameter to a query string (e.g., q).

Creating a Razor Template with a Search Box and Search Results

  1. Create a Template File:
    • In your website's file system, navigate to the templates folder for Query Publisher (e.g., /Templates/QueryPublisher).
    • Create a new Razor (.cshtml) file (e.g., SearchResults.cshtml).
  2. Add the Model Declaration:
    • At the top of your Razor template, declare the model:
      @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.QueryPublisher.QueryResultViewModel>
      
  3. Design the Search Box:
    • Include an HTML form with an input field for search terms.
      <form method="get">
          <input type="text" name="q" value="@Request.Query["q"]" placeholder="Search..." />
          <button type="submit">Search</button>
      </form>
      
  4. Display Search Results:
    • Check if there are any results and loop through them.
      @if (Model.Items != null && Model.Items.Any())
      {
          foreach (var item in Model.Results)
          {
              <div class="search-result">
                  <h2>@item.GetString("Title")</h2>
                  <p>@item.GetString("Body")</p>
                  <a href="@item.GetString("Url")">Read more</a>
              </div>
          }
      }
      else
      {
          <p>No results found for "@Request.Query["q"]".</p>
      }
      
  5. Handle Pagination (Optional):
    • If your query supports pagination, you can add navigation controls.
  6. Save the Template.

6. Next Steps to Improve the Search

Setting Up Spell/Term Suggestions

Enhance user experience by suggesting correct spellings for search terms.

  1. Enable Suggestions in the Index:
    • Edit your ContentIndex.
    • Go to the Suggestions tab.
    • Enable suggestions and configure settings like Minimum Term Frequency.
  2. Rebuild the Index:
    • Rebuild the index to include suggestion data.
  3. Modify the Razor Template:
    • After the search box, display suggestions if available.
      @if (Model.SpellCheckerSuggestions != null && Model.SpellCheckerSuggestions.Any())
      {
          <p>Did you mean:</p>
          <ul>
              @foreach (var suggestion in Model.SpellCheckerSuggestions)
              {
                  <li>
                      <a href="?q=@suggestion">@suggestion</a>
                  </li>
              }
          </ul>
      }
      

Introducing Filtering with Facets

Facets allow users to filter search results by categories or attributes.

  1. Define Facets in the Index:
    • Edit your ContentIndex.
    • Navigate to the Facets tab.
    • Add facets based on fields like Category or Author.
  2. Configure Facets in the Query:
    • Edit ContentSearchQuery.
    • Under Facets, select the facets to include.
  3. Update the Razor Template:
    • Display facet options for users to filter results.
      @if (Model.FacetGroups != null && Model.FacetGroups.Any())
      {
          foreach (var facet in Model.FacetGroups)
          {
              <div class="facet-group">
                  <h3>@facet.Name</h3>
                  <ul>
                      @foreach (var facetValue in facet.Facets)
                      {
                          var isSelected = facetValue.Selected;
                          var facetUrl = facetValue.Url;
      
                          <li>
                              @if (isSelected)
                              {
                                  <strong>@facetValue.Name (@facetValue.Count)</strong>
                              }
                              else
                              {
                                  <a href="@facetUrl">@facetValue.Name (@facetValue.Count)</a>
                              }
                          </li>
                      }
                  </ul>
              </div>
          }
      }
      
  4. Handle Facet Selections:
    • Ensure selected facets are applied to the query and results by using the URLs provided in the facet values.
 
Harry
Reply

Thanks!

 
Harry
Reply

Hi Nicolai,

Thanks for your detailed post! I tried my best to follow along - it appears that our interfaces aren't the same. For example, when trying to add an Index to my new Repository I didn't have the option to set the ContentIndexBuilder at the create step, I had to add it when creating a new Build.

I managed to attach my Query Publisher to a paragraph, but when I tried to load the page I ran into this error message:

And when I tried to add a Facet to my Query, I ran into this error:

I've attached a zip of my repository's content, could you provide any insight as to where I've got wrong?

Thanks,
Harrison

 

 
Dynamicweb Employee
Aleksandar Borislavov Ivanov
Reply

Hi Harry,

Will you try to check the "Indexed" and "Analyzed" settings on the field?

 

Also, the sources of your summary field should be the fields you want to search in. Here's an example configuration:

 

 

 
Harry
Reply

Hi Aleksandar,

Unfortunately setting the summary field to be indexed and analysed didn't fix the validation of the query:

 

Also, the only source I have available in my summary field is the CombinedContent - have I done something wrong in the setup?

Thanks,
Harry

 

You must be logged in to post in the forum