我试过这个,但它不起作用让我知道下面的代码有什么问题:
static const char *postthis="[{\"id\":\"4000\", \"to\":\"Life is to.\", \"cc\":\"unknown cc\", \"subject\":\"Life is subject\"}]";
static const char *postthis1= "http://192.168.0.164:8983/solr/collection1/update?wt=json&commit=true ";
static const char *postthis2= "'Content-type: application/json'";
struct curl_slist *list = NULL;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,postthis1 );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
list = curl_slist_append(list, "-H");
list = curl_slist_append(list, postthis2);
list = curl_slist_append(list, "-d");
/* if we don't provide POSTFIELDSIZE, libcurl will strlen() by
itself */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK){
logger.LogError("curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
使用上面的代码我在终端窗口得到响应:
{"responseHeader":{"status":0,"QTime":9}}
这是上述请求的回应。
在solr log中我得到了以下内容:
INFO - 2016-05-19 11:22:10.778; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; start commit{,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false}
INFO - 2016-05-19 11:22:10.779; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; No uncommitted changes. Skipping IW.commit.
INFO - 2016-05-19 11:22:10.781; [ x:collection1] org.apache.solr.core.SolrCore; SolrIndexSearcher has not changed - not re-opening: org.apache.solr.search.SolrIndexSearcher
INFO - 2016-05-19 11:22:10.783; [ x:collection1] org.apache.solr.update.DirectUpdateHandler2; end_commit_flush
INFO - 2016-05-19 11:22:10.784; [ x:collection1] org.apache.solr.update.processor.LogUpdateProcessor; [collection1] webapp=/solr path=/update params={commit=true&[{"id":"4001", "from":"Life is to", "to":"unknown cc", "subject":"Life is subject"}]=&wt=json} {commit=} 0 8
实际curl命令是:
curl http://192.168.0.164:8983/solr/collection1/update?commit=true -H "Content-Type: application/json" -d '[{"id":"450", "to":"Life is .", "cc":"unknown", "subject":"Life"}]'
它可以正常编制索引。