【发布时间】:2015-03-06 04:03:01
【问题描述】:
TL;DR:这个问题是关于
optimizing the design of a backend search engine:这个想法是返回一个完整的html页面,尽可能快地显示搜索结果。
我认为elasticsearch 和redis 将是这份工作的合适人选,尽管我还没有决定。 服务器为node.js,数据库为mongoDB。
不会使用前端框架,服务器会返回纯html。
我想出了以下服务器端设计*:
(*) 注意:我在服务器端设计方面经验很少,所以我的方法可能很幼稚
第一次搜索会:
- run a server function that makes an elasticSearch query and returns some json;
- create scraps of HTML from the returned json and store them in Redis
(for caching purposes);
- store the search keywords and the keys of the html scraps in Redis.
而后续搜索只会:
- recognize the search keywords, and get the html scraps keys from Redis;
- get the html scraps values;
然后服务器会:
- build the html page from the scraps;
- Return the html page.
让我们通过一个例子进一步阐述:
假设您有相当多的文章集合,例如 100,000 篇,您打算在您的网站上出售这些文章。
All articles are stacked in a MongoDb database 并且有多个键(标题、类别、评论、图片等...)
为实现搜索,文章收藏已indexed in elasticSearch。
第一个 elasticSearch 查询将返回 json containing a list of articles。
使用相同搜索词对 elasticSearch 的后续查询将返回相同的 json,但现在将返回 extract it from the elasticSearch cache。
但如果您不使用前端技术并希望从服务器返回plain html,您仍然需要insert that json into a templator, create an html page, then return to the end user。
-
如果您缓存了
whole html page into a superfast key/value database like Redis,则模板和弹性搜索都不会用于后续搜索:Redis 会识别搜索词,并立即返回 html。 -
或者,如果按照 cmets/answers 中的建议,Redis 被跳过,则
html could be stored into a cdn在模板计算之后。
问题:
请说明建议设计的哪些步骤是不必要/缺失的,以及原因。
感谢您的帮助!
【问题讨论】:
-
您不需要 Redis 进行缓存,因为 EL 会处理所有这些,并且以相同的方式可扩展。 elasticsearch.org/guide/en/elasticsearch/guide/current/…
-
好的。但它不会更慢,因为 ElasticSearch 只存储 json 吗?如果模板程序不获取 html 的碎片,它不会有更多的工作要做吗?
-
你不应该存储整个 HTML,你不需要那个阶段,因为 EL 会在 50 毫秒内响应你的结果。
-
对于热门/趋势静态页面,您可以轻松地在您选择的 Web 服务器之前安装一个 Varnish,它可以承载 10k 操作/秒。
-
请注意脚本编写速度非常快,您可以在一秒钟内进行 40 亿次操作。代码之外的任何东西都有一般的延迟,而且速度有点慢。
标签: mongodb optimization elasticsearch redis