Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1x 1x 19x 19x 2x 2x 19x 12x 12x 12x 9x 3x 3x 34x 4x 3x 3x 3x 2x 1x 1x 34x 7x 6x 6x 6x 1x 1x 5x 1x 1x 4x 4x 4x 3x 1x 1x 2x 2x 1x 1x 34x 9x 8x 8x 8x 2x 2x 6x 6x 2x 2x 4x 4x 4x 4x 4x 3x 3x 1x 1x 34x 14x 13x 13x 13x 1x 12x 2x 10x 2x 13x 5x 5x 8x 8x 8x 8x 8x 5x 8x 8x 3x 9x 9x 3x 2x 2x 2x 5x 5x 1x 1x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 1x 1x | /*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {HttpHeaders} from '@global/helpers/httpHeaders.mjs';
import helpers from '@back/controllers/helpers.mjs';
import datasets from '@back/helpers/datasets.mjs';
import { isRolesMode } from '@back/utils/roles.mjs';
import { getLoggerWithTag } from '@global/logger/v2/logger.mjs';
import { SEARCH_ALL_KEY } from '@global/search/constants.mjs';
import { SearchDataProvider } from '@global/search/search-data.mjs';
import { getFiltersForEntity, getFilterValueSuggestions, getSearchEntitiesList, performInEntitySearch, performSearchAll } from '@global/search/search-perform.mjs';
import { collectRelFields, parseRelTarget } from '@global/search/search-utils.mjs';
const LOG_TAG = 'controller-search';
const logger = getLoggerWithTag(LOG_TAG);
function getManifest(req) {
let manifest = req.storage.manifest;
if (isRolesMode()) {
const roleId = req.userProfile.roleId;
manifest = req.storage.manifests[roleId];
}
return manifest;
}
function createSearchDataProvider(req, manifest) {
const roleId = isRolesMode() ? req.userProfile?.roleId : undefined;
const datasetDriver = datasets(req.storage, roleId);
return new SearchDataProvider(manifest, (datasetId) =>
datasetDriver.releaseData(`/datasets/${datasetId}`)
);
}
function collectRelEntityIds(fullSchema, choice, manifest) {
return collectRelFields(fullSchema, choice, manifest)
.map(rel => parseRelTarget(rel.relTarget)?.entityId)
.filter(Boolean);
}
export default (app) => {
// Endpoint for getting search categories list
app.get('/seaf-core/api/core/storage/search/searchable-entities', async function(req, res) {
if (!helpers.isServiceReady(app, res)) return;
try {
const manifest = getManifest(req);
const result = getSearchEntitiesList(manifest);
res.status(200).json(result);
} catch (e) {
logger.error(() => 'Error in searchable-entities: ' + e.message, e);
res.status(500).json({ message: 'Unexpected error while composing searchable entities.' });
}
});
// Endpoint for getting filters for a category
app.get('/seaf-core/api/core/storage/search/entity-filters', async function(req, res) {
if (!helpers.isServiceReady(app, res)) return;
try {
const choice = req.query.choice;
if (!choice) {
res.status(400)
.json({
error: 'query param "choice" required'
});
return;
}
if (choice === SEARCH_ALL_KEY) {
res.status(200).json([]);
return;
}
const manifest = getManifest(req);
const fullSchema = req.storage?.schema ?? manifest;
const chosenEntitySchema = fullSchema?.properties?.[choice] ?? manifest.entities[choice]?.schema;
if (!chosenEntitySchema) {
res.status(400)
.json({
error: `Chosen entity ${choice} does not have schema.`
});
return;
}
const responseBody = getFiltersForEntity(manifest, choice, fullSchema, chosenEntitySchema);
res.status(200).json(responseBody);
} catch (e) {
logger.error(() => 'Error in entity-filters: ' + e.message, e);
res.status(500).json({ message: 'Unexpected error while composing filters for entity.' });
}
});
app.get('/seaf-core/api/core/storage/search/rel-suggestions', async function(req, res) {
if (!helpers.isServiceReady(app, res)) return;
try {
const relTarget = req.query.relTarget;
if (!relTarget || typeof relTarget !== 'string') {
res.status(400).json({ error: 'query param "relTarget" required' });
return;
}
const query = (req.query.query || '').trim().toLowerCase();
if (query.length === 0) {
res.status(200).json([]);
return;
}
const manifest = getManifest(req);
const parsed = parseRelTarget(relTarget);
Iif (!parsed?.entityId) {
res.status(200).json([]);
return;
}
const provider = createSearchDataProvider(req, manifest);
const entityData = await provider.getEntityData(parsed.entityId);
const items = getFilterValueSuggestions(manifest, relTarget, query, entityData);
res.status(200).json(items);
} catch (e) {
logger.error(() => 'Error in rel-suggestions: ' + e.message, e);
res.status(500).json({ message: 'Unexpected error while composing suggestions for filter.' });
}
});
app.post('/seaf-core/api/core/storage/search/search-run', async function(req, res) {
if (!helpers.isServiceReady(app, res)) return;
try {
const reqBodyRaw = req.body;
let error;
if (!reqBodyRaw) {
error = 'request must contains body';
} else if (!reqBodyRaw.choice || typeof reqBodyRaw.choice !== 'string') {
error = 'request body must contains string attr "choice"';
} else if (!reqBodyRaw.filters || !Array.isArray(reqBodyRaw.filters)) {
error = 'request body must contains array attr "filters"';
}
if (error) {
res.status(400)
.json({
error: error
});
return;
}
const manifest = getManifest(req);
const choice = reqBodyRaw.choice;
const contextHeader = req.headers?.[HttpHeaders.X_SFA_ORGCTX];
const orgCtx = typeof contextHeader === 'string' ? contextHeader : undefined;
const queryWords = (reqBodyRaw.searchQuery || '')
.trim()
.split(/\s+/)
.filter(Boolean)
.map(w => w.trim());
const provider = createSearchDataProvider(req, manifest);
if (choice === SEARCH_ALL_KEY) {
const searchableIds = getSearchEntitiesList(manifest).entities
.map(e => e.key)
.filter(k => k !== SEARCH_ALL_KEY);
const entityDataMap = await provider.getEntityDataMap(searchableIds);
const responseBody = await performSearchAll(manifest, queryWords, entityDataMap, orgCtx);
res.status(200).json(responseBody);
return;
}
const entityDef = manifest.entities?.[choice];
if (!entityDef) {
res.status(400)
.json({
error: `Chosen entity ${choice} is not found.`
});
return;
}
const fullSchema = req.storage?.schema ?? manifest;
const chosenEntitySchema = fullSchema?.properties?.[choice] ?? entityDef?.schema;
if (!chosenEntitySchema) {
res.status(400)
.json({
error: `Chosen entity ${choice} does not have schema.`
});
return;
}
const relEntityIds = collectRelEntityIds(fullSchema, choice, manifest);
const entityDataMap = await provider.getEntityDataMap([choice, ...relEntityIds]);
const filters = reqBodyRaw.filters;
const responseBody = await performInEntitySearch(
manifest,
fullSchema,
chosenEntitySchema,
choice,
filters,
queryWords,
entityDataMap[choice],
entityDataMap,
orgCtx
);
res.status(200).json(responseBody);
} catch (e) {
logger.error(() => 'Error in search-run: ' + e.message, e);
res.status(500).json({ message: 'Unexpected error while processing search request'});
}
});
};
|