Well,不要刷屏了

es2聚合子查询问题

Elasticsearch | 作者 蓝天白云 | 发布于2015年11月25日 | 阅读数:5359


一、源数据:
curl -XPUT ' http://192.168.0.101:9200/order/static_type/1' -d '{  "orderid": "00001","time":1,"f1":"A" }'
curl -XPUT ' http://192.168.0.101:9200/order/static_type/2' -d '{  "orderid": "00001","time":2,"f2":"B1" }'
curl -XPUT ' http://192.168.0.101:9200/order/static_type/3' -d '{  "orderid": "00001","time":3 ,"f2":"B2","f3":"C1"}'
curl -XPUT ' http://192.168.0.101:9200/order/static_type/4' -d '{  "orderid": "00001","time":4 ,"f3":"C2"}'
curl -XPUT ' http://192.168.0.101:9200/order/static_type/5' -d '{  "orderid": "00001","time":5 ,"f4":"D"}' 
curl -XPUT ' http://192.168.0.101:9200/order/static_type/6' -d '{  "orderid": "00001","time":6 ,"f5":2}'

curl -XPUT ' http://192.168.0.101:9200/order/static_type/7' -d '{  "orderid": "00002","time":1 ,"f1":"E"}'
curl -XPUT ' http://192.168.0.101:9200/order/static_type/8' -d '{  "orderid": "00002","time":3 ,"f1":"A","f2":"B"}'
二、JSON 聚合查询语句:
{
  "aggregations": {
    "orderid": {
      "terms": {
        "field": "orderid"
      },
      "aggregations": {
        "f2s": {
          "terms": {
            "field": "f2"
          }
        },
        "f1s": {
          "terms": {
            "field": "f1"
          }
        },
        "tmax": {
          "max": {
            "field": "time"
          }
        },
        "tmin": {
          "min": {
            "field": "time"
          }
        }
      }
    }
  }
}
三、结果两条记录:
orderID      tmax   tmin      f1s        f2s
000001       6         1           a           b1,b2
000002       3         1           a,e         b
四、想对第三步的聚合结果再做一次查询,能够根据orderid,f1s,f2s 作为查询条件,如果不符合整条不要。
请教 如果我在上述的情况,要刷选f1s=e 直接得到00002 这一行,请问
{
  "aggregations": {
    "orderid": {
      "terms": {
        "field": "orderid"
      },
      "aggregations": {
        "f2s": {
          "terms": {
            "field": "f2"
          }
        },
        "f1s": {
          "terms": {
            "field": "f1"
          }
        },
        "tmax": {
          "max": {
            "field": "time"
          }
        },
        "tmin": {
          "min": {
            "field": "time"
          }
        }
      }
    }
  }
}
 这个的怎么修改?? 这个过滤条件得怎么设置?请指点一下,谢谢。

 
已邀请:

medcl - 今晚打老虎。

赞同来自:

更新一下,楼主自己已解决,赞!
 
{
  "size": 0,
  "aggs": {
    "orderid": {
      "terms": {
        "field": "orderid"
      },
      "aggs": {
        "f1s": {
          "terms": {
            "field": "f1"
          }
        },
        "f2s": {
          "terms": {
            "field": "f2"
          }
        },
        "tmax": {
          "max": {
            "field": "time"
          }
        },
        "tmin": {
          "min": {
            "field": "time"
          }
        },
        "f1count": {
          "filter": {
            "term": {
              "f1": "a"
            }
          },
          "aggs": {
            "count": {
              "value_count": {}
            }
          }
        },
        "f2count": {
          "filter": {
            "term": { 
              "f2": "b1"
            }
          },
          "aggs": {
            "count": {
              "value_count": {}
            }
          }
        },
        "f1s_sel": {
          "bucket_selector": {
            "buckets_path": {
              "f1count": "f1count>count",
              "f2count": "f2count>count",
              "timemax": "tmax",
              "timemin": "tmin"
            },
            "script": "(f1count>0) && (f2count>0) && timemax-timemin>=5 "
          }
        }
      }
    }
  }
}
 

}LOG}Y61`[[}{X`ES(8V9LM.png

 

要回复问题请先登录注册