A module in 100% typescript used to provide efficient client side searching. It uses weight based KeyWord Search to search across a given object
KeyWord Search takes in a query, an array of objects and a list of search paramters. (Look in examples section for a better understanding)
The array may contain objects of any type and it even supports nested object searching. (Look in examples for a better understanding)
It tokenizes the query as well as the object parameters into lowercase strings on the basis of a regular expression, either supplied or taken by default.
It then inserts the query words into a map and loops through the tokenized words for an object parameter and finds the number of matches. the number of matches are multiplied by the parameter weight to find out the weight for that object.
This step is done for all objects and then the objects are sorted in the descending order of calculated weights.
So basically, the results with the highest weights are the results which are nearest to the query
class SearchParameter {
public parameterName: string;
public parameterWeight: number;
constructor(parameterName: string, parameterWeight: number) {
this.parameterName = parameterName;
this.parameterWeight = parameterWeight;
}
}
Example: We have a class named CustomObjects with the following structure :
class CustomObjects {
name : string,
age : number,
address : string,
friends : string[]
}
So if we want to search in name and address we do in the following ways:
let searchParam1 = new SearchParameter('name', 10);
let searchParam2 = new SearchParameter('address', 5);
let searchParameters = [searchParam1, searchParam2];
PS. the numbers passed inside the SearchParameter objects are basically weights, the parameter that has higher weight is assigned a higher priority.
lets consider two objects to understand how it works : -
object1 = {
name : "Robert Pattinson",
age : 25,
address : "25 xyz street, robert lane"
friends : ["friend1", "friend2"]
}
object1 = {
name : "Robert Clive",
age : 29,
address : "30 robert street,robert lane"
friends : ["friend1", "friend2"]
}
Now lets consider that the query is "Robert Pattinson"
For object 1 :
number of keyword matches in 'name' = 2
number of keyword matches in 'address' = 1
Thus weight assigned to object 1 = 2x(weight for 'name') + 1x(weight for 'address') = 2x10 + 1x5 = 25
For object 2 :
number of keyword matches in 'name' = 1
number of keyword matches in 'address' = 2
Thus weight assigned to object 1 = 1x(weight for 'name') + 2x(weight for 'address') = 1x10 + 2x5 = 20
Thus object1 is given higher priority than object2