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

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

问题描述

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

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 个 JSON 在 Logstash 中以 2 种不同类型处理(输入),因此它们的索引在 Elasticsearch 中的不同类型"中可用.

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

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

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 不同,因为 Elasticsearch 不会对属于每种类型的文档进行笛卡尔积.

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天全站免登陆