spring-data-elasticsearch 多级嵌套对象查询

数据存储格式:

{
    "_index":"earth",
    "_type":"earth2",
    "_id":"n-3",
    "_version":1,
    "_score":1,
    "_source":{
        "jid":"n-3",
        "udateTime":"2018-01-30 14:10:04",
        "orderItem":[
            {
                "orderType":"RO",
                "orderDate":"20170708",
                "orderItems":[
                    {
                        "skuNo":"154018",
                        "stockSite":"0001",
                        "costOfSale":108.9
                    }
                ]
            }
        ]
    }
}

查询语句:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "orderItem.orderItems",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "orderItem.orderItems.skuNo": "154018"
                    }
                  },
                  {
                    "match": {
                      "orderItem.orderItems.stockSite": "0001"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "orderItem",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "orderItem.orderType": "RO"
                    }
                  },
                  {
                    "match": {
                      "orderItem.orderDate": "20170708"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

转换为spring-data-elasticsearch和java API查询:

		QueryBuilder orderItemsQuery = QueryBuilders.nestedQuery("orderItem.orderItems",
				QueryBuilders.boolQuery()
						.must(QueryBuilders.matchQuery("orderItem.orderItems.stockSite", "0001"))
						.must(QueryBuilders.matchQuery("orderItem.orderItems.skuNo", "154018")),
				ScoreMode.Total);

		QueryBuilder orderQuery = QueryBuilders.nestedQuery("orderItem",
				QueryBuilders.boolQuery()
						.must(QueryBuilders.matchQuery("orderItem.orderType", "RO"))
						.must(QueryBuilders.matchQuery("orderItem.orderDate", "20170708")),
				ScoreMode.Total);

		QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(orderQuery).must(orderItemsQuery);

		Iterable<OrderItem> it = orderRepository.search(queryBuilder);
		List<OrderItem> list = Lists.newArrayList(it);
		System.out.println(list);
		


发布了144 篇原创文章 · 获赞 43 · 访问量 52万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览