elasticsearch - 搜索引擎正确有效的 Elasticsearch 结构
问题描述
我正在为我的音频商店建立一个搜索引擎。
我只对音频文档使用 1 个索引,结构如下:
{
id: { type: 'integer' },
title: { type: 'search_as_you_type' },
description: { type: 'text' },
createdAt: { type: 'date' },
updatedAt: { type: 'date' },
datePublished: { type: 'date' },
duration: { type: 'float' },
categories: {
type: 'nested',
properties: {
id: { type: 'integer' },
name: { type: 'text' }
},
}
}
按发布日期的顺序通过文本搜索音频文档很简单。但是我想通过基于特定范围内的音频收听时间和购买历史的趋势来进行文本搜索和排序,例如:过去 3 个月或过去 30 天的文本搜索趋势音频,所以我调整结构如下:
{
...previousProperties,
listenTimes: {
type: 'nested',
properties: {
timestamp: { type: 'date' },
progress: { type: 'float' }, // value 0-1.
},
},
purchaseHistories: {
type: 'nested',
properties: {
timestamp: { type: 'date' }
},
},
}
这是我获取过去 3 个月的热门音频的查询,它奏效了:
{
bool: {
should: [
{
nested: {
path: 'listenTimes',
query: {
function_score: {
query: {
range: {
'listenTimes.timestamp': {
gte: $range,
},
},
},
functions: [
{
field_value_factor: {
field: 'listenTimes.progress',
missing: 0,
},
},
],
boost_mode: 'replace',
},
},
score_mode: 'sum',
},
},
{
nested: {
path: 'purchaseHistories',
query: {
function_score: {
query: {
range: {
'purchaseHistories.timestamp': {
gte: 'now+1d-3M/d',
},
},
},
boost: 1.5,
},
},
score_mode: 'sum',
},
},
],
},
}
我对我的方法有一些不确定性,例如:
- 每个音频的收听次数和购买历史记录都很大,如果我这样构建数据是否有效?我只是用样本数据进行测试,它似乎工作正常。
- 每次我将收听时间和购买历史的新记录推送到音频文档时,Elasticsearch 是否会重新索引整个文档?
我是 Elasticsearch 的新手,所以有人可以就这个案例给我一些建议,非常感谢!
解决方案
第一个问题是一个很好的问题,它取决于你将如何实现它,你将不得不注意原子动作,因为我猜,你计划获取监听次数,然后保存增量值。如果您在一个线程中从一个应用程序执行此操作并且它设法及时处理它,那么您很好,但您无法扩展。我会说 elasticsearch 并不是真正为这种交易而设计的。我脑海中闪现的第一个想法是将数字保存到 SQL 数据库中并按某个时间表更新 elasticsearch。我想这些结果不必实时更新?
关于第二个问题,我将发布来自 elasticsearch 文档的引用,您可以在此链接The document must still be reindexed, but using update removes some network roundtrips and reduces chances of version conflicts between the GET and the index operation.
上找到更多信息。
推荐阅读
- amazon-web-services - 缓存亚马逊 ec2 元属性是否安全
- excel - 使用带有循环的自动过滤器
- android - Chrome 72 更新后 Android Cordova 应用程序渲染缓慢
- mongodb - 使用 CDK 在 Fargate 上运行两个容器
- r - 如何在ggplot2中的单个方面注释文本(包含下标)
- python-3.x - 如何使用 python-vlc 获取艺术家和标题信息?
- python-3.x - Waitforsingleobject 正在降低我的程序性能
- javascript - 如何让歌曲暂停并使用相同的按钮播放(按钮更改时)
- python - 你如何将 cookiejar 与 urllib3 一起使用?
- azure-data-factory-2 - 如何以编程方式跟踪错误消息/