2014-07-06 25 views
1

我剛開始使用SOLR。我想索引一些HTML頁面,並從文檔中得到這個:在Java中用於SOLR的捲曲等效POST

curl "http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true" -F "[email protected]/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 \"[email protected]\"" +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(); 
     } 
    } 
} 

我在做什麼錯了:我仍然不能正常工作,現在得到這個?

+0

你是否使用了「在java中發送http post」一詞?它可能會導致你[這個StackOverflow問題](http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java) –

+0

@RayToal看到我的編輯。 – user1759796

+0

當你說「不工作」時,你是什麼意思? - 你有錯誤嗎?或者只是沒有看到預期的結果?有沒有可以提供的日誌?你能調試,看看是否有任何異常拋出?對我們來說,理解沒有具體問題的完整問題是一個挑戰。 –

回答

3

你應該使用SolJ客戶端從Java,這將可能是一個比你要去的HTTP接口更容易訪問的Solr:

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(); 
+0

我試過了,它工作正常,但我將如何替換'document.addField(「id」,「552199」);'從磁盤中的html文件,以便我可以搜索任何發生在它的字符串? – user1759796

+0

在SolrJ中使用ContentStreamUpdateRequest。有關示例,請參閱http://wiki.apache.org/solr/ContentStreamUpdateRequestExample。 – MatsLindh

+0

@fiskfisk可以很好地工作,但是必須將html頁面保存在一個文件中(比如你給出的例子)還是有一種方法來索引包含所有html的字符串? – user1759796