gpt4 book ai didi

java - 在 Java 中为 SOLR curl 等效的 POST

转载 作者:可可西里 更新时间:2023-11-01 17:02:16 24 4
gpt4 key购买 nike

我刚开始使用 SOLR。我想索引一些 html 页面并从文档中获取:

curl "http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true" -F "myfile=@/home/binaryplease/workspace/SOLRTest/HTMLPages/hello2.html"

当查询返回预期结果时,它按预期工作。

我如何在 Java 应用程序中执行这个精确的 POST?

我试过这个,因为我不知道如何使用 HttpClient 来做,但它不起作用:

String command = "curl \"http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true\" -F \"myfile=@\"" +f.getAbsoluteFile() + "\"";

try {
proc = Runtime.getRuntime().exec(command );

InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();

System.out.println("Inputstream " + getStringFromInputStream(in));
System.out.println("Errorstream " + getStringFromInputStream(err));

} catch (IOException e) {
e.printStackTrace();
}

在 SOLR 中索引 html 文件并使用 java 进行查询的正确方法是什么?我将不胜感激。

编辑:我现在得到了这个,但仍然无法正常工作:

    HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("myfile", "@/home/binaryplease/workspace/SOLRTest/HTMLPages/hello3.html"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
InputStream instream = entity.getContent();
try {
System.out.println("Content " + getStringFromInputStream(instream));

} finally {
instream.close();
}
}
}

我做错了什么?

最佳答案

您应该使用 SolJ 客户端从 Java 访问 Solr,这对您来说可能比使用 HTTP 接口(interface)容易得多:

SolrJ is an API that makes it easy for Java applications to talk to Solr. SolrJ hides a lot of the details of connecting to Solr and allows your application to interact with Solr with simple high-level methods.

The center of SolrJ is the org.apache.solr.client.solrj package, which contains just five main classes. Begin by creating a SolrServer, which represents the Solr instance you want to use. Then send SolrRequests or SolrQuerys and get back SolrResponses.

SolrServer is abstract, so to connect to a remote Solr instance, you'll actually create an instance of HttpSolrServer, which knows how to use HTTP to talk to Solr.

https://cwiki.apache.org/confluence/display/solr/Using+SolrJ

设置非常简单:

String urlString = "http://localhost:8983/solr";
SolrServer solr = new HttpSolrServer(urlString);

查询也是如此:

SolrQuery parameters = new SolrQuery();
parameters.set("q", mQueryString);

QueryResponse response = solr.query(parameters);

SolrDocumentList list = response.getResults();

与索引相同:

String urlString = "http://localhost:8983/solr";
SolrServer solr = new HttpSolrServer(urlString);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "552199");
document.addField("name", "Gouda cheese wheel");
document.addField("price", "49.99");
UpdateResponse response = solr.add(document);

// Remember to commit your changes!

solr.commit();

关于java - 在 Java 中为 SOLR curl 等效的 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com