2014-06-12 71 views
3

我不知道我做錯了什麼。該記錄不會被添加。通過PHP將文檔添加到Apache Solr cURL

這裏是我的代碼:

$ch = curl_init("http://127.0.0.1:8983/solr/collection1/update/json?commit=true"); 

$data = array(
    "add" => array("doc" => array(
     "id" => "HW132", 
     "name" => "Hello World" 
    )) 
); 
$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); 

這裏是我從Solr的得到響應:

{"responseHeader":{"status":0,"QTime":4}} 

回答

6

很顯然,我需要問Apache Solr實現以提交文件。它不會自動提交文檔,也可能我不知道如何配置它來自動提交。以下是工作示例。希望它能幫助那些有同樣問題的人。

$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); 
1

所以不太清楚你用的是什麼版本的Solr,3.X或4.X(但它們在如何處理與提交不同,但將涵蓋)。無論哪種情況,這些都是您可以在solrconfig.xml文件中進行的更改

對於3.x,您可以在多個文檔或毫秒數或兩者中指定自動提交。打門檻後的Solr將提交更改,所以你不必在你的代碼:

<!-- 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> 

對於4.X你也有SoftCommit選項。這使得同步到磁盤之前,可供搜索的變化:

<!-- 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> 

我發現,思考和通在solrconfig.xml中實施這些設置,而不是依賴於應用程序代碼級提交使得一個更可預測的結果。

上Solr的更完整的討論提交,可在這裏找到:

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

1

我想發佈一個XML,但我不知道下面的作品,爲什麼解決方案。該文件說,我應該使用'@'加文件路徑上傳文件,但它沒有任何工作。所以,我沒有這樣:

<?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); 

而且我得到這個確定(200)狀態:

* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 8080 (#0) 
> POST /solr/update/?commit=true HTTP/1.1 
Host: localhost:8080 
Accept: */* 
Content-Type: text/xml 
Content-Length: 3502 
Expect: 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>