我正在开发一个移动网站,可以搜索MongoDB文章集的标签。
基本上,每个article对象都有一个tags属性,它存储一组标记字符串。 搜索工作正常,但我也想在搜索中添加日志记录。
原因是我希望了解访问者搜索的内容以及他们为优化代码而获得的结果。
例如,如果用户输入标签杂货,那么我想保存查询结果。
希望我的问题很明确。谢谢!
答案 0 :(得分:1)
如果不进行测量,就无法优化。您需要能够将新结果与旧结果进行比较。因此,您必须保存对搜索查询至关重要的所有信息的快照。这显然包括搜索术语本身,但也包括结果的准确快照。
您可以创建整个产品的快照,但仅保存确定搜索结果所涉及的信息可能更有效。在您的情况下,这些是文章标签,但也可能是文章说明,如果您的搜索引擎使用它。
在每次搜索查询之后,您必须构建类似于以下内容的文档,并将其保存在MongoDB中的searchLog
集合中。
{
query: "search terms",
timestamp: new Date(), // time of the search
results: [ // array of articles in the search result
{
articleId: 123, // _id of the original article
name: "Lettuce", // name of the article, for easier analysis
tags: [ "grocery", "lettuce" ] // snapshot of the article tags
// snapshots of other article properties, if relevant
},
{
articleId: 456,
name: "Bananas",
tags: [ "fruit", "banana", "yellow" ]
}
]
}