Azure Search - 创建 Index 时报错 index : Found 0 key fields in index ‘XXX‘. 问题解决 】
公司要使用Azure-Ai-Search替代OpenSearch,在用Java SDK创建Index时使用的是。一开始options传的是null。发现报错说index没有key,但是实体类明明有注解。这里的fieldName是null走不到解析注解那一步。解决方案1:类的属性由private改为public。说明注解没有解析到。Debug 查看源码发现。解决方案2:传入options修改可见性。
·
Azure-AI-Search 项目 Java SDK 通过类创建 Index 时无法获取 @SimpleField 和 @SearchableField 注解信息问题解决
报错信息
Caused by: com.azure.core.exception.HttpResponseException: Status code 400, "{"error":{"code":"OperationNotAllowed","message":"The request is invalid. Details: index : Found 0 key fields in index 'XXXXXX'. Each index must have exactly one key field.","details":[{"code":"MissingKeyField","message":"Found 0 key fields in index 'XXXXXX'. Each index must have exactly one key field. Parameters: index"}]}}"
at com.azure.search.documents.implementation.util.Utility.executeRestCallWithExceptionHandling(Utility.java:190) ~[azure-search-documents-11.8.0-beta.4.jar:11.8.0-beta.4]
at com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndexWithResponse(SearchIndexClient.java:754) ~[azure-search-documents-11.8.0-beta.4.jar:11.8.0-beta.4]
at com.azure.search.documents.indexes.SearchIndexClient.createOrUpdateIndex(SearchIndexClient.java:718) ~[azure-search-documents-11.8.0-beta.4.jar:11.8.0-beta.4]
at com.example.demo.service.IndexUpsertService.initSearchIndex(IndexUpsertService.java:56) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMethod.invoke(InitDestroyAnnotationBeanPostProcessor.java:457) ~[spring-beans-6.2.1.jar:6.2.1]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:401) ~[spring-beans-6.2.1.jar:6.2.1]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:219) ~[spring-beans-6.2.1.jar:6.2.1]
... 20 common frames omitted
背景
公司要使用Azure-Ai-Search替代OpenSearch,在用Java SDK创建Index时使用的是
public static List<SearchField> buildSearchFields(Class<?> model, FieldBuilderOptions options) {
return SearchIndexAsyncClient.buildSearchFields(model, options);
}
一开始options传的是null。发现报错说index没有key,但是实体类明明有注解
@SimpleField(isKey = true, isSortable = true)
private String id;
@SearchableField(isFilterable = true)
private String name;
说明注解没有解析到。Debug 查看源码发现
这里会返回 false 导致
这里的fieldName是null走不到解析注解那一步
List<SearchField> searchFields = (List)getDeclaredFieldsAndMethods(currentClass).filter(FieldBuilder::fieldOrMethodIgnored).map((classField) -> buildSearchField(classField, classChain, serializer)).filter(Objects::nonNull).collect(Collectors.toList());
这里的buildSearchField方法:
String fieldName = serializer.convertMemberName(member);
继续进入:
if (!f.isAnnotationPresent(JsonIgnore.class) && visibilityChecker.isFieldVisible(f)) {
isFieldVisible 方法
public boolean isVisible(Member m) {
switch (this) {
case ANY:
return true;
case NONE:
return false;
case NON_PRIVATE:
return !Modifier.isPrivate(m.getModifiers());
case PROTECTED_AND_PUBLIC:
if (Modifier.isProtected(m.getModifiers())) {
return true;
}
case PUBLIC_ONLY:
return Modifier.isPublic(m.getModifiers());
default:
return false;
}
}
默认是 PUBLIC_ONLY 我们的属性是private修饰的 所以返回的是false 导致无法解析到注解
解决方案
解决方案1:类的属性由private改为public
解决方案2:传入options修改可见性
new FieldBuilderOptions().setJsonSerializer(new JacksonJsonSerializerBuilder()
.serializer(new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY))
.build())
Github
issues链接 FIrst snippet for index creation does not work
相关依赖
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-search-documents</artifactId>
<version>11.8.0-beta.4</version>
</dependency>
Vector Search need Azure Ai
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.13</version>
</dependency>
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)