N1QL与ElasticSearch Join

时间:2015-07-30 18:29:40

标签: elasticsearch n1ql

我的文件是:

Iwp::1::Porcentaje::Period::1
{
  "id": null,
  "period": 1,
  "type": "IwpCumulative",
  "category": "Porcentaje",
  "sumEarn": 0,
  "sumActual": 0.2248520710059172,
  "sumForecast": 0,
  "sumPlanned": 0,
  "sumValue": 0,
  "parent": "Iwp::1"
}

Iwp::1
{
  "name": "Iwp 1",
  "description": "Iwp 1 Description",
  "manyPeriods": 50,
  "type": "Iwp",
  "countCC": 0,
  "costCode": [
    "CostCode::3",
    "CostCode::4"
  ],
  "iwpCumulatives": [
    "Iwp::1::Porcentaje::Period::1",
     .......
    "Iwp::1::Porcentaje::Period::50",
    "Iwp::1::Qty::Period::1",
........
    "Iwp::1::Qty::Period::50",
  ]
}

如何在ElasticSearch上执行此查询?

N1QL:

select 
t.category,
t.period,
sum(t.sumActual) 
from 
default as q 
inner join default as p on keys q.parent 
inner join default as t on keys p.iwpCumulatives 
where
q.type = 'IwpCumulative' 
and q.period = 50
and q.sumActual > 0 
and q.category = 'Porcentaje' 
group by t.category,t.period
order by t.period,t.category;

我在ElasticSearch有这个查询:

{
    "query":{
    "filtered":{
           "query":{
               "bool":{
                    "must":[
                        {"term":{"period":"5"}},
                        {"term":{"type":"iwpcumulative"}},
                        {"range":{"sumActual":{"gt":"0"}}},
                        {"term":{"category":"porcentaje"}}
                        ]
                }
            }
    }
    }
}

和此:

{
   "size":0,
   "aggs":{
      "group_by_state":{
         "terms":{
            "field":"category"
         },
         "aggs":{
            "costars":{
               "terms":{
                  "field":"period"
               },
               "aggs":{
                  "Suma":{
                     "sum":{
                        "field":"earn"
                     }
                  }
               }
            }
         }
      }
   }
}

现在,我需要将第一个结果与他们的Id一起使用,以便在第二次查询时使用。

提前致谢。

1 个答案:

答案 0 :(得分:1)

因为你只使用一个表导致选择术语(半连接),你可以使用siren-join插件进行elasticsearch: 看看这个:

SIREn Plugin to add relational join capabilities to Elasticsearch