Skip to content

A module in 100% Typescript used to provide efficient client side searching. It uses weight based KeyWord Search to search across a given object

Notifications You must be signed in to change notification settings

SparshaSaha/Highly-Flexible-Weight-Based-Keyword-Search

Repository files navigation

Weight Based Keyword Search

A module in 100% typescript used to provide efficient client side searching. It uses weight based KeyWord Search to search across a given object

How it works

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

Search Parameter Formation and Algorithm Description

Search parameter structure:

class SearchParameter {
 public parameterName: string;
 public parameterWeight: number;
 
 constructor(parameterName: string, parameterWeight: number) {
    this.parameterName = parameterName;
    this.parameterWeight = parameterWeight;
  }
}
ParameterName : Takes in the name of the object parameter to search upon
ParameterWeight : Takes in weight for the particular parameter

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

Usage

1> Import the KeywordSearch file ( Example: import { KeyWordSearch} from "./KeyWordSearch"; )
2> Create a search parameter array
3> Create a KeyWordSearch<T> object and pass in a regular expression (used to separate words). This is optional. If nothing is passed, it assumes a default RegularExpression : /[\s,+-._]+/ . Pass the name of the class you are conducting your search upon in place of T. For more details look in the test.ts file
4> Call getSearchResults(objectsToSearchIn: T[], searchParameters: SearchParameter[], query: string)
5> The getSearchResults() function returns the array of the filtered objects sorted on the descending order of weights

About

A module in 100% Typescript used to provide efficient client side searching. It uses weight based KeyWord Search to search across a given object

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published