0

i am trying to index documents with solrj, this is my code,

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.*;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;

public class index {

public static void main(String[] args) throws IOException, SolrServerException {
String url = "http://localhost:8080/solr/document/";
HttpSolrServer server = new HttpSolrServer( url );
server.setMaxRetries(1); // defaults to 0.  > 1 not recommended.
server.setConnectionTimeout(5000);
server.setSoTimeout(1000);  // socket read timeout
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false);  // defaults to false 
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", 23);
doc1.addField( "title", "doc1" );
doc1.addField( "author","Chetan Bhagat" );
doc1.addField( "contents", "I am the best." );
doc1.addField( "date_modified", "12-12-2014" );
server.commit();    
}
}

After running the console on eclipse shows this:

Dec 21, 2013 2:07:25 AM org.apache.solr.client.solrj.impl.HttpClientUtil createClient INFO: Creating new http client, config:maxConnections=128&maxConnectionsPerHost=32&followRedirects=false

What causes this abnormal termination?? I am new to solr.

user987339
  • 10,519
  • 8
  • 40
  • 45
user2737359
  • 61
  • 1
  • 9
  • What do you mean by "abnormal termination"? Are you not being able to store the document in Solr? You should give us more logging message details, the log you are providing is only showing the "default" solr configuration message... look at the "output" log of Solr. In a first look, it looks like it is not showing the `maxConnections` you have specified, but probably the default one that is specified in the "solrconfig.xml". – emgsilva Dec 21 '13 at 14:15
  • i check as with query: q=id:23, response is : numfound=0, Statictics is: Last Modified:16 minutes ago Num Docs:14 Max Doc:16 Deleted Docs:2 Version:495 Segment Count:5 Also for q=*:* , numfound=14 – user2737359 Dec 21 '13 at 14:54
  • I have just noticed you are missing the "adding" the document to the Solr index... Check my answer bellow. – emgsilva Dec 22 '13 at 10:13

2 Answers2

1

You seem to be missing adding the the "document" to the "server":

...
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", 23);
doc1.addField( "title", "doc1" );
doc1.addField( "author","Chetan Bhagat" );
doc1.addField( "contents", "I am the best." );
doc1.addField( "date_modified", "12-12-2014" );
server.add(doc1); // **MISSING LINE!!**
server.commit();
...

This is probably the reason why you are not seeing the document in the Solr index. This is an example of "adding document" to Solr: http://www.solrtutorial.com/solrj-tutorial.html

HTH.

emgsilva
  • 3,047
  • 1
  • 17
  • 16
  • I did add the statement later on,but still the doc is not visible when searched. I found that in the update handler stats, whenever i run the java program,the num of commits and cumulative_adds increase by 1,so does that mean that doc is indexed,but not searchable? – user2737359 Dec 22 '13 at 13:04
0

The URL http://localhost:8080/solr/document/ does not look correct, where did you get that from? There is not default handler for document?, unless someone customized your solrconfig.xml. To add documents your url needs to be something like : "http://localhost:8080/solr". Look here for more on how to use SolrJ http://wiki.apache.org/solr/Solrj

Update: add your doc1 to server object.

Arun
  • 1,777
  • 10
  • 11
  • i have created a core named: document,do solr/document/ represents that core. – user2737359 Dec 21 '13 at 17:29
  • Ok , did you check if your "document" collection is functional. go to http://localhost:8080/solr/#/document and see you can see the stats , then go to http://localhost:8080/solr/#/document/documents and see if you can post a test document. – Arun Dec 21 '13 at 17:36
  • sorry,but i did not get what do i have to check?? – user2737359 Dec 21 '13 at 19:10
  • I found that in the update handler stats, whenever i run the java program,the num of commits and cumulative_adds increase by 1,so does that mean that doc is indexed,but not searchable? – user2737359 Dec 22 '13 at 13:05
  • Can you copy paste your fields from schema.xml – Arun Dec 22 '13 at 15:04
  • stored="false" /> – user2737359 Dec 22 '13 at 15:26
  • You should be able to query based on your field definition. Check what you get for this query: http://localhost:8080/solr/document/select?q=*%3A*&wt=xml&indent=true – Arun Dec 22 '13 at 15:47
  • Great , you should accept answers so that you can encourage people for their time. – Arun Dec 22 '13 at 20:53