Tutorial 1: Free-text search

This guide will show you how to set up a free text search and use it to search a product list published with the Product Catalog app (Figure 1.1).

We will make it possible for visitors to search on the following data:

  • Product names
  • Short descriptions
  • Long descriptions

Simply follow this step-by-step guide, and you’re set.

 

A repository is a kind of top folder for indexing configuration files – each repository can contain a number of indexes, queries etc.

If you don't have a repository you should create one: 

  • Go to you Settings
  • Right click on the Repositories node and Click New Repository
  • Name it something appropriate e.g. Products
  • Click OK

This creates a new repository and opens the configuration page (Figure 2.1).​

Figure 2.1 Your new empty repository should look like this

Next, you must index your content – in this case your products. This is a fairly complicated process, but of course it only needs to be done once, after which the index can be automatically rebuilt at intervals.

Creating a working index requires you to add the elements in the proper order – Index > Instance > Build > Fields.

  • Click Add index
    • Name it
    • Click OK
  • Click Add instance
    • Name it
    • Select the LuceneIndexProvider
    • Provide a folder name
    • Click OK
  • Click Add Build
    • Name it
    • Select the Dynamicweb.Ecommerce.Indexing.ProductIndexBuilder
    • Select Full as your builder action
    • Click OK
  • Click Add field
    • Select field type Schema extender
    • Select ProductIndexSchemaExtender as your type
    • Click OK
  • Save your index, then build the index (click the "build"-button next to your build. It has the the same name as your instance)
  • Save and close your index – it should look like Figure 3.1
Figure 3.1 Your index should look like this

After you build the index a long list of schema extender fields are shown below the Field section - these are fields in the index which we have preconfigured for you.

This simplifies setting up an index greatly but you will want to add fields manually later on when you have more advanced needs.

Protip: It is usually recommended that you create at least two intances - that way users will still be able to search while the other instance is being (re)built. For this tutorial you need only create one, however.

 Once you have an index configured and built, you can start querying it – asking it to return information to you.

We will be building a simple query asking for active products containing a particular search term in their name or descriptions.

  • Click Add query
    • Name it
    • Select your index as data source
    • Click OK
  • Click Add parameter
    • Name it (e.g. Search)
    • Select System.String as type
    • Click OK
  • Click Expressions > Add group
    • In the dropdowns, select the Active field and the Equal operator.
    • Click the pencil and select Constant & System.Boolean. Set value to true.
    • Click Add group and select the OR operator in the dropdown
    • Add four expressions (Figure 4.1)
      • Select Product name, Product number, Short description and Long description from the dropdowns
      • Select Contains as the operator
      • Click the pencil and select Parameter > Search for all four
  • Save and close your query
Figure 4.1 Your expressions should look like this

Protip: In real-life scenarios you won't be querying each content field manually - rather, you will typically create a Summary field to query and then add/remove fields from it as necessary.

If you're creating a content index - an index of all pages and paragraph content on the solution - the schema extender contains fields with all text included, e.g. paragraph content and page items content.

Now that the query has been created you can pass a value to the Search parameter and the index will return a result matching the value - let's give it a go:

  • Create or open a paragraph with a product catalog app attached
  • In the app settings:
    • Under Show select Index
    • Under the Index settings select your query

This causes the app to publish only the products returned by the selected query - which in this case is all active products - unless restricted by a value passed to a parameter.

You can pass a value to a parameter by simply adding it to the URL in the format ?[parametername]=[value], but typically you want to do so via a search box.

Add a searchbox to the product list template - you can start with the code below or create your own. The important thing is to name the Search input field after your query parameters - in this case Search.

HTML
<form name="EcomSearch" method="get" class="form search" role="form"> <div style="display: none"><input name="Id" value="@Pageview.Page.ID" /></div> <div class="form-group"><input type="search" name="Search" value='@GetValue("Ecom:Search.Query")' class="form-control" placeholder='@Translate("Search", "Search")'/></div> <div class="form-group"><button type="submit" class="btn btn-default">@Translate("Search", "Search")</button></div> </form>
  • Save and close your module settings

And that’s it. You’re done! 

Click Show page and start searching..