From 9cc629825d0c94a6822b42f21d2e5995f325cddc Mon Sep 17 00:00:00 2001 From: Andrew Henry Date: Thu, 10 Apr 2025 11:57:47 -0700 Subject: [PATCH] Defined search index for object names --- .../persistence/couch/CouchObjectProvider.js | 29 +++++++++++++++---- .../persistence/couch/CouchSearchProvider.js | 23 +++++++-------- .../persistence/couch/setup-couchdb.sh | 21 ++++++++++++++ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/plugins/persistence/couch/CouchObjectProvider.js b/src/plugins/persistence/couch/CouchObjectProvider.js index 3348872d98..00480a3e89 100644 --- a/src/plugins/persistence/couch/CouchObjectProvider.js +++ b/src/plugins/persistence/couch/CouchObjectProvider.js @@ -434,16 +434,35 @@ class CouchObjectProvider { return Promise.resolve([]); } - async getObjectsByView({ designDoc, viewName, keysToSearch }, abortSignal) { - const stringifiedKeys = JSON.stringify(keysToSearch); - const url = `${this.url}/_design/${designDoc}/_view/${viewName}?keys=${stringifiedKeys}&include_docs=true`; + async getObjectsByView( + { designDoc, viewName, keysToSearch, startKey, endKey, limit }, + abortSignal + ) { + let stringifiedKeys = JSON.stringify(keysToSearch); + const url = `${this.url}/_design/${designDoc}/_view/${viewName}`; + const requestBody = { + include_docs: true + }; + + if (startKey !== undefined && endKey !== undefined) { + requestBody.startkey = startKey; + requestBody.endkey = endKey; + } else { + requestBody.keys = stringifiedKeys; + } + + if (limit !== undefined) { + requestBody.limit = limit; + } + let objectModels = []; try { const response = await fetch(url, { - method: 'GET', + method: 'POST', headers: { 'Content-Type': 'application/json' }, - signal: abortSignal + signal: abortSignal, + body: JSON.stringify(requestBody) }); if (!response.ok) { diff --git a/src/plugins/persistence/couch/CouchSearchProvider.js b/src/plugins/persistence/couch/CouchSearchProvider.js index 7c0740eff3..741d7d7dd3 100644 --- a/src/plugins/persistence/couch/CouchSearchProvider.js +++ b/src/plugins/persistence/couch/CouchSearchProvider.js @@ -33,7 +33,10 @@ class CouchSearchProvider { #bulkPromise; #batchIds; #lastAbortSignal; - + /** + * + * @param {import('./CouchObjectProvider').default} couchObjectProvider + */ constructor(couchObjectProvider) { this.couchObjectProvider = couchObjectProvider; this.searchTypes = couchObjectProvider.openmct.objects.SEARCH_TYPES; @@ -68,17 +71,13 @@ class CouchSearchProvider { } searchForObjects(query, abortSignal) { - const filter = { - selector: { - model: { - name: { - $regex: `(?i)${query}` - } - } - } - }; - - return this.couchObjectProvider.getObjectsByFilter(filter, abortSignal); + const preparedQuery = query.toLowerCase().trim(); + return this.couchObjectProvider.getObjectsByView({ + designDoc: 'object_names', + viewName: 'object_names', + startKey: preparedQuery, + endKey: preparedQuery + encodeURIComponent(String.fromCharCode(0xfff0)) + }); } async #deferBatchAnnotationSearch() { diff --git a/src/plugins/persistence/couch/setup-couchdb.sh b/src/plugins/persistence/couch/setup-couchdb.sh index 88ae9f5323..e6be58c7aa 100755 --- a/src/plugins/persistence/couch/setup-couchdb.sh +++ b/src/plugins/persistence/couch/setup-couchdb.sh @@ -99,6 +99,27 @@ create_replicator_table() { add_index_and_views() { echo "Adding index and views to $OPENMCT_DATABASE_NAME database" + # Add object names search index + response=$(curl --silent --user "${CURL_USERPASS_ARG}" --request PUT "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME"/_design/object_names/\ + --header 'Content-Type: application/json' \ + --data '{ + "_id":"_design/object_names", + "views":{ + "object_names":{ + "map":"function (doc) { if (doc.model && doc.model.name) {const name = doc.model.name.toLowerCase(); const tokens = name.split('\'' '\'');tokens.forEach((token) => {emit(token, doc._id);});}}" + } + } + }') + + if [[ $response =~ "\"ok\":true" ]]; then + echo "Successfully created object_names" + elif [[ $response =~ "\"error\":\"conflict\"" ]]; then + echo "object_names already exists, skipping creation" + else + echo "Unable to create object_names" + echo $response + fi + # Add type_tags_index response=$(curl --silent --user "${CURL_USERPASS_ARG}" --request POST "$COUCH_BASE_LOCAL"/"$OPENMCT_DATABASE_NAME"/_index/\ --header 'Content-Type: application/json' \