ml-modules.root.data-hub.data-services.graph.searchNodes.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of marklogic-data-hub Show documentation
Show all versions of marklogic-data-hub Show documentation
Library for Creating an Operational Data Hub on MarkLogic
/**
Copyright (c) 2021 MarkLogic Corporation
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.
*/
// No privilege required: No special privilege is needed for this endpoint
import entityLib from "/data-hub/5/impl/entity-lib.mjs";
import graphUtils from "/data-hub/5/impl/graph-utils.mjs";
import httpUtils from "/data-hub/5/impl/http-utils.mjs";
const search = require('/MarkLogic/appservices/search/search.xqy');
const sem = require("/MarkLogic/semantics.xqy");
const returnFlags = `false
false
false
false
false
false
false
false
false
false
true `;
const stylesheet = fn.head(xdmp.unquote(`
${returnFlags}
`));
const queryParam = external.query;
const startParam = external.start;
const pageLengthParam = external.limit;
let query = queryParam;
let start = startParam;
let pageLength = pageLengthParam;
let structuredQuery = external.structuredQuery;
let queryOptions = external.queryOptions;
if (query == null) {
httpUtils.throwBadRequest("Request cannot be empty");
}
let queryObj = JSON.parse(query);
let relatedEntityTypeIds = queryObj.relatedEntityTypeIds || [];
let relatedEntityTypeIRIs = [];
let entityTypeIRIs = [];
let entitiesDifferentsFromBaseAndRelated = [];
let allRelatedPredicateList = [];
let predicateConceptList = [];
start = start || 0;
pageLength = pageLength || 1000;
let qrySearch;
if (structuredQuery !== undefined && structuredQuery.toString().length > 0) {
structuredQuery = fn.head(xdmp.unquote(structuredQuery)).root;
queryOptions = fn.head(xdmp.unquote(queryOptions)).root;
const newOptions = fn.head(xdmp.xsltEval(stylesheet, queryOptions)).root;
const searchResponse = fn.head(search.resolve(structuredQuery, newOptions));
qrySearch = cts.query(searchResponse.xpath('./*/*'));
}
fn.collection(entityLib.getModelCollection()).toArray().forEach(model => {
model = model.toObject();
const entityName = model.info.title;
const entityNameIri = entityLib.getEntityTypeId(model, entityName);
if (relatedEntityTypeIds.includes(entityName)) {
relatedEntityTypeIRIs.push(sem.iri(entityNameIri));
}
if (queryObj.entityTypeIds.includes(entityName)) {
//get predicate from concepts
predicateConceptList = predicateConceptList.concat(entityLib.getConceptPredicatesByModel(model));
if (relatedEntityTypeIds != null) {
const predicateListBaseEntities = entityLib.getPredicatesByModelAndBaseEntities(model, relatedEntityTypeIds);
allRelatedPredicateList = allRelatedPredicateList.concat(predicateListBaseEntities);
}
entityTypeIRIs.push(sem.iri(entityNameIri));
}
if (!queryObj.entityTypeIds.includes(entityName) && !(relatedEntityTypeIds && relatedEntityTypeIds.includes(entityName))) {
entitiesDifferentsFromBaseAndRelated.push(sem.iri(entityNameIri));
}
});
let ctsQuery = cts.trueQuery();
if (queryObj.searchText !== undefined && queryObj.searchText.toString().length > 0) {
const searchTxtResponse = fn.head(search.parse(queryObj.searchText));
ctsQuery = cts.query(searchTxtResponse);
if (qrySearch !== undefined) {
ctsQuery = cts.andQuery([qrySearch, ctsQuery]);
}
} else {
// if doesn't has search text, but could has facetSelects
if (qrySearch !== undefined) {
ctsQuery = qrySearch;
}
}
let conceptFacetList = [];
if (queryObj.conceptsFilterTypeIds != null) {
queryObj.conceptsFilterTypeIds.map(item => {
conceptFacetList.push(sem.iri(item));
});
}
const result = graphUtils.getEntityNodesWithRelated(entityTypeIRIs, relatedEntityTypeIRIs, predicateConceptList, entitiesDifferentsFromBaseAndRelated, conceptFacetList, ctsQuery, pageLength);
//get total from base entities
let resultBaseCounting = graphUtils.getEntityTypeIRIsCounting(entityTypeIRIs, ctsQuery);
let totalCount = fn.head(resultBaseCounting).total;
if (relatedEntityTypeIRIs.length) {
//get total from related entities
let totalRelatedEntities = graphUtils.getRelatedEntitiesCounting(allRelatedPredicateList, ctsQuery);
let totalRelated = fn.head(totalRelatedEntities).total;
totalCount += totalRelated;
}
if (predicateConceptList.length) {
//get total Concepts
let totalConcepts = graphUtils.getConceptCounting(entityTypeIRIs, predicateConceptList, ctsQuery);
let totalConcept = fn.head(totalConcepts).total;
totalCount += totalConcept;
}
const totalEstimate = totalCount;
const {nodes, edges} = graphUtils.graphResultsToNodesAndEdges(result, queryObj.entityTypeIds);
const supportsGraphConceptsSearch = true;
const response = {
supportsGraphConceptsSearch,
'total': (start === 0 && (!pageLength || totalEstimate < pageLength)) ? nodes.length: totalEstimate,
'start': start,
'limit': nodes.length,
'nodes': nodes,
'edges': edges
};
response;
© 2015 - 2024 Weber Informatics LLC | Privacy Policy