elasticsearch ElasticSearch中的连接查询
有没有办法(查询)在ElasticSearch中加入下面的2个JSON
elasticsearch ElasticSearch中的连接查询,
elasticsearch,logstash,kibana,
elasticsearch,Logstash,Kibana,有没有办法(查询)在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字段中的两
{
product_id: "1111",
price: "23.56",
stock: "100"
}
{
product_id: "1111",
category: "iPhone case",
manufacturer: "Belkin"
}
以上2个JSON在Logstash中的2个不同类型下处理(输入),因此它们的索引在Elasticsearch中的不同“类型”字段中可用
我想加入product_id字段中的两个JSON。这取决于你说加入时的意图。Elasticsearch不像常规数据库那样支持表之间的连接。它是一个文本搜索引擎,管理索引中的文档 另一方面,您可以在同一索引中使用每个类型通用的字段搜索多个类型 例如,以您的数据为例,我可以创建一个包含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的索引,它有两种类型: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将搜索该索引中的所有文档,而不管其类型如何。这仍然不同于联接,因为Elasticsearch不会对属于每种类型的文档进行笛卡尔积
希望这能对艾萨克有所帮助。哈桑的回答很有效,但我想补充几点,这对我解决这种情况有帮助: 当我试图解决一个类似的问题时,我登陆了这个页面,因为我必须根据另一个索引的文档排除一个索引的多个记录。缺乏关系是非结构化数据库的主要缺点之一 上的elasticsearch文档页解释了很多 在Elasticsearch中,有四种常用技术用于管理关系数据:
- 应用程序端连接
- 数据非规范化
- 嵌套对象
- 父/子关系
例如,您可能会发现要列出所有价格高于x的产品,或者列出所有不再有库存的产品。若要处理此类情况,请使用上述解决方案之一。若要在Elasticsearch上执行连接,请查看Siren“Federate”插件。它通过扩展Elasticsearch本机查询语法来添加连接功能
有人能就此回复我吗?非常感谢您的回复,您能告诉我在这种情况下最好的解决方案是什么吗。如上所述,我有一个索引'product',两种类型'type1'和'type2',两种类型中都有一个公共字段'product_id'。现在,对于我来说,在相同的“product_id”情况下,在Elasticsearch的新索引或类型中合并和索引JSON的可能选项是什么。仅供参考,我正在使用ELK stack(Elasticsearch、Logstash和Kibana)。我不确定我是否了解您试图实现的目标,您的最终目标是什么?最后你想在Kibana中显示什么?你的答案肯定有用。事实上@isaac.hazan我的最终目标是在Kibana的单一事件下显示“产品id”、“价格”、“库存”、“类别”和“制造商”。要提升你的答案,我需要15个声誉,但我只有大约三天的股份溢出:(关于你的最终目标,我认为这不可能成为一个内置功能。我在另一个项目中处理过同样的问题,我克服这一问题的方法是在顶部开发一个层,将两个事件合并,并将它们作为单个事件保存到elasticsearch中。也就是说,在Kibana中,如果你只查询索引w/o您将获得整个数据集的类型,这在技术上意味着您将能够显示一些控制,但我同意这并不理想,例如对于直方图,因为您将显示2个事件而不是1个事件。