Getting the most out of the XPages View Panel Control – Part 3: Implementing Field-Specific Search Functionality

In part 2 of this series, I showed how you can implement a dynamic list of Jump To options in a standard View Panel control. In this post, I’ll demonstrate is how to provide field-specific full-text search capability in a view.

Implementing standard full-text search

Implementing full-text search capability has been documented since before most of us ever heard of XPages in Declan Lynch’s ‘Learning XPages’ tutorial, so I won’t focus on it in as much detail here.

At a high level, you can implement full-text searching on a view panel with these three steps:

  1. Add a search field to your page and bind it to a scope variable.
    ViewPanel_3A
  2. Update the view panel’s data source to use the value of that scope variable in the search property
    ViewPanel_3B
  3. Add a button next to the search field that triggers a partial refresh on the view panel

This will execute the search in your view and display the results on the same page.

Of course, the database is full-text indexed, if not, you’ll get an error like this:

ViewPanel_3C

Enhancing the search to be field-specific

We can pretty easily take this a step further and allow our users to perform field-specific searches.

To do so, we’ll need to take these three steps:

  1. Add a combo box with a list of options for the field to search.
  2. Remove the binding of the search field to the scope variable (if you already have it in place)
  3. Update the Search button to build the search string and set it in the scope variable that the view is expecting

Building the list of options

Create a combo box with the list of search options. Make the value of each option correspond to the actual field name on the documents. Otherwise, you’ll need a little bit more code on the Search button to map the selection to the appropriate field name.

Also, be sure to include an ‘All Fields’ option to allow a search across all fields.

Building the search string

The key to enhancing the search from being generic full-text to being field-specific lies in the search string syntax. If you’ve done this before with LotusScript, you’ll find that you already know it!

To search my database of NFL teams for all teams in the National Football Conference (NFC), I would need the search string to look like this:

FIELD Conference CONTAINS NFC

To search the database for all teams that are located in a city that starts with the letter P, I would need this search string:

FIELD Location CONTAINS P*

Instead of requiring that the users type the precise full value of the field that they are searching, I generally append an asterisk to the search in order to provide the results that the user is most likely expecting.

The Search Button Code

Now that this is all set up, we just need to add some code to the Search button to build the search string and store it in the scope variable.

var field = getComponent('cbField').getValue();
var search = getComponent('txtSearch').getValue();
var query = '';
if (field == 'All Fields') {
	query = search + '*';
} else {
	query = 'FIELD ' + field + ' CONTAINS ' + search + '*';
}
viewScope.searchString = query;

Line 1 gets the selected field to search (my field’s ID is cbField)

Line 2 gets the search string (my field’s ID is txtSearch)

Lines 4-5 handle the generic full-text search for the case when a specific field is not selected

Line 7 builds the field-specific search string and appends the asterisk to provide more predictable results

Line 9 stores the search string in the scope variable that the view will use to filter the results

Here is the final result:

ViewPanel_3D

Now, I can open my view and easily perform a search for all teams who are located in a city that starts with the letter P.

ViewPanel_3E

This is a simple example, in a larger database, you will likely need to build the form name into your search, by including ‘FIELD Form CONTAINS form_name’ in the search string.

Further Enhancement

Once you know the syntax to building the full-text search, there are numerous ways you could change the interface for it, depending on your needs. You could build a search form that provides many more options for field-specific searches. You could also provide a series of combo boxes for the user to select multiple parameters to filter the view. You could even populate the combo boxes dynamically based on data in the view, using a concept similar to the one I showed in part 2 of this series.

Advertisements

11 responses to “Getting the most out of the XPages View Panel Control – Part 3: Implementing Field-Specific Search Functionality”

  1. Brian Moore says :

    Great work! This is exactly what I need for a project. Thanks for saving me the time it would have taken me to work it out on my own.

    Cheers,
    Brian

  2. sridevi says :

    How we could do this search for a date range. I tried in many ways mentioned below . it is not working.

    Searchquery = Field Deadline is after “08/01/2013”
    Searchquery = Field Deadline is after 08/01/2013
    Searchquery = Field Deadline >= “08/01/2013”
    Searchquery = Field Deadline >= 08/01/2013

    • Brad Balassaitis says :

      Dates require different handling. In code where I’ve seen it work, we read the date value into a java.util.Date object and formatted it as yyyy-MM-dd before adding it to the search string.

  3. sridevi says :

    i even tried that it is not working Brad… Can you plz attach some sample codes.

  4. Richard Arun Kumar.A says :

    var FromValue=getComponent(“fromDate”).getValue();
    var f_date:NotesDateTime = session.createDateTime(FromValue);
    var From = f_date.getDateOnly();
    Searchquery = Field subdate>=From

  5. Keertika says :

    Thanks Brad but i am unable to see the images. I need to know the 2nd point that you mentioned.
    Update the view panel’s data source to use the value of that scope variable in the search property

  6. Amirul Afiq says :

    Hi, im very new to xpages. I was wondering can we use other keyword instead of FIELD like AUTHOR or else. I’ve tried to filter by author but failed. Please help. Thanks

    • Brad Balassaitis says :

      The search generally either works on a specific field (in which case you’d need to use FIELD field_name or [field_name]) or if you just search on text by itself, it will do a full-text search of any field on the document.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: