elasticsearch - Elasticsearch - 如何在两种类型之间连接数据?

标签 elasticsearch

我正在尝试为我正在从事的项目探索 Elasticsearch ,但坚持如何加入两种类型的文档。

例如,如果我有 10 个文件是酒店可用性价格,还有 10 个文件都是飞往酒店所在目的地的航类。

通常在 MySQL 中,我会根据日期、酒店和航类的持续时间等进行连接。

我将如何返回一个包含 10 个可用航类中最便宜航类的酒店文件?

最佳答案

我能想到做你想做的最接近的事情是Composite Aggregations .它不是真正的连接,而是 可以 让你接近你想要的。

规定:

  • 索引之间的字段必须具有相同的名称
  • 您将不得不展平生成的聚​​合
  • 所有结果字段(您关心的)都将是某种聚合

  • 这是一个最小的示例(在 Kibana 控制台中被破解):

    使用文档:
    POST my-test1/_doc/_bulk
    {"index": {}}
    {"entityID":"entity1", "value": 12}
    {"index": {}}
    {"entityID":"entity1", "value": 22}
    {"index": {}}
    {"entityID":"entity2", "value": 2}
    {"index": {}}
    {"entityID":"entity2", "value": 12}
    
    
    POST my-test2/_doc/_bulk
    {"index": {}}
    {"entityID":"entity1", "otherValue": 5}
    {"index": {}}
    {"entityID":"entity1", "otherValue": 1}
    {"index": {}}
    {"entityID":"entity2", "otherValue": 3}
    {"index": {}}
    {"entityID":"entity2", "otherValue": 7}
    

    我们将围绕公共(public)实体字段 entityID 进行聚合
    GET my-test*/_search
    {
      "size": 0,
      "aggs": {
        "by-entity": {
          "composite": {
            "sources": [
              {
                "entityID": {
                  "terms": {
                    "field": "entityID.keyword"
                  }
                }
              }
            ]
          },
          "aggs": {
            "value": {
              "avg": {
                "field": "value"
              }
            },
            "otherValue": {
              "avg": {
                "field": "otherValue"
              }
            }
          }
        }
      }
    }
    

    这将导致响应:
    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 10,
        "successful" : 10,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 8,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "by-entity" : {
          "after_key" : {
            "entityID" : "entity2"
          },
          "buckets" : [
            {
              "key" : {
                "entityID" : "entity1"
              },
              "doc_count" : 4,
              "otherValue" : {
                "value" : 3.0
              },
              "value" : {
                "value" : 17.0
              }
            },
            {
              "key" : {
                "entityID" : "entity2"
              },
              "doc_count" : 4,
              "otherValue" : {
                "value" : 5.0
              },
              "value" : {
                "value" : 7.0
              }
            }
          ]
        }
      }
    }
    

    您可以围绕许多不同的字段和不同的存储桶聚合创建复合聚合。因此,您可以创建一个 terms为您的 hotel_id 聚合并将其与 date_histogram 结合使用围绕您的timestamp .

    关于elasticsearch - Elasticsearch - 如何在两种类型之间连接数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075886/

    相关文章:

    php - Elasticsearch的Logstash couchdb_changes插件

    现有 JHipster 项目中的 Elasticsearch

    pandas - 如何将pandas数据导出到elasticsearch?

    elasticsearch - ElasticSearch-搜索范围内的任何嵌套字段

    elasticsearch - 在空白分析器上搜索词组前缀后,Elasticsearch反复崩溃

    Docker启动sonarqube :7. 5-community fails with es log permission

    java - 使用java api的Elasticsearch多条件查询

    elasticsearch - 如何告诉ElasticSearch创建嵌套字段

    elasticsearch - 使用批量API将批处理上传到Elasticsearch存储中

    ruby-on-rails - 升级到Elasticsearch 7时重新索引错误