
Full-Text Search
Introducton
In the course material, you learned about the general process for identifying what needs to be indexed based on your search objectives, how to create the appropriate index, how to test a search and evaluate the results. In this lab you will implement two indexes using this approach and you will try your hand at running searches using a variety of different methods, including executing the search from application code.
What you will gain experience with:
-
Defining search objectives
-
Creating an index to support the objectives
-
Testing a search to evaluate the effectiveness of the search
-
Implementing the search in code
Estimated time to complete: 45 minutes
Instructions
You will spend the first part of this lab in the Capella dashboard. If you have not been active recently, you will likely need to log in again. The second half of the lab will be preformed from the lab environment. If you do not already have this active, you may want to open this in a separate tab. Your starting point will be here You should have already launched a session and this session will likely still be active. If for some reason the session has expired, you will need to go through the process of re-starting the session. Review the lab instructions from Lab 2.
Before beginning the labs, recall the overall process for defining a search that includes defining an index based on your objectives, understand and define the analysis process, execute the search, and evaluate the results.

For the exercises in this lab, we will be using the default analyzer and many of the default behaviors.
Finding users having a desired favorite genre
Use-Case
In this first scenario, you will be defining a search that is intended to return all userprofile documents having the expected genre found in one of the favoriteGenres array elements. To better visualize this, here is an example of an individual document.
{
"lastName": "Peña",
"address": {
"state": "castilla la mancha",
"city": "cartagena",
"countryCode": "ES",
"street": "2925 calle del barquillo",
"postalCode": 79910
},
"gender": "male",
"created": "2015-06-08T17:05:00",
"phones": [{
"verified": "2015-01-14T12:52:39",
"type": "cell",
"number": "669-101-079"
}],
"dateOfBirth": "1976-11-28",
"title": "Mr",
"type": "userprofile",
"picture": {
"large": "https://randomuser.me/api/portraits/men/47.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/47.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/47.jpg"
},
"firstName": "Ignacio",
"favoriteGenres": ["Rock"],
"pwd": "6d616770696573",
"updated": "2015-08-25T10:27:39",
"email": "ignacio.peña@msn.com",
"status": "active",
"username": "betrayalsslanderously7304"
}
Implementation
Based on the above specification, define a search index to enable the search, limiting the search to only documents of type userprofile and searching for matches only in the favoriteGenres field.
-
Go to the Full Text Search link, found under the Data Tools tab on the Couchify Database.
-
Add a new index with the following characteristics.
-
Name: couchmusic2-index-userprofile-genres
Note: This name is critical to ensure the code works with the index -
Bucket: couchmusic2
-
Ensure the box titled Use non-default scope/collection(s) is unchecked and also select the couchify scope
-
-
Define a custom type mapping by expanding the Advanced Configuration and clicking Add Mapping.
Define the mapping with the following characteristics
-
Collection - select the userprofile collection to only index documents in that collection
-
Uncheck index all contained fields
-
On this new type mapping, add a child field.
Specify the field by favoriteGenres and be sure all the boxes are checked (Index, Store,Include in all field, Include term vectors and Docvalues).
Once completed, you should have an index definition similar to the following.
Save this definition and let the indexing analysis complete
-
-
Test the index by entering a search term like rock or americana and verify that the search returns results. If no results are returned, go back and review the type mapping and the child field definition to make sure you correctly typed the values.
Executing in Code
Return to the Couchbase Associate Architect Lab Session page and locate the example code section on the top navigation corresponding to Lab 6: Search 1.
You will be executing the a code sample from one of the four covered SDK languages, which correspond to the index you just created in the prior section.
-
Review the search examples in your selected language
-
Notice the way in which the search is invoked
-
What object is the search invoked on?
-
How is this similar/different from the N1QL queries?
-
How are the results of the search processed?
-
-
Verify that the name used in the search matches the name you used to define the index in the Web UI.
-
Execute one or more of these code examples to see the results.
-
Now, locate the part of the code where the search term is specified (rock). This location varies depending on the code example for the language.
-
Change the search term to crock and execute the search again.
-
What were the results? The search returns no matches because there is no favorite genre that exactly matches crock.
-
Fix this by locating where the fuzziness value is set - this will vary by language. Change the fuzziness of 0 to 1 and re-execute the code. Notice that there are now matches because it only takes one change (delete, insert or substitution) to get from crock to rock.
-
Some things to think about
-
What happened when you changed the search term slightly and used crock instead of rock? How was this addressed?
-
Notice that the items being output are the values found on the SearchRow object.
-
What are these values?
-
What if you wanted to print the username and favorite genre for each document match? How would this be done?
In the next section, you will implement another search index and look at a way you can return the desired values from the matching documents as part of the search.
Finding song tracks matching a song title
Use-Case
For this second use-case, consider the challenge of finding a song with a certain word or phrase in it. Most of us have had a situation like that where you can remember part of the title but can’t quite recall the artist or actual song title. Perhaps you may not even correctly remember the word or phrase but still wish to locate the song that has been running around in your head along with the artist who performs the song. This exercise will involve creating an index to search for a song title and artist in tracks documents and printing out the the song title and artist for all matches.
Consider the following document to get an idea of how to set up the index.
{
"mp3": "https://stream.song365.co/h/227420/Billy%20Vaughn%20-%20Cocoanut%20Grove_(song365.cc).mp3",
"artist": "Billy Vaughn",
"created": "2015-08-17T21:00:36",
"ratings": [{
"rating": 1,
"created": "2015-08-18T18:07:40",
"username": "brambliertypo75631"
}, {
"rating": 4,
"created": "2015-08-19T07:31:57",
"username": "spearheadingswat92804"
}, {
"rating": 1,
"created": "2015-08-19T08:30:36",
"username": "libidinizeddepleting17126"
}, {
"rating": 3,
"created": "2015-08-21T20:29:34",
"username": "reconnoiteruterine63108"
}, {
"rating": 1,
"created": "2015-08-22T15:01:26",
"username": "originalsstylist66049"
}],
"avg_rating": 2.375,
"genre": "Orchestral Pop",
"id": "00003466BBCDD2242E13D71F075D01458B8910FE",
"title": "Cocoanut Grove",
"type": "track",
"updated": "2015-08-17T21:00:36"
}
Implementation
Return to the Search page on the Couchbase Admin console. Create a new index, this time focused on searching documents having a type of tracks. This index will be restricted to searching only the title field. However, it is desired to also return the associated artist for search matches. How can this be done?
-
Create an index called couchmusic2-track-title
Note: This name is critical to ensure the code works with the index
-
Recall from the last section how the index was defined.
-
You restricted the index to only index and store results for documents in the userprofile collection. This time, you will restrict the index to only documents in the track collection of the couchify scope in the couchmusic2 bucket.
-
You will only be indexing on the title field.
-
Ensure you indicate to index on specified fields.
-
Add that field as a child mapping. To ensure the title values are returned for matches, be sure to check the store option for that
-
-
In order to also return the artist field data for matches, add an additional child mapping for that field
-
Since you won’t be indexing on the artist field, uncheck the index option.
-
Since you want to return the data for this field on matches, check the remaining boxes.
-
Your resulting type mappings for the index should resemble the following.

Once ready, create the index.
Test the index on a few song titles like the rose or stairway to heaven. Note that the results (currently) don’t return both fields, but this can be verified later when running code for this search index.

Once you have the index created and verify the results, you are ready to return to the code examples.
Executing in Code
Return to the Couchbase Associate Architect Lab Session page and again locate. the code samples for Lab 6 : Search 2. Take a moment to examine the code for your selected language.
-
What changes have been made to the Search Query?
-
How has the result processing changed?
Go ahead and execute one or more of the code examples to see the results. You can see now that both the song title and the associated artist can be obtained from the search query and do not require a subsequent call to the database to get that information.
Lab Summary
In this lab, you had a chance to gain experience using the Couchbase Full-Text Search capability. As a result, you had a chance to create indexes suited to responding to certain searches and execute code in a variety of languages against those indexes.
Some things you have gained experience with include:
-
Using the index definition to control what documents are indexed
-
Specifying which fields of those documents will be indexed and whether the associated matching values will be returned on the search
-
Executing code to submit the search query and process the results
-
Using fuzziness to control how strictly the search term must match the associated data being searched
-
Storing additional field data in the search in order to make it available when matches are found.
Congratulations! You have completed this lab.