curl通过c++请求索引solr文档

curl request through c++ for indexing solr document

本文关键字:solr 文档 索引 请求 通过 c++ curl      更新时间:2023-10-16

我在solr 中使用了以下代码为文档编制索引

CURL *curl = curl_easy_init(); 
CURLcode res;
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
    data. */
    curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.164:8983/solr/collection1/update?replacefields=false  -H 'Content-type: application/json' -d '[{"id":"4000", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'");
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
     if(CURLE_OK == res){
        logger.LogError("res value CURLE_OK");
    }
    /* always cleanup */
    curl_easy_cleanup(curl);
}

curl_easy_perform(curl)的返回值,即res是CURLE_OK,但该记录在solr的集合1中没有索引,并且当从终端记录发布以下命令时,正在获取索引

curl http://192.168.0.164:8983/solr/collection1/update?replacefields=false  -H 'Content-type: application/json' -d '[{"id":"4000", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'

您没有为cURL提供正确的URL。实际URL -H 'Content-type: application/json' -d '[{"id":"4000", "to":"Life is to.", "cc":"unknown ", "subject":"Life"}]'之后的所有内容都只是cURL命令行工具的参数,而不是URL的一部分。

URL选项应仅为http://192.168.0.164:8983/solr/collection1/update?replacefields=false。其余的参数必须设置为它们自己的curl_easy_setopt调用。

要将数据设置为POST,请使用CURLOPT_POSTFIELDS。

传递一个char*作为参数,指向要在HTTPPOST操作中发送的完整数据。您必须确保数据的格式符合您希望服务器接收数据的方式

要设置正确的请求内容类型,请使用CURLOPT_HTTPHEADER。

将指向HTTP标头链接列表的指针传递给HTTP请求中的服务器和/或代理。主机和代理请求都可以使用相同的列表!

设置完所有选项后,调用curl_easy_perform。您可能还想查看Solr服务器上的日志,看看Solr是否生成了异常。您还可以设置CURLOPT_ERRORBUFFER和CURLOPT_VERBOSE,以获取有关cURL内部任何故障的更多信息(只要cURL能够发出请求,就会返回CURLE_OK,但如果服务器返回400404或服务器端的任何实际错误代码,则不会更改(设置了CURLOPT_FAILONERROR时除外)。