Skip to content

Commit

Permalink
Merge pull request #14 from biothings/fix_issue_167
Browse files Browse the repository at this point in the history
fix: Expand predicates, post query logs, query score
  • Loading branch information
marcodarko authored Jun 15, 2021
2 parents 1f7ec7f + a922134 commit 363aead
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/batch_edge_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const NodesUpdateHandler = require('./update_nodes');
const debug = require('debug')('biothings-explorer-trapi:batch_edge_query');
const CacheHandler = require('./cache_handler');
const utils = require('./utils');
const LogEntry = require('./log_entry');

module.exports = class BatchEdgeQueryHandler {
constructor(kg, resolveOutputIDs = true) {
Expand Down Expand Up @@ -50,19 +51,47 @@ module.exports = class BatchEdgeQueryHandler {
*/
async _postQueryFilter(response) {
try {
const filtered = response.filter((item) => {
let edge_predicate = item['$edge_metadata']['predicate'];
let predicate_filters = item['$edge_metadata']['trapi_qEdge_obj']['qEdge']['predicate'];
//remove prefix from filter list to match predicate name format
predicate_filters = predicate_filters.map((item) => utils.removeBioLinkPrefix(item));
//compare edge predicate to filter list
if (predicate_filters.includes(edge_predicate)) {
const filtered = response.filter(item => {
debug(`ITEM ${JSON.stringify(item)}`);
if (
'predicate' in item['$edge_metadata']['trapi_qEdge_obj']['qEdge']&&
'expanded_predicates' in item['$edge_metadata']['trapi_qEdge_obj']['qEdge']
) {
let edge_predicate = item['$edge_metadata']['predicate'];
let predicate_filters = [];
predicate_filters = item['$edge_metadata']['trapi_qEdge_obj']['qEdge']['expanded_predicates'];
if (predicate_filters) {
//add query predicate to the expanded list
predicate_filters.concat(edge_predicate);
//remove prefix from filter list to match predicate name format
predicate_filters = predicate_filters.map((item) => utils.removeBioLinkPrefix(item));
//compare edge predicate to filter list
this.logs.push(
new LogEntry('DEBUG', null, `query_graph_handler: Current edge post-query predicate restriction includes: ${JSON.stringify(predicate_filters)}`).getLog()
);
if (predicate_filters.includes(edge_predicate)) {
return item;
}
}else{
// No predicate restriction on this edge, just add to results
return item
}
}else{
// No predicate restriction on this edge, just add to results
return item;
}
});
// filter result
debug(`Filtered results from ${response.length} down to ${filtered.length} results`);
return filtered;
this.logs.push(
new LogEntry('DEBUG', null, `query_graph_handler: Total number of results returned for this query is ${response.length}.`).getLog()
);
this.logs.push(
new LogEntry('DEBUG', null, `query_graph_handler: Successfully applied post-query predicate restriction with ${filtered.length} results.`).getLog()
);
return filtered
} catch (error) {
// in case of rare failure return all
debug(`Failed to filter ${response.length} results due to ${error}`);
return response;
}
Expand Down
2 changes: 2 additions & 0 deletions src/query_results.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = class QueryResult {
this.results.push({
node_bindings: this._createNodeBindings(record),
edge_bindings: this._createEdgeBindings(record),
//default score issue #200 - TODO: turn to evaluating module eventually
score: "1.0"
});
});
}
Expand Down

0 comments on commit 363aead

Please sign in to comment.