Last active
August 29, 2015 14:16
-
-
Save kozy4324/882af3fc370aea52fb66 to your computer and use it in GitHub Desktop.
[Solr] ResponseWriterで書き込んでみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import org.apache.solr.common.SolrDocument; | |
import org.apache.solr.common.SolrDocumentList; | |
import org.apache.solr.common.params.SolrParams; | |
import org.apache.solr.common.util.NamedList; | |
import org.apache.solr.request.SolrQueryRequest; | |
import org.apache.solr.request.SolrQueryRequestBase; | |
import org.apache.solr.response.SolrQueryResponse; | |
import org.apache.solr.response.XMLResponseWriter; | |
import org.apache.solr.search.ReturnFields; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App | |
{ | |
@SuppressWarnings({ "rawtypes", "serial", "unchecked" }) | |
public static void main( String[] args ) throws IOException | |
{ | |
// Request | |
SolrQueryRequest req = new SolrQueryRequestBase(null, null){}; | |
SolrParams params = new SolrParams() { | |
private Map<String, Object> map = new HashMap<String, Object>(); | |
public String get(String param) { | |
if ("indent".equals(param)) { | |
return "1"; | |
} | |
return null; | |
} | |
public String[] getParams(String param) { | |
return (String[]) map.keySet().toArray(); | |
} | |
public Iterator<String> getParameterNamesIterator() { | |
return map.keySet().iterator(); | |
} | |
}; | |
req.setParams(params); | |
// Response | |
SolrQueryResponse rsp = new SolrQueryResponse(); | |
rsp.add("responseHeader", new NamedList(){{ | |
this.add("status", 0); | |
this.add("QTime", 0); | |
}}); | |
SolrDocumentList docList = new SolrDocumentList(); | |
rsp.add("response", docList); | |
SolrDocument doc = new SolrDocument(); | |
doc.setField("poi_code", "G0438231113-001"); | |
doc.setField("category1_code", new ArrayList(){{this.add("M01");}}); | |
docList.add(doc); | |
docList.setNumFound(1); | |
docList.setStart(0); | |
docList.setMaxScore(1.3f); | |
// ResponseWriter | |
XMLResponseWriter responseWriter = new XMLResponseWriter(); | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
OutputStreamWriter writer = new OutputStreamWriter(outputStream); | |
// write! | |
responseWriter.write(writer, req, rsp); | |
writer.flush(); | |
// check! | |
String str = new String(outputStream.toByteArray(), "utf-8"); | |
System.out.println(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment