简体   繁体   中英

REST api in node js for full text search using elastic search

I am developing REST API in node js using mongodb as database. I have many models in my database schema such as Province, Attractions, Food, Festivals, etc. In the front end, I have a search box that performs search from all these models. I have stored my data in mongo atlas online. So, I want to use elastic search to build whole search api for my application from all models.

I found that mongodb full text search is not efficient. And also mongoosastic automatically indexes for elastic search but it only does for single model where I need to search from all models. I did not get step by step approach to build rest api in node js using elastic search. I am newbie and I am stuck with this full search from all models in mongo db.

For an out-of-the-box solution, you could try https://github.com/yougov/mongo-connector - this would require you to run a replica set instead of a single database, however.

If you wanted more control, then you could consider adding code in your database layer which (synchronously or asynchronously) updates ElasticSearch indexes with an object once you successfully store it in MongoDB. This would have to handle failures/retries when updating the ES index as well.


Lastly...if you need to search from all models, perhaps you could should give some more thought to how you are modeling your data / indexing it in ElasticSearch.

For example, if I want my users to be able to search for a name which could be any of: [Province, Attraction, Food, Festival], then I might create an index which only contains those "name" fields + the unique IDs which point to the objects they represent. Perhaps a multi_match query could be helpful for that.

Going further (and still assuming you only need to search the "name" field), you could consider storing only the name/ID in the ElasticSearch index, and then retrieve the full object from Mongo via its ID. A lot less to index that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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