Make searches by view more flexible

This commit is contained in:
Andrew Henry 2025-04-11 12:23:27 -07:00
parent bba6f0e790
commit 8679630621
3 changed files with 40 additions and 22 deletions

View File

@ -35,9 +35,13 @@ export default function (folderName, couchPlugin, searchFilter) {
);
},
load() {
return couchProvider.getObjectsByFilter(searchFilter).then((objects) => {
return objects.map((object) => object.identifier);
});
if (searchFilter.viewName !== undefined) {
// Use a view to search, instead of an _all_docs find
return couchProvider.getObjectsByView(searchFilter);
} else {
// Use the _find endpoint to search _all_docs
return couchProvider.getObjectsByFilter(searchFilter);
}
}
});
};

View File

@ -435,14 +435,15 @@ class CouchObjectProvider {
}
async getObjectsByView(
{ designDoc, viewName, keysToSearch, startKey, endKey, limit },
{ designDoc, viewName, keysToSearch, startKey, endKey, limit, objectIdField },
abortSignal
) {
let stringifiedKeys = JSON.stringify(keysToSearch);
const url = `${this.url}/_design/${designDoc}/_view/${viewName}`;
const requestBody = {
include_docs: true
};
const requestBody = {};
if (objectIdField === undefined) {
requestBody.include_docs = true;
}
if (startKey !== undefined && endKey !== undefined) {
requestBody.startkey = startKey;
@ -473,13 +474,21 @@ class CouchObjectProvider {
const result = await response.json();
const couchRows = result.rows;
couchRows.forEach((couchRow) => {
const couchDoc = couchRow.doc;
const objectModel = this.#getModel(couchDoc);
if (objectModel) {
objectModels.push(objectModel);
}
});
if (objectIdField !== undefined) {
const objectIdsToResolve = [];
couchRows.forEach((couchRow) => {
objectIdsToResolve.push(couchRow[objectIdField]);
});
objectModels = Object.values(await this.#bulkGet(objectIdsToResolve), abortSignal);
} else {
couchRows.forEach((couchRow) => {
const couchDoc = couchRow.doc;
const objectModel = this.#getModel(couchDoc);
if (objectModel) {
objectModels.push(objectModel);
}
});
}
} catch (error) {
// do nothing
}

View File

@ -34,8 +34,8 @@ class CouchSearchProvider {
#batchIds;
#lastAbortSignal;
/**
*
* @param {import('./CouchObjectProvider').default} couchObjectProvider
*
* @param {import('./CouchObjectProvider').default} couchObjectProvider
*/
constructor(couchObjectProvider) {
this.couchObjectProvider = couchObjectProvider;
@ -72,12 +72,17 @@ class CouchSearchProvider {
searchForObjects(query, abortSignal) {
const preparedQuery = query.toLowerCase().trim();
return this.couchObjectProvider.getObjectsByView({
designDoc: 'object_names',
viewName: 'object_names',
startKey: preparedQuery,
endKey: preparedQuery + encodeURIComponent(String.fromCharCode(0xfff0))
});
return this.couchObjectProvider.getObjectsByView(
{
designDoc: 'object_names',
viewName: 'object_names',
startKey: preparedQuery,
endKey: preparedQuery + '\\ufff0',
objectIdField: 'value',
limit: 1000
},
abortSignal
);
}
async #deferBatchAnnotationSearch() {