簡體   English   中英

如何在Elasticsearch中編寫此復雜查詢

[英]How can I write this complex query in elasticsearch

我如何獲得用Elasticsearch編寫的以下復雜查詢

select username, count(id), 
  concat(YEAR(postedtime),'-',MONTH(postedtime),'-',DAY(postedtime),' ',HOUR(postedtime)) 
from table 
where username in ("user1", "user2", "user3") 
group by username, 
  concat(YEAR(postedtime),'-',MONTH(postedtime),'-',DAY(postedtime),' ',HOUR(postedtime));

以下是一個應該入門的查詢-一些注意事項:

  • 計數是自動返回的,不需要顯式請求。
  • "size":0防止文檔被退回-您只會看到匯總。
  • 返回的日期將為紀元格式。
  • 您可以用terms查詢替換query_string ,甚至可以使用過濾器-這取決於您的要求。
  • 聚合分為2個級別,第一個是用戶名,第二個是日期存儲桶。

編碼:

curl -XGET 'http://localhost:9200/myindex/table/_search?pretty' -d '{
 "size": 0,
 "query":{
    "query_string": { "query":"user1 OR user2 OR user3", "fields": ["username"]}
   },
 "aggs" : {
    "username_agg" : {
      "terms": {"field" : "username"},
      "aggs" : {
          "date_agg": { 
            "date_histogram" : { "field" : "postedtime", "interval" : "hour" } 
          }
       }
    }
  }
}'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM