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

Fulltext search breaks in mongodb when filters are enabled. #4065

Closed
funduck opened this issue Feb 23, 2023 · 0 comments · Fixed by #4066
Closed

Fulltext search breaks in mongodb when filters are enabled. #4065

funduck opened this issue Feb 23, 2023 · 0 comments · Fixed by #4066
Labels
bug Something isn't working

Comments

@funduck
Copy link
Contributor

funduck commented Feb 23, 2023

  • With mongodb driver
  • if query contains $fulltext
  • and EntityManager applies filters

then error is thrown.

Stack trace

Error: Full text search is only supported on the top level of the query object.',
        '    at MongoDriver.renameFields (/app/node_modules/@mikro-orm/mongodb/MongoDriver.js:118:19)',
        '    at MongoDriver.find (/app/node_modules/@mikro-orm/mongodb/MongoDriver.js:23:22)',
        '    at MongoEntityManager.find (/app/node_modules/@mikro-orm/core/EntityManager.js:112:41)',
        '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',

To Reproduce
A simple test case

const god = new Author('God', 'hello@heaven.god');
const bible = new Book('Bible', god);
await orm.em.persistAndFlush(bible);
orm.em.clear();

const booksRepository = orm.em.getRepository(Book);
orm.em.addFilter('BreakFulltext', {title: {$re: '.*'}}, Book, true)
const books = await booksRepository.find({ $fulltext: 'Bible' })

Expected behavior
It should be possible to use full text search and use filters with Mongo driver.
Working example with Postgre:

const god = new Author2('God', 'hello@heaven.god');
const bible = new Book2('Bible', god);
await orm.em.persistAndFlush(bible);
orm.em.clear();

const booksRepository = orm.em.getRepository(Book2);
orm.em.addFilter('BreakFulltext', {title: {$re: '.*'}}, Book2, true);
const books = await booksRepository.find({ title: {$fulltext: 'Bible'} });

I propose following:

  • if query has $and in top level and $fulltext in any of its elements, move it to top level
  • if there are several $fulltext in $and or top level already has one throw an error
@B4nan B4nan added the bug Something isn't working label Feb 25, 2023
B4nan added a commit that referenced this issue Feb 26, 2023
Closes #4065

---------

Co-authored-by: Martin Adámek <banan23@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants