以正确有效的方式为搜索引擎构建 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,}

我的方法有些不确定,例如:

  • 每个音频的收听次数和购买历史记录都很大,我这样组织数据是否有效?我只是用示例数据进行测试,它似乎工作正常。
  • 每次我将收听时间和购买历史的新记录推送到音频文档中时,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.,您可以在此 link 上找到更多信息。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...