我的Oracle数据库中有这个SQL查询和EVENT表。
SELECT *
FROM (SELECT
CITY,
max(DATE) AS eventdate
FROM EVENT
WHERE TYPE = 'CRASH'
GROUP BY CITY
ORDER BY eventdate DESC, CITY ASC)
WHERE ROWNUM < 6;
NEW YORK 15/02/27
LONDON 15/02/27
LONDON 15/02/27
LONDON 15/02/11
LONDON 15/02/19
EVENT: ID, NAME, DATE, CITY, TYPE
我想使用JavaAPI在ElasticSearch中执行相同的查询。 可能吗?我是初学者,不知道如何开始。 documentation没有任何我的案例,或者我看不到它们。
我必须知道有最新活动和日期的6个城市。 然后我会向ElasticSearch询问这些城市的3个最后事件。我想我必须做两个查询,因为在一个响应中不可能有这样的结果:CITY-&gt; listOf3LastEvents,CITY2-&gt; listOf3LastEvents,City3-&gt; listOf3LastEvents
答案 0 :(得分:1)
好的,让我们说你的活动文件只是这样建模:
{
"id": 123,
"name": "Some event name",
"event_date": "2016-09-02T12:00:00.000Z",
"city": "New York",
"type": "CRASH"
}
现在让我们创建一个索引,其中包含上述文档的合理映射:
PUT events
{
"mappings": {
"event": {
"properties": {
"id": {
"type": "long"
},
"type": {
"type": "string",
"index": "not_analyzed"
},
"event_date": {
"type": "date"
},
"city": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
"name": {
"type": "string"
}
}
}
}
}
我们现在可以使用以下命令索引上述文档(+其他几个):
PUT events/event/123
{
"id": 123,
"name": "Some event name",
"event_date": "2016-09-02T12:00:00.000Z",
"city": "New York",
"type": "CRASH"
}
最后,您将能够发送一个与您的SQL查询等效的查询:
POST events/event/_search
{
"size": 0,
"query": {
"term": {
"type": "CRASH"
}
},
"aggs": {
"cities": {
"terms": {
"field": "city.raw",
"size": 6,
"order": {
"latest": "desc"
}
},
"aggs": {
"latest": {
"max": {
"field": "event_date"
}
},
"last_3_events": {
"top_hits": {
"size": 3,
"sort": {
"event_date": "desc"
}
}
}
}
}
}
}