2.0.2 • Published 3 years ago

@webfaster.com/search v2.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

research

A generic, yet robust mongoose, regex based, paginating and ranked search package.

how to use it

The package exports three things:

  • init (function)
  • search (function)
  • saveResult (function)
  • Queries (mongoose model)

init

This function initializes the search engine by connecting to the mongodb. It expects one parameter:

  • options: an object specifying init options The options object:
options: {
  mongoURI: 'mongodb://...:27017', // required!
}

search

A function 'search' is exported, this function expects 3 parameters:

  • model: The model you want to search on.
  • query: The string you're searching for.
  • options: An object to specify extra options.

The options object and its default values:

options: {
  pageSize: 25,
  page: 0,
  queryField: 'nname', // the name field normalized
  queryFields: [], // an array of fields to query on
  queryObject: {}, // a custom prebuilt query we will append to
  select: {}, // the fields you want to select (used in a mongo select)
  verbose: false, // enables debug loggin output
  score: undefined, // custom scoring function 
  fuzzy: false, // enable or disable fuzzy matching
}

! Make sure the field you're searching on is normalized, and contains no special characters, like 'é'.

The function will return an object containing the results, and pagination info.

{
  page: {
    size, // the size of the current page
    prev, // the previous page (undefined if no prev)
    curr, // the current page
    next, // the next page (undefined if no next)
    last, // the last page
    count,// the total count of matching results
  },
  results,// the results sorted based on their score
}

custom sort

You can pass a custom sorting function that will be used to score and sort the results

This function recieves the following arguments:

  • query: the queryString used in the search
  • result: the result of the search function to score
  • queryField: the field we searched on

It should always return a number.

Take a look at the default sorting function for inspiration:

const score = (query, result, queryField) => {
  let score = 1;
  if (result.count) score *= result.count;
  score *= query.length / result[queryField || 'nname'].length;
  return score;
};

saveResult

Another function 'saveResult' is exported. It is used to save what results have been clicked for a given query to score and sort the results.

This function expects 3 parameters:

  • query: the querystring used for the search.
  • result: the result object returned by the search function
  • meta: (optional) an object containing extra info (the user for example)

! We want the result object returned by the search function, because it has its mongo type attached to it.

Queries

We also export the model used to save queries and their results, this way you can do custom queries on it. It has the option 'strict' set to 'false', so this allows you to even set any extra fields on it you may want.

The Queries model file looks like this:

const mongoose = require('mongoose');

const { ObjectId } = mongoose.SchemaTypes;

const Schema = new mongoose.Schema({

  query: { type: String, unique: true },
  results: [{
    count: Number,// how many times this result was clicked
    ref: String,  // the model type of the search result  
    _id: ObjectId,// the id of the search result
  }],
  meta: Object,   // to store the user for example
}, {
  timestamps: true, // creates createdAt and updatedAt
  strict: false,
});


const Model = mongoose.model('queries', Schema);
module.exports = Model;

Example usage:

A very basic example that queries the db for a person with the name 'John'.

const engine = require('search');
const Person = require('./models/Person'); // a mongoose model

engine.init({ mongoURI: 'mongodb://xxxxx:27017' })

const people = await engine.searc(Model, 'john');

console.log(people);
2.0.2

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

2.0.1

3 years ago

1.0.24

3 years ago

2.0.0

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago