1
我该如何在elasticsearch中编写这个mysql查询?如何在elasticsearch中编写此查询?
SELECT *,count(products.store_id)
FROM stores
INNER JOIN products
ON stores.store_id=products.store_id
group by stores.store_id;
我该如何在elasticsearch中编写这个mysql查询?如何在elasticsearch中编写此查询?
SELECT *,count(products.store_id)
FROM stores
INNER JOIN products
ON stores.store_id=products.store_id
group by stores.store_id;
Elasticsearch是不完全是一个数据库,它是一个搜索引擎,因此它支持非常有限JOIN操作(parent-child queries)。
如果你想执行上面的查询,那么你将不得不重做模式并尝试将数据放在一个索引中(即使它不在2NF/3NF中也没关系)。也许你可以将store_id与每个产品文档一起编入索引。
现在,回到查询,如果你想解决上面的查询让我们说一个索引,那么你可以使用TERMS
aggregation做到这一点。它会给你按商店编号分组的产品计数,请求将如下所示:
$ curl -XPOST 'http://localhost:9200/products/_search?search_type=count' -d '{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"product_type" : "sometype"}}
]
}
}
}
},
"aggs" : {
"products" : {
"terms" : {
"field" : "store_id"
}
}
}
}'