Quantcast
Channel: Technical Blogs » Solr
Viewing all articles
Browse latest Browse all 3

Apache Solr Server integration with Solr4j – Adding Documents with Multivalued Field

$
0
0



When we want to integrate Apache Solr Server within Java Application, we need to use Solr4j Api.

For Adding the Solr Documents programmatically,

We will code step by step -

Adding the Solr Server in Code -



public static String url = "http://localhost:8983/solr";
...

CommonsHttpSolrServer server = new CommonsHttpSolrServer(url); 
		
...

Adding Single Document to Solr Server -


   SolrInputDocument doc = new SolrInputDocument();
   doc.addField("id","ID Field Value");
   doc.addField("subject","Subject Field Value");
   server.add( doc); 			   
   server.commit();

Adding Documents to Solr Server and commit after 100 Documents add



public static Collection docs = new ArrayList();

....

We can write within loop while increasing counter //pseudo code is here
   {
       SolrInputDocument doc = new SolrInputDocument();
       doc.addField("id","ID Field Value");
       doc.addField("subject","Subject Field Value");
       docs.add(doc);
   }

now commit after some more document adding -


 if((counter%100)==0)
   {
       server.add( doc); 			   
       server.commit();
   }

How to add a mutivalued field

Code is


    List categories = new ArrayList();
    String[] categoriesArray = arrStr[1].split(",");
    for(int l = 0; l < categoriesArray.length;l++)
    {
       categories.add(categoriesArray[l]);
   }
			   
   doc.addField("categories", categories ); // Here categories is a multivalued field

Viewing all articles
Browse latest Browse all 3

Trending Articles