The requested URL was not found on this server. 不管你信不信,反正我是没找到

elasticsearch 能不能做交集查询

Elasticsearch | 作者 aoliao_paopao | 发布于2018年01月24日 | 阅读数:12369

类似于这样的求交集
select user_id from myindex where date >="2018-01-01 00:00:00" and date < "2018-01-01 00:01:00" and user_id in(select  user_id from myindex  where  date >="2018-01-01 00:01:00" and  date < "2018-01-01 00:02:00")
 
用elasticsearch 该怎么写呢
已邀请:

shiyuan

赞同来自:

{
"size": 0,
"query": {
"bool": {
"should": [
{
"range": {
"date": {
"gte": "2018-01-01",
"lt": "2018-01-02"
}
}
},
{
"range": {
"date": {
"gte": "2018-01-02",
"lt": "2018-01-03"
}
}
}
]
}
},
"aggs": {
"intersect": {
"scripted_metric": {
"init_script": "params._agg.a=new HashSet();params._agg.b=new HashSet()",
"map_script": "params._agg[(doc['date'].value>=1514851200000L?'b':'a')].add(doc['user_id'].value)",
"reduce_script": "def a=new HashSet(),b=new HashSet();params._aggs.forEach(item->{a.addAll(item.a);b.addAll(item.b);});a.retainAll(b);return a"
}
}
}
}
date范围:["2018-01-01", "2018-01-02") OR ["2018-01-02", "2018-01-03")

lengchanguo - 得一爸

赞同来自:

我试了这个json,报了一个错:
"type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "lbs_20190410",
        "node": "Uj-ZStATT9y66mIBIHbpKA",
        "reason": {
          "type": "i_o_exception",
          "reason": "can not write type [class java.util.HashSet]"
        }
      }
    ]
 
您遇到过吗?
 
文档上写只支持primitive types,String,Map,Array四种类型,不是这样吗

要回复问题请先登录注册