mirror of
https://github.com/nasa/openmct.git
synced 2025-06-01 23:20:50 +00:00
[Search] Aggregator merges results
The search aggregator now merges search results from different providers better. It removed duplicate results, and orders the list of results from highest to lowest score.
This commit is contained in:
parent
bc2072b8c6
commit
80e0bd875a
@ -15,7 +15,7 @@
|
|||||||
{
|
{
|
||||||
"key": "SearchController",
|
"key": "SearchController",
|
||||||
"implementation": "controllers/SearchController.js",
|
"implementation": "controllers/SearchController.js",
|
||||||
"depends": [ "$scope", "searchService" ]
|
"depends": [ "$scope", "searchService", "objectService" ]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"key": "SearchbarController",
|
"key": "SearchbarController",
|
||||||
|
@ -43,6 +43,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<mct-representation key="'grid-item'"
|
<mct-representation key="'grid-item'"
|
||||||
ng-repeat="result in results"
|
ng-repeat="result in results"
|
||||||
mct-object="result">
|
mct-object="result.object">
|
||||||
</mct-representation>
|
</mct-representation>
|
||||||
</div>
|
</div>
|
@ -50,12 +50,48 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove extra objects that have the same ID
|
||||||
|
function filterRepeats(results) {
|
||||||
|
var ids = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < results.length; i += 1) {
|
||||||
|
//if (ids.includes(results[i].id)) {
|
||||||
|
if (ids.indexOf(results[i].id) !== -1) {
|
||||||
|
// If this result's ID is already there, remove the object
|
||||||
|
results.splice(i, 1);
|
||||||
|
// Reduce loop index because we shortened the array
|
||||||
|
i -= 1;
|
||||||
|
} else {
|
||||||
|
// Otherwise add the ID to the list of the ones we have seen
|
||||||
|
ids.push(results[i].id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Order the objects from highest to lowest score in the array
|
||||||
|
function orderByScore(results) {
|
||||||
|
|
||||||
|
results = results.sort(function (a, b) {
|
||||||
|
if (a.score > b.score) {
|
||||||
|
return -1;
|
||||||
|
} else if (b.score < a.score) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
// Calls the searches of each of the providers, then
|
// Calls the searches of each of the providers, then
|
||||||
// merges the results lists so that there are not redundant
|
// merges the results lists so that there are not redundant
|
||||||
// results
|
// results
|
||||||
function mergeResults(inputID) {
|
function mergeResults(inputID) {
|
||||||
var resultsPromises = [],
|
var resultsPromises = [],
|
||||||
mergedResults = [];
|
mergedResults;
|
||||||
|
|
||||||
for (var i = 0; i < providers.length; i += 1) {
|
for (var i = 0; i < providers.length; i += 1) {
|
||||||
resultsPromises.push(providers[i].query(inputID));
|
resultsPromises.push(providers[i].query(inputID));
|
||||||
@ -63,9 +99,10 @@ define(
|
|||||||
|
|
||||||
mergedResults = getPromisedResults(resultsPromises, 0, []);
|
mergedResults = getPromisedResults(resultsPromises, 0, []);
|
||||||
|
|
||||||
//return mergedResults;
|
|
||||||
return mergedResults.then(function (c) {
|
return mergedResults.then(function (c) {
|
||||||
//console.log('returning ', c);
|
// Get rid of the repeated objects and put in correct order
|
||||||
|
c = filterRepeats(c);
|
||||||
|
c = orderByScore(c);
|
||||||
return c;
|
return c;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -27,15 +27,22 @@
|
|||||||
define(function () {
|
define(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
function SearchController($scope, searchService) {
|
function SearchController($scope, searchService, objectService) {
|
||||||
|
|
||||||
|
function getResults(inputID) {
|
||||||
|
|
||||||
|
// Later, the search result format will be different
|
||||||
|
// Will need to compile search result list (for this
|
||||||
|
// result page) here, using pseudo linkedlist searchResult
|
||||||
|
|
||||||
return {
|
|
||||||
// Search the database using the user input of id "searchinput"
|
|
||||||
search: function (inputID) {
|
|
||||||
searchService.query(inputID).then(function (c) {
|
searchService.query(inputID).then(function (c) {
|
||||||
$scope.results = c;
|
$scope.results = c;
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
// Search the database using the user input of id "searchinput"
|
||||||
|
search: getResults,
|
||||||
|
|
||||||
// Check to see if there are any search results to display.
|
// Check to see if there are any search results to display.
|
||||||
areResults: function () {
|
areResults: function () {
|
||||||
@ -44,7 +51,16 @@ define(function () {
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}/*,
|
||||||
|
|
||||||
|
// Get a domain object from its ID
|
||||||
|
getObjectByID: function (id) {
|
||||||
|
return objectService.getObjects([id]).then(function (objects) {
|
||||||
|
var obj = objects[id];
|
||||||
|
console.log('get object', obj);
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
}*/
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return SearchController;
|
return SearchController;
|
||||||
|
@ -92,7 +92,7 @@ define(
|
|||||||
searchTerm = 'name:' + searchTerm;
|
searchTerm = 'name:' + searchTerm;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('search term ', searchTerm);
|
//console.log('search term ', searchTerm);
|
||||||
return searchTerm;
|
return searchTerm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,6 +102,8 @@ define(
|
|||||||
var results = rawResults.data.hits.hits,
|
var results = rawResults.data.hits.hits,
|
||||||
resultsLength = results.length,
|
resultsLength = results.length,
|
||||||
ids = [],
|
ids = [],
|
||||||
|
scores = {},
|
||||||
|
searchResults = [],
|
||||||
i;
|
i;
|
||||||
|
|
||||||
if (rawResults.data.hits.total > resultsLength) {
|
if (rawResults.data.hits.total > resultsLength) {
|
||||||
@ -114,24 +116,36 @@ define(
|
|||||||
ids.push(results[i][ID]);
|
ids.push(results[i][ID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the result objects' scores
|
||||||
|
for (i = 0; i < resultsLength; i += 1) {
|
||||||
|
//scores.push(results[i][SCORE]);
|
||||||
|
scores[ ids[i] ] = results[i][SCORE];
|
||||||
|
}
|
||||||
|
|
||||||
// Get the domain objects from their IDs
|
// Get the domain objects from their IDs
|
||||||
return objectService.getObjects(ids).then(function (objects) {
|
return objectService.getObjects(ids).then(function (objects) {
|
||||||
var output = [],
|
var id,
|
||||||
id,
|
|
||||||
j;
|
j;
|
||||||
|
|
||||||
|
// Filter by search term
|
||||||
for (j = 0; j < resultsLength; j += 1) {
|
for (j = 0; j < resultsLength; j += 1) {
|
||||||
id = ids[j];
|
id = ids[j];
|
||||||
|
|
||||||
// Include any item except folders
|
// Include any item except folders
|
||||||
if (objects[id].getModel) {
|
if (objects[id].getModel) {
|
||||||
if (objects[id].getModel().type !== "folder") {
|
if (objects[id].getModel().type !== "folder") {
|
||||||
output.push(objects[id]);
|
// Format the results as searchResult objects
|
||||||
|
searchResults.push({
|
||||||
|
id: id,
|
||||||
|
object: objects[id],
|
||||||
|
score: scores[id]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
//console.log('searchResults (in ES provider)', searchResults);
|
||||||
|
return searchResults;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +67,20 @@ define(
|
|||||||
// Aquire My Items (root folder)
|
// Aquire My Items (root folder)
|
||||||
return objectService.getObjects(['mine']).then(function (objects) {
|
return objectService.getObjects(['mine']).then(function (objects) {
|
||||||
// Get all of its descendents
|
// Get all of its descendents
|
||||||
return itemsHelper([objects.mine], 0).then(function (c) {
|
return itemsHelper([objects.mine], 0).then(function (items) {
|
||||||
return c;
|
// Turn them into searchResult objects (object, id, and score)
|
||||||
|
var searchResultItems = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < items.length; i += 1) {
|
||||||
|
searchResultItems.push({
|
||||||
|
id: items[i].getId(),
|
||||||
|
object: items[i],
|
||||||
|
score: 1 // TODO: Find how to score these properly
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log('searchResultItems (in Everything)', searchResultItems);
|
||||||
|
return searchResultItems;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -112,10 +124,10 @@ define(
|
|||||||
term = term.toLocaleLowerCase();
|
term = term.toLocaleLowerCase();
|
||||||
|
|
||||||
// Get items list
|
// Get items list
|
||||||
return getItems().then(function (items) {
|
return getItems().then(function (searchResultItems) {
|
||||||
// Keep track of the number of results to display
|
// Keep track of the number of results to display
|
||||||
if (items.length < maxResults) {
|
if (searchResultItems.length < maxResults) {
|
||||||
resultsLength = items.length;
|
resultsLength = searchResultItems.length;
|
||||||
} else {
|
} else {
|
||||||
resultsLength = maxResults;
|
resultsLength = maxResults;
|
||||||
}
|
}
|
||||||
@ -123,17 +135,18 @@ define(
|
|||||||
// Then filter through the items list
|
// Then filter through the items list
|
||||||
for (i = 0; i < resultsLength; i += 1) {
|
for (i = 0; i < resultsLength; i += 1) {
|
||||||
// Prevent errors from getModel not being defined
|
// Prevent errors from getModel not being defined
|
||||||
if (items[i].getModel) {
|
if (searchResultItems[i].object.getModel) {
|
||||||
itemModel = items[i].getModel();
|
itemModel = searchResultItems[i].object.getModel();
|
||||||
itemName = itemModel.name.toLocaleLowerCase();
|
itemName = itemModel.name.toLocaleLowerCase();
|
||||||
|
|
||||||
// Include any matching items, except folders
|
// Include any matching items, except folders
|
||||||
if (itemName.includes(term) && itemModel.type !== "folder") {
|
if (itemName.includes(term) && itemModel.type !== "folder") {
|
||||||
searchResults.push(items[i]);
|
searchResults.push(searchResultItems[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//console.log('filtered searchResults (in Everything)', searchResults);
|
||||||
return searchResults;
|
return searchResults;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user