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

Unable to use text indices in queries #286

Open
smirea opened this issue Apr 21, 2016 · 1 comment
Open

Unable to use text indices in queries #286

smirea opened this issue Apr 21, 2016 · 1 comment

Comments

@smirea
Copy link
Contributor

smirea commented Apr 21, 2016

Mongodb supports a text index

You use it in mongoose by enabling an extra key on the find() operation:

const Product = mongoose.model('Product', new Schema({
    name: String,
}))
Product.schema.index({name: 'text'})

Product.find({
        $text: { $search: 'deli' },
    }, {
        score: { $meta: 'textScore' },
    })
    .sort({score: { $meta: 'textScore' }})        

In the above query the score property is only exposed by passing the 2nd argument to Product.find() which assigns $meta: 'textScore' to score, making it available to sort by

You can't currently search against a text search using this plugin as you can't pass extra options to find by any means

If you attempt to emulate this and do a query: /Product?query={"$text":{"$search":"deli"}}&sort={"score":{"$meta":"textScore"}}

It throws an error (Can't canonicalize query: BadValue must have $meta projection for all $meta sort keys) which seems to be a direct result of the missing option

Is there any plan to support this functionality?

@Zertz
Copy link
Collaborator

Zertz commented Apr 25, 2016

What I've done for full-text search is implement a separate endpoint such as /search/v1. That said, I agree that it would be incredibly convenient to be able to do it directly from the API!

This is actually a problem I am tackling at the moment as our current method is high maintenance since we have to keep queries in sync across two different modules. I'm thinking of something along those lines,

restify.serve(router, connection.model('Model'), {
  textSearch: '/search/v1',
  preSearch: (req, res, next) => {
    // Authorize user, normalize data, etc.
    next()
  },
  postSearch: (req, res, next) => {
    next()
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants