mirror of
https://github.com/nasa/openmct.git
synced 2025-01-20 11:38:56 +00:00
[Search] Rudimentary scroing for manual
Created a rudimentary scoring function for the EverythingSearchProvider. Also corrected the sorting by score function.
This commit is contained in:
parent
922a724e36
commit
442a1979e7
@ -76,7 +76,7 @@ define(
|
|||||||
results = results.sort(function (a, b) {
|
results = results.sort(function (a, b) {
|
||||||
if (a.score > b.score) {
|
if (a.score > b.score) {
|
||||||
return -1;
|
return -1;
|
||||||
} else if (b.score < a.score) {
|
} else if (b.score > a.score) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -75,7 +75,7 @@ define(
|
|||||||
searchResultItems.push({
|
searchResultItems.push({
|
||||||
id: items[i].getId(),
|
id: items[i].getId(),
|
||||||
object: items[i],
|
object: items[i],
|
||||||
score: 1 // TODO: Find how to score these properly
|
score: 0 // Assign actual score when filtering for term
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +85,43 @@ define(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate a score for an item based on its similarity to a search term
|
||||||
|
// Very rudimentary
|
||||||
|
function score(item, term) {
|
||||||
|
var name = item.object.getModel().name,
|
||||||
|
numWordsinName = name.split(' ').length,
|
||||||
|
numWordsinTerm = term.split(' ').length,
|
||||||
|
weight = 1.5,
|
||||||
|
score = (term.length / name.length)/*(numWordsinTerm / numWordsinName)*/ * weight;
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter through a list of searchResults based on a search term
|
||||||
|
function filterResults(results, term, resultsLength) {
|
||||||
|
var searchResults = [],
|
||||||
|
itemModel,
|
||||||
|
itemName;
|
||||||
|
|
||||||
|
for (var i = 0; i < resultsLength; i += 1) {
|
||||||
|
// Prevent errors from getModel not being defined
|
||||||
|
if (results[i].object.getModel) {
|
||||||
|
itemModel = results[i].object.getModel();
|
||||||
|
itemName = itemModel.name.toLocaleLowerCase();
|
||||||
|
|
||||||
|
// Include any matching items, except folders
|
||||||
|
if (itemName.includes(term) && itemModel.type !== "folder") {
|
||||||
|
// Score the result
|
||||||
|
score(results[i], term);
|
||||||
|
// Add the result to the result list
|
||||||
|
searchResults.push(results[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchResults;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches through the filetree for domain objects which match
|
* Searches through the filetree for domain objects which match
|
||||||
* the search term. This function is to be used as a fallback
|
* the search term. This function is to be used as a fallback
|
||||||
@ -105,10 +142,7 @@ define(
|
|||||||
function queryManual(inputID, maxResults) {
|
function queryManual(inputID, maxResults) {
|
||||||
var term,
|
var term,
|
||||||
searchResults = [],
|
searchResults = [],
|
||||||
resultsLength,
|
resultsLength;
|
||||||
itemModel,
|
|
||||||
itemName,
|
|
||||||
i;
|
|
||||||
|
|
||||||
// Check to see if the user provided a maximum
|
// Check to see if the user provided a maximum
|
||||||
// number of results to display
|
// number of results to display
|
||||||
@ -133,18 +167,7 @@ define(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Then filter through the items list
|
// Then filter through the items list
|
||||||
for (i = 0; i < resultsLength; i += 1) {
|
searchResults = filterResults(searchResultItems, term, resultsLength);
|
||||||
// Prevent errors from getModel not being defined
|
|
||||||
if (searchResultItems[i].object.getModel) {
|
|
||||||
itemModel = searchResultItems[i].object.getModel();
|
|
||||||
itemName = itemModel.name.toLocaleLowerCase();
|
|
||||||
|
|
||||||
// Include any matching items, except folders
|
|
||||||
if (itemName.includes(term) && itemModel.type !== "folder") {
|
|
||||||
searchResults.push(searchResultItems[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//console.log('filtered searchResults (in Everything)', searchResults);
|
//console.log('filtered searchResults (in Everything)', searchResults);
|
||||||
return searchResults;
|
return searchResults;
|
||||||
|
Loading…
Reference in New Issue
Block a user