我有一个包含嵌套文档的文档,我想为 Solr 定义架构。我一直在阅读文档,但我不知道如何使用嵌套文档定义 schema.xml。
当我尝试使用 addBean
索引文档时出现错误,因为我在架构中没有字段 obj1
而且我不知道如何定义它。
我正在使用带有 @Field
注释的 java 对象。
public class ObjToIndex {
@Field
String id;
@Field
String name;
@Field
ObjToIndex2 obj1;
public class ObjToIndex2 {
@Field
String id;
@Field
String lastName;
我不知道如何在模式中定义类型为“object”或类似内容的字段 obj1
。
最佳答案
I don't know how to define in the schema a field obj1 with type "object" or something similar.
你不能(至少不是你想的那样)
Solr 不是这样设计的:信息的单位是由字段组成的文档;字段可以是不同的类型,但简而言之,它们只是原始类型(字符串、数字、 boolean 值),字段不能是复杂对象。看看How Solr Sees the World在文档中。
这是否意味着您无法管理嵌套文档?不。您可以通过一些注意事项
来管理它们如何定义模式
首先,您需要像这样定义内部 _root_
字段:
<field name="_root_" type="string" indexed="true" stored="false" docValues="false" />
然后您需要将父对象和子对象的所有“原始”字段合并到一个单个字段列表中。这有一些在 solr documentation 中也提到的对应物:
- 您必须定义一个 id 字段,该字段对于父对象和子对象都必须存在,并且您必须保证它是全局唯一的
- 只有同时存在于父对象和子对象中的字段才能声明为“必填”
例如,让我们看一个稍微复杂的案例,您可以在博客文章中嵌套多个评论:
public class BlogPost {
@Field
String id;
@Field
String title;
@Field(child = true)
List<Comment> comments;
}
public class Comment {
@Field
String id;
@Field
String content;
}
那么你需要这样的架构:
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="${solr.core.name}" version="1.5">
<types>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.LongPointField" positionIncrementGap="0"/>
<fields>
<field name="_version_" type="long" indexed="true" stored="true" />
<field name="_root_" type="string" indexed="true" stored="false" docValues="false" />
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true" />
<field name="title" type="string" indexed="true" stored="true" multiValued="false" required="false" />
<field name="content" type="string" indexed="true" stored="true" multiValued="false" required="false" />
</fields>
<uniqueKey>id</uniqueKey>
</schema>
如何索引文档
使用 solrj 非常简单:只需在 Java 中创建嵌套对象,库将在添加它们时负责创建正确的请求
final BlogPost myPost = new BlogPost();
myPost.id = "P1";
myPost.title = "My post";
final Comment comment1 = new Comment();
comment1.id = "P1.C1";
comment1.content = "My first comment";
final Comment comment2 = new Comment();
comment2.id = "P1.C2";
comment2.content = "My second comment";
myPost.comments = List.of(comment1, comment2);
...
solrClient.addBean("my_core", myPost);
如何检索文件
这有点棘手:要重建原始对象及其子对象,您必须使用 child doc transformer在你的请求中(query.addField([child]")
):
final SolrQuery query = new SolrQuery("*:*");
query.addField("*");
query.addField("[child]");
try {
final QueryResponse response = solrClient.query("my_core", query);
final List<BlogPost> documents = response.getBeans(BlogPost.class);
关于java - Solr,如何在 schema.xml 中定义嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30863276/