8

有什么方法(查询)可以在 ElasticSearch 中加入下面的 2 个 JSON

{
product_id: "1111",
price: "23.56",
stock: "100"
}

{
product_id: "1111",
category: "iPhone case",
manufacturer: "Belkin"
}

以上 2 个 JSON 在 Logstash 中以 2 种不同类型处理(输入),因此它们的索引在 Elasticsearch 中的不同“类型”字段中可用。

我想要的是在 product_id 字段上加入 2 个 JSON。

4

3 回答 3

21

这取决于您说 JOIN 时的意图。Elasticsearch 不像常规数据库那样支持表之间的 JOIN。它是一个文本搜索引擎,用于管理索引中的文档。

另一方面,您可以使用每种类型共有的字段在同一索引中搜索多种类型。

例如,获取您的数据,我可以创建具有 2 种类型的索引,它们的数据如下:

curl -XPOST localhost:9200/product -d '{
    "settings" : {
        "number_of_shards" : 5
    }
}'

curl -XPOST localhost:9200/product/type1/_mapping -d '{
        "type1" : {
            "properties" : {
                "product_id" : { "type" : "string" },
                "price" : { "type" : "integer" },
                "stock" : { "type" : "integer" }
            }
        }   
}'              

curl -XPOST localhost:9200/product/type2/_mapping -d '{
        "type2" : {
            "properties" : {
                "product_id" : { "type" : "string" },
                "category" : { "type" : "string" },
                "manufacturer" : { "type" : "string" }
            }
        }
}'  

curl -XPOST localhost:9200/product/type1/1 -d '{
        product_id: "1111", 
        price: "23",
        stock: "100"
}'

curl -XPOST localhost:9200/product/type2/1 -d '{
        product_id: "1111",
        category: "iPhone case",
        manufacturer: "Belkin"
}'

我有效地创建了一个名为 product 的索引,其中包含 2 个类型 type1 和 type2。现在我可以执行以下查询,它将返回两个文档:

curl -XGET 'http://localhost:9200/product/_search?pretty=1' -d '{
    "query": {
        "query_string" : {
            "query" : "product_id:1111"
        }
    }
}'

{
  "took" : 95,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5945348,
    "hits" : [ {
      "_index" : "product",
      "_type" : "type1",
      "_id" : "1",
      "_score" : 0.5945348, "_source" : {
    product_id: "1111",
    price: "23",
    stock: "100"
}
    }, {
      "_index" : "product",
      "_type" : "type2",
      "_id" : "1",
      "_score" : 0.5945348, "_source" : {
    product_id: "1111",
    category: "iPhone case",
    manufacturer: "Belkin"
}
    } ]
  }
}

原因是 Elasticsearch 将搜索该索引中的所有文档,而不管它们的类型如何。这仍然与 JOIN 不同,因为 Elasticsearch 不会对属于每种类型的文档进行笛卡尔积。

希望有帮助

于 2014-03-25T11:52:16.263 回答
6

isaac.hazan 的回答效果很好,但我想补充几点帮助我解决这种情况:

当我试图解决一个类似的问题时,我登陆了这个页面,因为我必须根据另一个索引的文档排除一个索引的多个记录。缺乏关系是非结构化数据库的主要缺点之一。

处理关系上的 elasticsearch 文档页面解释了很多。

在 Elasticsearch 中使用四种常用技术来管理关系数据:

  • 应用程序端连接
  • 数据非规范化
  • 嵌套对象
  • 父子关系

通常,最终的解决方案需要混合使用其中一些技术。

我主要使用嵌套对象和应用程序端连接。虽然使用相同的字段名称可以暂时解决问题,但我认为最好重新考虑并为您的应用程序创建最适合的映射。

例如,您可能会发现要列出价格大于 x 的所有产品,或者列出所有不再有库存的产品。为了处理这种情况,如果您使用上述解决方案之一,它会有所帮助。

于 2016-03-30T09:43:27.867 回答
0

要在 Elasticsearch 上执行连接,请查看 Siren “Federate”插件。它通过扩展 Elasticsearch 原生查询语法来添加连接功能。

https://siren.io/federate/

于 2020-07-29T11:01:06.550 回答