最近在做一个微服务项目,项目中基本用Redis和ES替代了Mysql,Mysql更像是一个备份。但是在开发中我遇到了一个ES查询问题——ES的嵌套查询。项目中存在一对多的关系,使用Mysql只要关联查询就行了,但是改用ES之后一直没能直接实现我的需求,最后还是没有解决我的问题,下面就简单梳理一下。因为最近好几个小伙伴问怎么搭梯子,所以最后会附上一个搭建梯子教程。
还是使用spring boot来整合ES,我根据项目的需求自己简单的创建一个项目,一个一对多的查询。
首先创建ES一个document(和Mysql的model基本一样),代码如下:
@Document(indexName = "team_index",type = "team_type", shards = 2, replicas = 1,createIndex = false)
public class TeamType {
@Id
@Field(type = FieldType.Keyword, store = true)
private String id;
@Field(type = FieldType.Keyword, store = true)
private Long teamId;
@Field(type = FieldType.Nested, store = true)
private List<MemberType> memberTypes;
@Field(type = FieldType.Keyword, store = true)
private String skuCode;
@Field(type = FieldType.Keyword, store = true)
private String ownerId;
@Field(type = FieldType.Keyword, store = true)
private String status;
@Field(type = FieldType.Integer, store = true)
private Integer needAmount;
// 省略部分字段
......
}
// MemberType
public class MemberType {
@Id
@Field(type = FieldType.Keyword, store = true)
private String id;
@Field(type = FieldType.Keyword, store = true)
private String teamId;
@Field(type = FieldType.Keyword, store = true)
private String userId;
@Field(type = FieldType.Boolean, store = true)
private boolean captain;
@Field(type = FieldType.Keyword, store = true)
private String orderId;
// 省略部分字段
......
}
根据代码就可以看出在TeamType内嵌套了一个MemberType列表,这是一个很简单的一对多的关系。现在我的需求是这样的我需要先对TeamType进行一次过滤查询出满足条件的对象,然后还需要对TeamType内部的MemberType也进行一个过滤。
1、创建索引,添加数据
首先我启动ES和kibana,然后通过调用接口方式创建索引并做好映射关系,当然也可以通过直接操作ES完成,不过既然是项目中使用我觉得自己写个接口调用一下更方便一点,首先是创建索引:
public Map<String, Object> addIndex() {
Map<String,Object> resultMap = new HashMap<>();
if (elasticsearchTemplate.indexExists("team_index")) {
elasticsearchTemplate.deleteIndex("team_index");
}
elasticsearchTemplate.createIndex("team_index");
elasticsearchTemplate.putMapping(TeamType.class);
elasticsearchTemplate.refresh(TeamType.class);
resultMap.put("success",true);
resultMap.put("message","创建索引成功");
return resultMap;
}
接下来就是将数据从数据库的数据存放到ES。在数据库中我的Team和Member是分表存放的,二者通过Team的主键进行关联。而在ES中只有TeamType一个类型,所以查询从数据库查询出数据后需要自己拼装一下。因为整个业务很简单,封装这一点也比较简单,先从数据库查询所有Team,然后遍历查询所有的Member即可(或者使用之前说过的Mybatis一对多查询),代码如下:
public void addDataToES() {
List<Team> teamList = teamRepository.findAll();
for(Team team : teamList) {
List<Member> memberList = memberRepository.queryAllByTeamId(team.getId());
List<MemberType> memberTypeList = toTypeList(memberList);
TeamType teamType = new TeamType(team,memberTypeList);
teamTypeRepository.save(teamType);
teamTypeRepository.refresh();
}
}
现在索引建了、数据也存放了,接下来就是查询
2、使用ES查询
首先通过使用kibana来查询,看下ES中的数据是否完整,
GET team_index/_search
{
"query": {
"match_all": {}
}
}
因为数据比较少,所以就贴提下查询的结果:
{
"took": 62,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "team_index",
"_type": "team_type",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"teamId": 2,
"memberTypes": [
{
"id": "5",
"teamId": "2",
"userId": "U22222222",
"captain": false,
"orderId": "O888888888",
"createTime": "2019-07-09T05:18:00.303+0000",
"updateTime": "2019-07-09T05:18:09.365+0000"
},
{
"id": "4",
"teamId": "2",
"userId": "U11111111",
"captain": false,
"orderId": "O999999999",
"createTime": "2019-07-09T05:17:34.585+0000",
"updateTime": "2019-07-09T05:17:46.820+0000"
}
],
"skuCode": "8887545454514",
"ownerId": "U00000001",
"status": "success",
"needAmount": 6,
"createTime": "2019-07-09T05:18:56.112+0000",
"updateTime": "2019-07-09T05:19:16.234+0000"
}
},
{
"_index": "team_index",
"_type": "team_type",
"_id": "1",
"_score": 1,
"_source": {
"id": "1",
"teamId": 1,
"memberTypes": [
{
"id": "3",
"teamId": "1",
"userId": "U77777777",
"captain": true,
"orderId": "O333333333",
"createTime": "2019-07-09T05:17:15.285+0000",
"updateTime": "2019-07-09T05:17:25.145+0000"
},
{
"id": "2",
"teamId": "1",
"userId": "U88888888",
"captain": false,
"orderId": "O222222222",
"createTime": "2019-07-09T05:16:45.362+0000",
"updateTime": "2019-07-09T05:16:57.684+0000"
},
{
"id": "1",
"teamId": "1",
"userId": "U99999999",
"captain": false,
"orderId": "O111111111",
"createTime": "2019-07-09T05:16:21.608+0000",
"updateTime": "2019-07-09T05:16:32.736+0000"
}
],
"skuCode": "9944545412121",
"ownerId": "U55555555",
"status": "success",
"needAmount": 10,
"createTime": "2019-07-09T05:18:27.576+0000",
"updateTime": "2019-07-09T05:18:50.830+0000"
}
}
]
}
}
查询结果出来是2个TeamType,现在需求是这样的:先对TeamType进行一个过滤,然后对其内部嵌套的MemberType再进行一个过滤。以上面的查询结果为例,我要查询"status"等于"success",然后内部的MemberType的"captain"属性为false。也就是说我期望的结果如下:
{
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "team_index",
"_type": "team_type",
"_id": "2",
"_score": 1,
"_source": {
"id": "2",
"teamId": 2,
"memberTypes": [
{
"id": "5",
"teamId": "2",
"userId": "U22222222",
"captain": false,
"orderId": "O888888888",
"createTime": "2019-07-09T05:18:00.303+0000",
"updateTime": "2019-07-09T05:18:09.365+0000"
},
{
"id": "4",
"teamId": "2",
"userId": "U11111111",
"captain": false,
"orderId": "O999999999",
"createTime": "2019-07-09T05:17:34.585+0000",
"updateTime": "2019-07-09T05:17:46.820+0000"
}
],
"skuCode": "8887545454514",
"ownerId": "U00000001",
"status": "success",
"needAmount": 6,
"createTime": "2019-07-09T05:18:56.112+0000",
"updateTime": "2019-07-09T05:19:16.234+0000"
}
},
{
"_index": "team_index",
"_type": "team_type",
"_id": "1",
"_score": 1,
"_source": {
"id": "1",
"teamId": 1,
"memberTypes": [
{
"id": "2",
"teamId": "1",
"userId": "U88888888",
"captain": false,
"orderId": "O222222222",
"createTime": "2019-07-09T05:16:45.362+0000",
"updateTime": "2019-07-09T05:16:57.684+0000"
},
{
"id": "1",
"teamId": "1",
"userId": "U99999999",
"captain": false,
"orderId": "O111111111",
"createTime": "2019-07-09T05:16:21.608+0000",
"updateTime": "2019-07-09T05:16:32.736+0000"
}
],
"skuCode": "9944545412121",
"ownerId": "U55555555",
"status": "success",
"needAmount": 10,
"createTime": "2019-07-09T05:18:27.576+0000",
"updateTime": "2019-07-09T05:18:50.830+0000"
}
}
]
}
}
按照原有想法,先过滤"status"为"success"的,然后内部去过滤"memberTypes.captain"为false。但是实际使用并不是这么回事,我使用嵌套查询尝试了一下,但是没有得到期望的结果,查询如下:
GET team_index/_search
{
"query": {
"bool": {
"must": [
{"match": {
"status": "success"
}},
{
"nested": {
"path": "memberTypes",
"query": {
"match": {
"memberTypes.captain": false
}
}
}
}
]
}
}
}
使用这个查询出来的结果和不使用嵌套查询结果是一样的,当然也可能是因为我数据量少的原因。但是嵌套查询条件确实没有生效,查询结果中依然有"captain"为true的数据。不知道是不是我的思路有问题,如果有了解ES的可以告知一下,网上找了很久也没有找到合适的方法。如果根据结果来推导,会不会是这么一个逻辑,嵌套查询查找"captain"为false的TeamType,也就是说只要TeamType中的memberTypes有一个满足"captain"为false,那么就会将这个TeamType返回,而返回的结果是不过滤内部的MemberType的。为此我又专门添加了一条TeamType数据,并将其内部的MemberType的"captain"全部设置成true,然后再次查询,发现查询的结果依然是全部数据,也就是说嵌套的"captain"为false条件没有生效,所以我的猜想是错的。
目前完全进入了死胡同,感觉这么简单的查询,自己却没有办法解决。最后对查询的结果在代码里面对其进行筛选,不过这个方法感觉有点low。 代码如下:
public Map<String, Object> queryMembers(Integer teamId) {
Map<String,Object> resultMap = new HashMap<>();
NestedQueryBuilder nestedQueryBuilder =
new NestedQueryBuilder("memberTypes",termQuery("memberTypes.captain",false),ScoreMode.None);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(termQuery("teamId",teamId)).must(nestedQueryBuilder);
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices("team_index")
.withTypes("team_type")
.withQuery(boolQueryBuilder)
.build();
List<TeamType> teamTypeList = new ArrayList<>();
Iterable<TeamType> teamTypes = teamTypeRepository.search(searchQuery);
Iterator<TeamType> iterator = teamTypes.iterator();
while (iterator.hasNext()) {
TeamType teamType = iterator.next();
List<MemberType> memberTypeList = teamType.getMemberTypes();
teamType.setMemberTypes(memberTypeList.stream().filter(m -> !m.isCaptain()).collect(Collectors.toList()));
teamTypeList.add(teamType);
}
resultMap.put("result",teamTypeList);
return resultMap;
}
不知道是不是自己使用查询的姿势不正确,但是不管是在java代码还是在kibana里面操作,都无法得到自己想要的结果,最后只能在代码中多结果再次进行一次过滤。官方文档中的说明也不是很清楚,自己看着感觉疑问挺多的。自己网上找了一些嵌套查询的例子,但是都只给查询的DSL,并没有返回相应的结果。
自己也不知道怎么解决了,问了几个同事也都没有解决,如果哪位小伙伴对这个问题比较熟悉,希望指点一下,自己现在也不知道到底问题处在哪里了。
另外有好几个小伙伴问我自建梯子的问题,因为我现在用的GCP服务器在海外(以前在日本,现在我迁到印度了),所以如果自建梯子必须要有海外的服务器,网上有如何薅羊毛的教程,一般都是一年或者送一定金额,具体可以网上百度一下。比如AWS、GCP,但是AWS速度比较慢,GCP的话目前好像国内没法注册了。你也可以自己买一个小型机,一月好像5刀左右吧。我将搭梯子的教程放到github,我以前在简书发过一次AWS的,但是被官方给锁定了,所以就和这次的项目代码放我的github了,有需要的可以自取。公众号如果不能打开github链接,建议到简书查看链接。
网友评论