繁体   English   中英

用于SOLR的java中的卷曲等效POST

[英]Curl equivalent POST in java for SOLR

我刚开始使用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接口更容易:

SolrJ是一个API,使Java应用程序可以轻松地与Solr交谈。 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM