I have a document with a nested document and I want to define the schema to Solr. I have been reading the documentation but I don't know how to define the schema.xml with nested documents.
When I try to index a document with addBean
I get an error because I don't have in the schema the field obj1
and I don't know how to define it.
I'm using java object with @Field
annotations.
public class ObjToIndex {
@Field
String id;
@Field
String name;
@Field
ObjToIndex2 obj1;
public class ObjToIndex2 {
@Field
String id;
@Field
String lastName;
I don't know how to define in the schema a field obj1
with type "object" or something similar.
You can't (at least not in the way you think it)
Solr is not designed in that way: the unit of information is a document that is composed by fields; fields may be of different types, but, in short, they are only primitive types (strings, numbers, booleans), fields cannot be complex objects. Take a look at How Solr Sees the World in the documentation.
Does it mean you can't manage nested documents? No. You can manage them with some caveats
How to define the schema
First of all you need to define the internal
_root_
field like this:Then you need to merge all "primitive" fields of your parent and children objects in a single list of fields. This has some counterparts that are also mentioned in the solr documentation:
For example let's see a slightly more complex case where you can nest multiple comments to blog posts:
Then you need a schema like this:
How to index documents
Using solrj it is pretty straightforward: simply create your nested objects in Java and the library will take care of creating the correct request when adding them
How to retrieve documents
This is a little bit tricky: to rebuild the original object and its children you have to use the child doc transformer in your request (
query.addField("[child]")
):