【问题标题】:Curl equivalent POST in java for SOLR用于 SOLR 的 java 中的 curl 等效 POST
【发布时间】:2014-07-06 00:58:14
【问题描述】:

我刚开始使用 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();
        }
    }
}

我做错了什么?

【问题讨论】:

  • 你用谷歌搜索过“用java发送http帖子”这个短语吗?它可能会将您带到this StackOverflow question
  • @RayToal 查看我的编辑。
  • 你说“不工作”是什么意思 - 你有错误吗?或者只是没有看到预期的结果?有没有可以提供的日志?能不能调试一下看看有没有抛出异常?在没有细节的情况下理解整个问题对我们来说是一个挑战。
  • 嗯,没有特别的错误,我得到一个 200 响应,但文件没有被索引。如果我查询 html 文件中出现的字符串,我不会得到任何结果。

标签: java http post curl solr


【解决方案1】:

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

SolrJ 是一种 API,可让 Java 应用程序轻松与之通信 索尔。 SolrJ 隐藏了很多连接到 Solr 和 允许您的应用程序与 Solr 进行简单的高级交互 方法。

SolrJ 的中心是 org.apache.solr.client.solrj 包,它 仅包含五个主要类。首先创建一个 SolrServer,它 表示您要使用的 Solr 实例。然后发送 SolrRequests 或 SolrQuerys 并取回 SolrResponses。

SolrServer 是抽象的,因此要连接到远程 Solr 实例, 您实际上将创建一个 HttpSolrServer 实例,它知道如何 使用 HTTP 与 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();

【讨论】:

  • 我试过了,它工作正常,但是我如何用磁盘中的 html 文件替换 document.addField("id", "552199"); 以便我可以搜索其中出现的任何字符串?
  • 在 SolrJ 中使用 ContentStreamUpdateRequest。有关示例,请参见 wiki.apache.org/solr/ContentStreamUpdateRequestExample
  • @fiskfisk 效果很好,但必须将 html 页面保存在文件中(如您提供的示例中),或者有没有办法索引包含所有 html 的字符串?
  • 即使我添加了jar文件solr-solrj-6.4.1.jar并添加了对我的pom.xml的依赖,我仍然无法添加import org.apache.solr.client.solrj.SolrServer;,也无法使用setParser()函数.有什么原因吗?
猜你喜欢
  • 1970-01-01
  • 2011-01-10
  • 2010-09-12
  • 1970-01-01
  • 2015-05-25
  • 2018-03-18
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多