在ElasticSearch中加入查询 [英] Join query in ElasticSearch

查看:135
本文介绍了在ElasticSearch中加入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(查询)在ElasticSearch中加入2个JSONs

Is there any way (query) to join 2 JSONs below in ElasticSearch

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

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

以上2在Logstash中,2个不同类型的JSON被处理(输入),所以它们的索引可以在Elasticsearch中提交的不同的类型中。

Above 2 JSONs processed (input) under 2 different types in Logstash, so their indexes are available in different 'type' filed in Elasticsearch.

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

What I want is to join 2 JSONs on product_id field.

推荐答案

这取决于你说JOIN时你打算什么。 Elasticsearch不像支持表之间的JOIN的常规数据库。它是一个文本搜索引擎,用于管理索引中的文档。

It depends what you intend when you say JOIN. Elasticsearch is not like regular database that supports JOIN between tables. It is a text search engine that manages documents within indexes.

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

On the other hand you can search within the same index over multiple types using a fields that are common to every type.

例如,使用您的数据,我可以创建一个具有2种类型的数据,其数据如下所示:

For example taking your data I can create an index with 2 types and their data like follows:

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。
现在我可以执行以下查询,它将返回两个文档:

I effectively created one index called product with 2 type type1 and type2. Now I can do the following query and it will return both documents:

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的区别在于弹性搜索不会对属于每种类型的文档进行笛卡尔积分。

The reason is because Elasticsearch will search over all documents within that index regardless of their type. This is still different than a JOIN in the sense Elasticsearch is not going to do a Cartesian product of the documents that belong to each type.

希望有帮助

这篇关于在ElasticSearch中加入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆