Search code examples
node.jssolrsolrjsolr4

How to approach Solr schema for the first time


I am trying to understand what is the best way to design my Solr Schema. and if there is a possibility to do it in a less complex way using solrJ.
I am currently working with solr's example server so i can understand how solr works. if i understood correctly so far, the way to define the following schema:

Book= { title: String,
        year: Int } 

Author = { name: String,
           books: [book] } <-- list/array of book objects 

is to use CopyFields:

<fields>
    <field name="name" type="string" indexed="true" stored="true" multiValued="false"/>
    <field name="books" type="string" indexed="true" stored="false" multiValued="false"/>
    <!-- books will contain: --> 
    <field name="title" type="string" indexed="true" stored="true" multiValued="true"/>
    <field name="year" type="int" indexed="true" stored="true"/>
</fields>
<copyField source="title" dest="books"/>
<copyField source="year" dest="books"/>

was i correct? if so, how do i upload a new author to my database? i tried to upload form my node.js server using solr-client:

function ADDONE(){
    var docs = [];
    //generate 4 docs.
    for(var i = 0; i <= 4 ; i++){
       var doc = {
            id : 20 + i , 
            name : "Author"+i ,
            books: [{title: "firstBook" , year: 1900+i} , 
                    {title: "SecondBook" , year: 1901+i} ]

       }
       docs.push(doc);
    }
    // Add documents to Solr
    client.add(docs,function(err,obj){
       if(err){
          console.log(err);
       }else{
          console.log(obj);
       }
    });

}
ADDONE();

But this won't work. what is the correct way to define each document? am i even close? the example i gave was written for node.js solr-client, but i prefer to use Java and it's solr Client (solrJ?).

I would also like to know how a contract a query for books form the years 1900 to 1910.

Thanks.


Solution

  • you can also use the dynamics fields

    <fields>
        <field name="name" type="string" indexed="true" stored="true" multiValued="false"/>
        <field name="books" type="string" indexed="true" stored="false" multiValued="false"/>
        <dynamicField name="books_year_*"  type="string" indexed="true"  stored="true" multiValued="false"/>
        <dynamicField name="books_title_*"  type="string" indexed="true"  stored="true" multiValued="false"/>
        <field name="book_title" type="string" indexed="true" stored="true" multiValued="true"/>
        <copyField source="books_title_*" dest="book_title"/>
        <field name="book_year" type="string" indexed="true" stored="true" multiValued="true"/>
        <copyField source="books_year_*" dest="book_year"/>
    
    </fields>
    

    and with your method,

    books: [{title: "firstBook" , year: 1900+i} => 
    "books_title_"+i : firstBook
    "books_year_"+i : 1900+i
    

    Query:

    select?q=book_year:"1970"