Code Monkey home page Code Monkey logo

textgoose's Introduction

textgoose

This is a simple plugin to enable textsearch in your mongoose schema.

Usage

You can use this plugin just as any other mongoose plugin. For example for a blog post schema you would do something like that.

const mongoose = require('mongoose');
const textgoose = require('textgoose');

const postSchema = new mongoose.Schema({
    title: String,
    text: String,
    author: String,
    created_at: {type: Date, default: Date.now}
});

postSchema.index({title: 'text', text: 'text'});
postSchema.plugin(textgoose, options);

mongoose.model('Item', itemSchema);

Note that you need to mark the text fiels you want to use in search queries as indexes.

Plugin options

Also you can provide plugin with options object. Here's what you can customize

language: string - the language of the queries. Default is en.

Note that you need to mark your index field the same default_language you use in plugin's options like that:

postSchema.index({title: 'text', text: 'text'}, {default_language: 'russian'});

caseSensetive: boolean - clarify if you want your searches to be case sesitive. Default is false.

diacriticSensitive: boolean - clarify if you want your searches to be diacritic sensitive. Default is false.

To use textgoose on your module write use textSearch method like that.

Item.textSearch(query, queryOptions, (err, data) => {
    if (err)
        return console.log(err);

    return res.send(data);
});

Query options

Queries are customizable too. You can use the following options.

filter: object - use this as additional filter for your database documetns. For examle you can filter your posts by author using this filter {author: "John"}. Default is null.

limit: number - defines the maximum documents returned by your database. Default is 100.

scoreSort: boolean - If you want your docs to be sorted by their search score set it to true. Default is false.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.