Creating a Dynamic Index via the REST API
This example quickly creates the same index as Creating a Dynamic Index via the UI.
The cURL command below was initially created via the Classic Editor in the UI, however the follwoing modifications were made.
-
The curl flag "-s" to suppress some runtime output.
-
The credentials "-u <username>:<password>" were altered to "-u ${CB_USERNAME}:${CB_PASSWORD}".
-
The hostname or IP address was replaced with ${CB_HOSTNAME}.
-
The commands output is piped through the utility jq to enhance readability.
-
The two (2) UUIDs were removed (similar to the below) because we want to make a new index not modify an existing one.
json"uuid": "273a60635f5248e5", "sourceUUID": "2b421d183cb76aebbffa45424736ec2e",
The Creation Command
The full command to create the index is below and can be executed verbatim if you have the environment variable ${CB_USERNAME}, ${CB_PASSWORD} and ${CB_HOSTNAME} set.
commandcurl -s -XPUT -H "Content-Type: application/json" \ -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/index/test_dynamic -d \ '{ "type": "fulltext-index", "name": "test_dynamic", "sourceType": "gocbcore", "sourceName": "travel-sample", "planParams": { "maxPartitionsPerPIndex": 1024, "indexPartitions": 1 }, "params": { "doc_config": { "docid_prefix_delim": "", "docid_regexp": "", "mode": "scope.collection.type_field", "type_field": "type" }, "mapping": { "analysis": {}, "default_analyzer": "standard", "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": { "_default._default": { "dynamic": true, "enabled": true } } }, "store": { "indexType": "scorch", "segmentVersion": 15 } }, "sourceParams": {} }' | jq .
If you successfully create the index you should a response liekt the follwoing
json{
"status": "ok",
"uuid": "56304e8bab69bc99"
}
Test the Dynamic Index with a simple query
Request the first 10 item closet to a longitude of -2.235143
and a latitude of 53.482358
.
The target-field geo
is specified, as is a distance
of 100
miles: this is the radius within which target-locations must reside for their documents to be returned. Don’t worry about newlines when you paste the text.
commandcurl -s -XPOST -H "Content-Type: application/json" \ -u ${CB_USERNAME}:${CB_PASSWORD} http://${CB_HOSTNAME}:8094/api/index/test_dynamic/query \ -d '{ "query": { "query": "+view +food +beach" }, "size": 10, "from": 0 }' | jq .
The output of a ten (10) hits (from a total of 121 matching docs) is as follows
json{
"status": {
"total": 1,
"failed": 0,
"successful": 1
},
"request": {
"query": {
"query": "+view +food +beach"
},
"size": 10,
"from": 0,
"highlight": null,
"fields": null,
"facets": null,
"explain": false,
"sort": [
"-_score"
],
"includeLocations": false,
"search_after": null,
"search_before": null
},
"hits": [
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "landmark_38035",
"score": 1.1579735254455,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "landmark_4428",
"score": 1.0216606971061395,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "landmark_26385",
"score": 0.8510363574544033,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_6169",
"score": 0.6627638582612397,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_15914",
"score": 0.6488767405998539,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_15917",
"score": 0.6408954058353277,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_35855",
"score": 0.5994386303570878,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_21855",
"score": 0.5876768363989866,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_21889",
"score": 0.5815097705436758,
"sort": [
"_score"
]
},
{
"index": "test_dynamic_56304e8bab69bc99_4c1c5584",
"id": "hotel_5080",
"score": 0.5795265708969183,
"sort": [
"_score"
]
}
],
"total_hits": 121,
"max_score": 1.1579735254455,
"took": 916181,
"facets": null
}