简体   繁体   English

在 nodeJS 中使用多个字段自动完成搜索

[英]Autocomplete search with multiple fields in nodeJS

I'm using nodejs, mongodb and want to build an autocomplete search like Facebook. when typing a keyword, it needs to search groups, users, pages etc.我正在使用 nodejs,mongodb 并希望构建一个自动完成搜索,如 Facebook。当输入关键字时,它需要搜索组、用户、页面等。

For example, I have two collections: users and companies.比如我有两个collections:用户和公司。 They both have a name field.他们都有一个名称字段。 When someone type a keyword in the search bar, I will provide suggestions containing both the users and companies that have the entered text.当有人在搜索栏中输入关键字时,我会提供包含输入文本的用户和公司的建议。 Using stuff like User.find(name:"typed name") and Company.find("name:"typed name"). The part I'm stuck on is how to suggest names from both collections, yet have the results look like it's from a single collection.使用 User.find(name:"typed name") 和 Company.find("name:"typed name") 之类的东西。我坚持的部分是如何从 collections 建议名称,但结果看起来像它来自一个集合。

How can I combine the results from multiple collections into a single result collection?如何将来自多个 collections 的结果合并到一个结果集合中?

You could build a "search" method in your Controllers / Services layer by means of which you execute the searches of each collection (User.find, Company.find, etc) sequentially and independently.您可以在您的控制器/服务层中构建一个“搜索”方法,通过该方法您可以按顺序独立地执行每个集合(User.find、Company.find 等)的搜索。 You must concatenate the results into a new entity that could be named "SuggestionDTO" applying the Data Transfer Object pattern.您必须将结果连接到一个新实体中,该实体可以应用数据传输 Object 模式命名为“SuggestionDTO”。 You would only use this new entity to present / transfer the results.您只会使用这个新实体来呈现/传输结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM