Spring data elasticsearch如何为关键字字段创建存储库方法
假设我有这样的映射,并且我想通过“requestId.keyword”字段进行搜索以获取完全匹配的请求。如何在不使用 @Query 注解的情况下使用 Spring Data Elasticsearch 存储库实现它?
"requestId": {
"type": "text",
"analyzer": "1_to_15_analyzer_without_space",
"search_analyzer": "all_symbols_and_fold_analyzer",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
Let's say I have mapping like this, and I want to search by the "requestId.keyword" field to fetch the exact match requests. How can I implement it with the Spring Data Elasticsearch repository without using @Query annotation?
"requestId": {
"type": "text",
"analyzer": "1_to_15_analyzer_without_space",
"search_analyzer": "all_symbols_and_fold_analyzer",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于通过内省方法名称来构建查询的机制来说,这是不可能的。第一个想法是有类似的东西(我在这里使用
Foo
实体):方法名称的分析是在 spring-data-common 模块中完成的,该模块仅使用 Java 的属性名称实体的属性(可能是嵌套的)。但是 keyword 子字段仅存在于 Elasticsearch 中,并且(如果不是自动创建)存在于
@MultiField
注释中。但是解析方法名的代码不使用特定于商店的信息,因此这样的方法将不起作用并失败,并出现keyword
不是text
的属性的错误- 这对于 Java 对象来说是正确的。您可以做的是首先添加一个自定义存储库片段接口:
并提供一个实现,该实现的命名必须类似于以 Impl 作为后缀的接口:
您注入了一个
ElasticsearchOperations
并使用它来执行您构建的查询。我采用了两种方法来构建查询,两种方法都有效。您要使用的存储库定义将是:
This is not possible with the mechanism to build queries by introspecting the method name. The first idea is to have something like (I am using a
Foo
entity here):The analysis of the method name is done in the spring-data-common module which only uses the property names of the Java properties of an entity (might be nested). But the keyword subfield only exists in Elasticsearch and - if not autocreated - in the
@MultiField
annotation. But the code to parse the methodname does not use store-specific information and so an approach like this will not work and fail with the error thatkeyword
is not a property oftext
- which is right for the Java object.What you can do is to first add a custom repository fragment interface:
and provide an implementation that must be named like the interface with Impl as suffix:
You have an
ElasticsearchOperations
injected and use that to execute a query that you build. I have put in two ways to build the query, both work.Your repository definition to use would then be: