在 ElasticSearch 中保存用户搜索查询的最佳方法是什么?

我正在开发使用 ElasticSearch 和 SQL 数据库的应用程序.那么,保存用户搜索查询并显示统计信息(例如最受欢迎的请求是什么)的最佳方法是什么?简单的方法是保存在 SQL 数据库中并计数.但也许 ElasticSearch 中内置了一些技术?

I am developing application that uses ElasticSearch and SQL database. So, what is the best way to save users search queries and show statistics like what are the most popular requests? The simply way is to save in SQL database and count. But maybe there are some technique built in ElasticSearch?

推荐答案

您可以通过在 ES 集群中创建第二个索引来做到这一点.当用户通过您的应用程序提交搜索时,您需要执行两个步骤.

You could do this by creating a second index in your ES cluster. When a user submits a search through your application you perform two steps.

  1. 将搜索作为查询提交给 Elasticsearch,以获得正常的搜索行为.
  2. 使用用户提供的搜索词向集群提交索引请求.

使用已提交的所有搜索词的第二个索引,您可以做许多巧妙的事情.对于您的情况,您可以像在 SQL 中一样拥有一个计数"字段,随着更多人搜索该术语而增加.另一个很好的用例是谷歌风格的推荐条款.您的 UI 可以在每次按键时使用输入的文本提交搜索请求,并使用来自先前搜索的术语的命中填充下拉列表.您甚至可以通过添加用户字段并过滤掉不是来自该特定用户的结果来个性化这一点.

With a second index of all search terms that have been submitted you can do a number of neat things. For your case, you can have a 'count' field just like in SQL that you increment as more people search for that term. Another great use case is a google style recommended terms. Your UI can submit a search request with the entered text on each key press and populate a drop down with hits from the previously searched terms. You can even personalize this by adding a user field and filtering out results not from that particular user.

要记住的是,ElasticSearch 既可以用作主数据存储也可以用作辅助数据存储.我总是建议你只保留你愿意丢失的数据(如搜索历史)作为主要数据.将您的系统关键数据保存在 SQL 等更传统的数据存储中,这样在出现任何问题时都可以轻松备份和恢复!

The thing to keep in mind is that ElasticSearch can be used as both a primary and secondary data store. I always suggest that you only keep data you are willing to lose (like search history) as primary data though. Keep your system critical data in a more traditional data store like SQL, that way it is easy to back up and restore if anything ever goes wrong!

相关文章