Defined search index for object names

This commit is contained in:
Andrew Henry 2025-04-10 11:57:47 -07:00
parent e5631c9f6c
commit 9cc629825d
3 changed files with 56 additions and 17 deletions

View File

@ -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) {

View File

@ -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() {

View File

@ -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' \