2012-07-20 30 views
1

我在TomCat-Servlet上設置Solr服務器。在我的索引中,大約有610.000個文檔包含多個字段。我的schema.xml:solrj xml在文件中輸出

<field name="ID" type="myText" indexed="true" stored="true" required="true" /> 
<field name="text" type="myText" indexed="true" stored="false" multiValued="true" /> 
<dynamicField name="AT*" type="text_general" indexed="true" stored="true" multiValued="true" /> 

要搜索與給定字符串索引(可能是從其他系統)我創建了一個小JavaProgram

class SolrjTest 
{ 
public static void main(String[] args) throws IOException 
{ 
SolrjTest solrj = new SolrjTest(); 
solrj.query(args[0]); 
} 
public void query(String q) throws IOException 
{ 
CommonsHttpSolrServer server = null; 
String uuid = null; 
boolean flag = true; 
while(flag==true) 
{ 
uuid = UUID.randomUUID().toString(); 
File f = new File("E:/dw-solr/tomcat-solr/bin/solr/data/SearchResult/"+uuid+".txt"); 
if(!f.exists()){ 
flag = false; 
} 
} 
try 
{ 
server = new CommonsHttpSolrServer("http://localhost:8080/solr"); 
} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
SolrQuery query = new SolrQuery(); 
query.setQuery(q); 
FileWriter fw = new FileWriter("E:/dw-solr/tomcat-solr/bin/solr/data/SearchResult/"+uuid+".txt"); 
try 
{ 
QueryResponse qr = server.query(query); 
SolrDocumentList sdl = qr.getResults(); 

Object[] o = new Object[sdl.size()]; 
o = sdl.toArray(); 
for (int i = 0; i < o.length; i++) { 
System.out.println(o[i].toString()); 
fw.write(o[i].toString() + "\n"); 
} 
fw.flush(); 
fw.close(); 
System.out.println("finished"); 
} 
catch (SolrServerException e) 
{ 
e.printStackTrace(); 
} 
} 

並將結果保存在文件中。問題是,數據的格式真的很奇怪。它看起來像:

「SolrDocument [{ID = 0000000,AT_anyName = [anyValue,多值,justMoreValue],AT_anyName2 = [標準,標準,標準],AT_mightbeanothername = [couldbealoooooooooooooooongvalue,andanotherone,andanotherone] muchMoreStuff ...約20 - 可以不同於ID到ID}]「

我想要的是一個明確的XML格式的數據。所以我將它保存爲XML文件並將其發送回其他系統。問題是,方法SolrDocumentList sdl = qr.getResults();返回如上所示的結果。正如我之前發佈的,我有dynamicFields,所以東西變得非常複雜(至少對我來說: - /)。 有沒有解決方案,我可以如何將格式更改爲簡單明瞭的XML格式?

非常感謝您的幫助。

問候

回答

2

Response Writers用於生成搜索不同格式responses.By默認情況下它返回的XML響應。通過發送HTTP請求,您可以獲得xml格式的響應。

+0

感謝您的答覆。我添加了這一行「server.setParser(new XMLResponseParser());因此,如果我現在打印出QueryResponse對象,我會得到相同的數據,如線程中的postet,但只有一行... – 2012-07-20 13:02:32

+0

@parvin如果使用SolrJ默認的格式是javabin,你可以通過wt參數來改變它,但是查詢響應已經包含了從響應本身獲得的java對象,我認爲用SolrJ得到原始的xml響應並不是一種開箱即用的方式。 – javanna 2012-07-20 14:04:05

+0

@javanna我寫的http請求並不意味着發送solrj請求,我的意思是在不使用solrj的情況下發送http請求。 – 2012-07-20 14:35:19

1

有一種方法可以通過solrj方法從solrj回覆中獲得清晰的XML格式。相關的方法是toxml用於(得到一個字符串)或中WriteXML(寫入文件)從http://lucene.apache.org/solr/5_0_0/solr-solrj/org/apache/solr/client/solrj/util/ClientUtils.html

對於實例方法,只打印你可以做的XML:

QueryResponse qr = solrclient.query(query); 
    SolrDocumentList results = qr.getResults(); 

    for (int i = 0; i < results.size(); i++) { 
     String xml = ClientUtils.toXML(ClientUtils.toSolrInputDocument(results.get(i))); 
     System.out.println(xml); 
    }