Configure an Autocomplete Search Index
- how-to
Create a Search index with the Couchbase Server Web Console or the REST API to start using autocomplete with the Search Service.
Prerequisites
-
You have the Search Service enabled on a node in your database. For more information about how to deploy a new node and Services on your database, see Manage Nodes and Clusters.
-
You have a bucket with scopes and collections in your database. For more information about how to create a bucket, see Create a Bucket.
-
Your user account has the Search Admin role for the bucket where you want to create the index.
-
You have logged in to the Couchbase Server Web Console.
Procedure
You can create a compatible Search index with the Web Console or the REST API.
Create the Autocomplete Search Index with the Web Console
To create the Search index with the Web Console:
-
Go to Search.
-
Click Add Index.
-
In the Index Name field, enter a name for the new index.
Your index name must start with an alphabetic character (a-z or A-Z). It can only contain alphanumeric characters (a-z, A-Z, or 0-9), hyphens (-), or underscores (_).
For Couchbase Server version 7.6 and later, your index name must be unique inside your selected bucket and scope. You cannot have 2 indexes with the same name inside the same bucket and scope.
-
In the Bucket list, select the bucket where you want to create the index.
-
Expand Customize Index.
-
Select Use non-default scope/collection(s).
-
In the Scope list, select the scope where you want to create the index.
-
Create a Custom Analyzer with the following settings:
-
In the Name field, enter
keyword_to_lower
. -
In the Tokenizer list, select single.
-
In the Token Filters list, select and add the to_lower token filter.
-
-
Expand Custom Filters.
-
Create a custom token filter with the following settings:
-
In the Name field, enter
edge_ngram_2_8
. -
In the Type list, select edge_ngram.
-
In the Min field, enter
2
, or the minimum number of characters you want the Search index to use for autocomplete. -
In the Max field, enter
8
, or the maximum number of characters you want the Search index to use for autocomplete.
-
-
Create another custom analyzer with the following settings:
-
In the Name field, enter
edge_ngram
. -
In the Tokenizer list, select unicode.
-
In the Token Filters list, select and add the to_lower and your custom edge_ngram_2_8 token filter.
-
-
Under Mappings, clear the default type mapping.
-
Create a Type Mapping with the following settings:
-
Clear index all contained fields.
-
-
(Optional) If the field you want to search with autocomplete is nested inside a JSON object in your documents, Create a Child Mapping for the JSON object with the following settings:
-
Clear index all contained fields.
-
-
Under the type mapping or child mapping, Create a Child Field with the following settings:
-
In the field field, enter the name of a text field in a document from your selected scope and collection.
-
In the analyzer list, select your edge_ngram analyzer.
-
Select index.
-
Select store.
-
Select include in _all field.
-
-
Expand Advanced.
-
In the Default Analyzer list, select your keyword_to_lower analyzer.
-
Click Create Index
You can use the generated index definition JSON payload from the UI to create a Search index with the REST API. |
Create an Autocomplete Search Index with the REST API
To create an autocomplete Search index from the REST API:
-
Create a Search Index with the REST API and curl/HTTP with the following JSON payload, replacing all placeholder values that start with a
$
:{ "type": "fulltext-index", "name": "e-ngram-2-8", "uuid": "$GENERATED_INDEX_UUID", "sourceType": "gocbcore", "sourceName": "$BUCKET_NAME", "sourceUUID": "$BUCKET_UUID", "planParams": { "maxPartitionsPerPIndex": 1024, "indexPartitions": 1 }, "params": { "doc_config": { "docid_prefix_delim": "", "docid_regexp": "", "mode": "scope.collection.type_field", "type_field": "type" }, "mapping": { "analysis": { "analyzers": { "edge_ngram": { "token_filters": [ "to_lower", "edge_ngram_2_8" ], "tokenizer": "unicode", "type": "custom" }, "keyword_to_lower": { "token_filters": [ "to_lower" ], "tokenizer": "single", "type": "custom" } }, "token_filters": { "edge_ngram_2_8": { "back": false, "max": 8, "min": 2, "type": "edge_ngram" } } }, "default_analyzer": "keyword_to_lower", "default_datetime_parser": "dateTimeOptional", "default_field": "_all", "default_mapping": { "dynamic": true, "enabled": false }, "default_type": "_default", "docvalues_dynamic": false, "index_dynamic": true, "store_dynamic": false, "type_field": "_type", "types": { "$SCOPE_NAME.$COLLECTION_NAME": { "dynamic": false, "enabled": true, "properties": { "$FIELD_NAME": { "dynamic": false, "enabled": true, "fields": [ { "analyzer": "edge_ngram", "include_in_all": true, "index": true, "name": "$FIELD_NAME", "store": true, "type": "text" } ] } } } } }, "store": { "indexType": "scorch", "segmentVersion": 15 } }, "sourceParams": {} }
Next Steps
To add an autocomplete feature to your application, see Add Autocomplete to Your Application for example code.