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 it = orderRepository.search(queryBuilder);
		List list = Lists.newArrayList(it);
		System.out.println(list);
		


你可能感兴趣的:(ElasticSearch)