elasticsearch 父子关联查询

elasticsearch 父子关联查询,支持父+子数据按条件过滤

es 查询语句

{
  "query": {
    "bool": {
      "should": [
        {
          "has_parent": {
            "type": "device",
            "query": {
              "match": {
                "user_id": "8"
              }
            }
          }
        },
        {
          "match": {
            "alarm_type": "1"
          }
        }
      ]
    }
  }
}



java代码:

//group、sum、count 查询返回参数封装
SearchRequestBuilder sbuilder = ElasticSearchDao.getTransportClient().prepareSearch("trackerdata_01").setTypes("alarm");

//拼装请求条件
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.should(QueryBuilders.hasParentQuery("device", QueryBuilders.matchQuery("user_id","8")));
queryBuilder.should(QueryBuilders.matchQuery("alarm_type","1"));

   SearchResponse actionGet = sbuilder
    .setQuery(queryBuilder)
             .setScroll(new TimeValue(10000))
             .setFrom(startRow)  
             .setSize(pageSize)
             .execute().actionGet();
   SearchHits hits = actionGet.getHits();
   List data = new ArrayList();
   for (SearchHit hit : hits.getHits())
   {
    System.out.println(hit.getSource().toString());
   }

你可能感兴趣的:(技术)