Add document to Apache Solr via PHP cURL Add document to Apache Solr via PHP cURL curl curl

Add document to Apache Solr via PHP cURL


Apparently, I need to ask Apache Solr to commit the document. It does not auto commit the document or maybe I don't know how to config for it to auto commit. The following is the working example. Hope it will help to those who has the same problem.

$ch = curl_init("http://127.0.0.1:8983/solr/collection1/update?wt=json");$data = array(    "add" => array(         "doc" => array(            "id"   => "HW2212",            "title" => "Hello World 2"        ),        "commitWithin" => 1000,    ),);$data_string = json_encode($data);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);curl_setopt($ch, CURLOPT_POST, TRUE);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);$response = curl_exec($ch);


So not clear what version of Solr you are using, 3.X or 4.X (they differ in how they deal with commits but will cover both). In either case these are changes you can make in the solrconfig.xml file

For 3.x you can specify Autocommit in either number of documents or number of milliseconds or both. After hitting the threshold Solr will commit your changes so you don't have to in your code:

 <!-- autocommit pending docs if certain criteria are met.  Future versions may expand the available     criteria -->    <autoCommit>      <maxDocs>10000</maxDocs> <!-- maximum uncommited docs before autocommit triggered -->      <maxTime>15000</maxTime> <!-- maximum time (in MS) after adding a doc before an autocommit is triggered -->      <openSearcher>false</openSearcher> <!-- SOLR 4.0.  Optionally don't open a searcher on hard commit.  This is useful to minimize the size of transaction logs that keep track of uncommitted updates. -->    </autoCommit>

For 4.X you also have the SoftCommit option. It makes the changes available to searches prior to syncing to disk:

 <!-- SoftAutoCommit         Perform a 'soft' commit automatically under certain conditions.         This commit avoids ensuring that data is synched to disk.         maxDocs - Maximum number of documents to add since the last                   soft commit before automaticly triggering a new soft commit.         maxTime - Maximum amount of time in ms that is allowed to pass                   since a document was added before automaticly                   triggering a new soft commit.      -->     <autoSoftCommit>       <maxTime>1000</maxTime>     </autoSoftCommit>

I've found that thinking thru and implementing these settings in solrconfig.xml instead of depending upon application code level commits makes for a more predictable outcome.

A more complete discussion on Solr commits can be found here:

http://wiki.apache.org/solr/SolrConfigXml#Update_Handler_Sectionhttp://searchhub.org/2013/08/23/understanding-transaction-logs-softcommit-and-commit-in-sorlcloud/


I'm trying to post an xml but I don't know why the solution below works. The documentation says that I should use the '@' plus filepath to upload file, but it does no work. So I did in this way:

<?php$url = 'http://localhost:8080/solr/update/?commit=true';$file = realpath('/home/fabio/target_file.xml');$header = array(    "Content-Type: text/xml",);$post = file_get_contents($file);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HTTPHEADER, $header);curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);curl_setopt($ch, CURLOPT_POST, TRUE);curl_setopt($ch, CURLOPT_POSTFIELDS, $post);curl_setopt($ch, CURLOPT_VERBOSE, TRUE); echo curl_exec($ch);curl_close($ch);

And I got this OK (200) status:

*   Trying 127.0.0.1...* Connected to localhost (127.0.0.1) port 8080 (#0)> POST /solr/update/?commit=true HTTP/1.1Host: localhost:8080Accept: */*Content-Type: text/xmlContent-Length: 3502Expect: 100-continue< HTTP/1.1 100 Continue* We are completely uploaded and fine< HTTP/1.1 200 OK< Server: Apache-Coyote/1.1< Content-Type: application/xml;charset=UTF-8< Transfer-Encoding: chunked< Date: Tue, 19 Jan 2016 16:52:19 GMT< * Connection #0 to host localhost left intact<?xml version="1.0" encoding="UTF-8"?><response><lst name="responseHeader"><int name="status">0</int><int name="QTime">269</int></lst></response>