Spring Data ElasticSearch 嵌套对象查询

avatar 2021年12月17日19:13:11 6 3265 views
博主分享免费Java教学视频,B站账号:Java刘哥 ,长期提供技术问题解决、项目定制:本站商品点此

之前的es文件索引需要加一个权限判断,我现在的思路是每个文件的角色和权限放到嵌套对象里

然后查询的时候通过查询嵌套对象来实现

just do it

一、创建索引

http://10.1.5.190:9200/common_file

{
    "settings": {
        "index": {
            "number_of_shards": "1",
            "number_of_replicas": "1"
        },
        "index.max_ngram_diff": 5,
        "analysis": {
            "analyzer": {
                "ngram_analyzer": {
                    "tokenizer": "ngram_tokenizer"
                }
            },
            "tokenizer": {
                "ngram_tokenizer": {
                    "type": "ngram",
                    "min_gram": 1,
                    "max_gram": 5,
                    "token_chars": [
                        "letter",
                        "digit"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "repoPrefix": {
                "type": "keyword"
            },
            "fileName": {
                "type": "text",
                "analyzer": "ngram_analyzer"
            },
            "level": {
                "type": "long"
            },
            "leafFlag": {
                "type": "boolean"
            },
            "filePath": {
            	"type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword"
                    },
                    "text": {
                        "type": "text",
                        "analyzer": "ngram_analyzer"
                    }
                }
            },
            "creatorName": {
                "type": "text"
            },
            "description": {
                "type": "text"
            },
            "updateTime": {
                "type": "date"
            },
            "revision": {
                "type": "long"
            },
            "createTime": {
                "type": "date"
            },
            "fileSize": {
                "type": "long"
            },
            "updaterName": {
                "type": "text"
            },
            "_class": {
                "type": "keyword"
            },
            "id": {
                "type": "long"
            },
            "projectId": {
                "type": "long"
            },
            "fileType": {
                "type": "long"
            },
            "roleAuthList": {
            	"type": "nested",
                "properties": {
                	"roleCode" : {
                		"type": "keyword"
                	},
                	"auth" : {
                		"type": "text"
                	}
                }
            }
        }
    }
}

数据示例

 

二、查询代码

//查询对象
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// level查询,查询>=level+1 (range查询)
if (CommonUtil.isNotEmpty(fileSearchDTO.getLevel()))
{
	boolQueryBuilder.must(QueryBuilders.rangeQuery("level").from(fileSearchDTO.getLevel() + 1));
}
// repoPrefix查询 精确查询 (match查询)
if (CommonUtil.isNotEmpty(fileSearchDTO.getRepoPrefix()))
{
	boolQueryBuilder.must(QueryBuilders.matchQuery("repoPrefix", fileSearchDTO.getRepoPrefix()).operator(Operator.AND));
}
// filePath查询 文件路径模糊查询 (wildcard查询)
if (CommonUtil.isNotEmpty(fileSearchDTO.getFilePath()))
{
	boolQueryBuilder.must(QueryBuilders.wildcardQuery("filePath.keyword", fileSearchDTO.getFilePath() + "*"));
}

// 文件名查询 ES分词查询 (match查询)
if (CommonUtil.isNotEmpty(fileSearchDTO.getSearchKeywords()))
{
	boolQueryBuilder.must(QueryBuilders.matchQuery("fileName", fileSearchDTO.getSearchKeywords()).operator(Operator.AND));
}
// 获得登录用户角色列表
List<String> loginUserRoleCodes = prjUserRoleService.getLoginUserRoleCodeList();

// roleCode里必须包含登录用户的角色编码或为*
// auth里必须为r或rw
List<String> authList = new ArrayList<>();
authList.add("rw");
authList.add("r");
loginUserRoleCodes.add("*");
boolQueryBuilder.must(QueryBuilders.nestedQuery("roleAuthList", QueryBuilders.termsQuery("roleAuthList.roleCode", loginUserRoleCodes), ScoreMode.None));
boolQueryBuilder.must(QueryBuilders.nestedQuery("roleAuthList", QueryBuilders.termsQuery("roleAuthList.auth", authList), ScoreMode.None));


// 排序,文件类型、根据查询精度分数、更新时间倒序排序  (文件夹需要在前面)
PageRequest pageRequest = PageRequest.of(fileSearchDTO.getPage() - 1, fileSearchDTO.getLimit(),
		Sort.Direction.DESC, "fileType", "_score", "updateTime");
Page<EsCommonFile> esCommonFilePage = esCommonFileRepository.search(boolQueryBuilder, pageRequest);

 

 

  • 微信
  • 交流学习,资料分享
  • weinxin
  • 个人淘宝
  • 店铺名:言曌博客咨询部

  • (部分商品未及时上架淘宝)
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0