在ElasticSearch中加入查询

Faw*_*wad 5 elasticsearch logstash kibana

有没有办法(查询)在ElasticSearch中加入以下2个JSON

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

{
product_id: "1111",
category: "iPhone case",
manufacturer: "Belkin"
}
Run Code Online (Sandbox Code Playgroud)

在Logstash中,在2种不同类型下处理(输入)2个JSON以上,因此它们的索引在Elasticsearch中提供了不同的"类型".

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

isa*_*zan 15

这取决于你说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"
}'
Run Code Online (Sandbox Code Playgroud)

我有效地创建了一个名为product的索引,类型为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"
}
    } ]
  }
}
Run Code Online (Sandbox Code Playgroud)

原因是因为Elasticsearch将搜索该索引中的所有文档,而不管其类型如何.在某种意义上,这仍然不同于JOIN,Elasticsearch不会对属于每种类型的文档执行笛卡尔积.

希望有所帮助


yst*_*ark 6

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

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

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

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

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

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

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

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