Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuzzy search same model, but different fields #38

Open
pinguluk opened this issue Mar 10, 2020 · 6 comments
Open

Fuzzy search same model, but different fields #38

pinguluk opened this issue Mar 10, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@pinguluk
Copy link

pinguluk commented Mar 10, 2020

Hi.

I have and model named Article that contains the tilte and content .

let mongoose = require('mongoose');
let Schema = mongoose.Schema;  
var mongoose_fuzzy_searching = require('mongoose-fuzzy-searching');

var ArticleSchema = new Schema({
    title: {
        type: String,
    },
   content: {
        type: String,
    },
})

module.exports = mongoose.model('Article', ArticleSchema);

The model is exported and used in another .js.
How can I fuzzy search the title and content separately, without declaring

ArticleSchema.plugin(mongoose_fuzzy_searching, {fields: ['title', 'content']});

in the model schema file?

I want to have 2 functions that search articles one by the title, and the other one by the content, separately.

If I put the

ArticleSchema.plugin(mongoose_fuzzy_searching, {fields: ['title', 'content']});

then my both functions will use title and content for the fuzzy search.

Can I achieve something like this?

function a() {
    ArticleSchema.plugin(mongoose_fuzzy_searching, {fields: ['title']});
    fuzzySearch();
}
function b() {
    ArticleSchema.plugin(mongoose_fuzzy_searching, {fields: ['content']});
    fuzzySearch();
}
@VassilisPallas
Copy link
Owner

Hi, this is not possible, but even if it was, I wasn't suggested you to use fuzzy search in big strings. Even better if you set as searchable column only the title, the results will be more accurate.

@VassilisPallas
Copy link
Owner

But this is a very nice idea though, I will add it on the backlog.

@VassilisPallas VassilisPallas added the enhancement New feature or request label Mar 11, 2020
@evgeniy-skakun
Copy link

@VassilisPallas did you have time to do this?

@evgeniy-skakun
Copy link

try this https://github.com/evgeniy-skakun/mongoose-fuzzy-searching, there is a search for specific fields

User.fuzzySearch({ query: 'jo', prefixOnly: true, fields: ['firstName'] })
  .then(console.log)
  .catch(console.error);

@gnanaprakash
Copy link

gnanaprakash commented May 15, 2021

Does the fuzzysearch() look into all the fuzzy fields for matching input string, in my case even through both firstName, lastName mentioned as fuzzy fields the search occur only on the firstName, but for lastName only the extract match works

// schema
const UserSchema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
age: Number,
})

UserSchema.plugin(mongoose_fuzzy_searching, { fields: ['firstName', 'lastName'] })
User = mongoose.model('User', UserSchema)

// insert
await insert({ firstName: 'Joe', lastName: 'Doe', email: 'joe.doe@mail.com', age: 30 })

// search
const users = await User.fuzzySearch({ query: 'do', prefixOnly: true, fields: ['lastName'] })

output: []

const users = await User.fuzzySearch({ query: 'doe', prefixOnly: true, fields: ['lastName'] })

output: [{ _id: 609f6e9d7fd01b5b260b24ef,
firstName: 'Joe',
lastName: 'Doe',
email: 'joe.doe@mail.com',
age: 30,
__v: 0,
confidenceScore: 3.3000000000000003 }]

@19007361
Copy link

19007361 commented Feb 5, 2022

so if I have
const Post = PostSchema.plugin(mongoose_fuzzy_searching, {fields: ['start', 'end']})

and then, without fuzzy search, I would search like this:
Post.find({ start: "abc", end: "def, })

With this package, there will not be a way to fuzzy search different strings for each of the fields "start" and "end"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants