Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Created February 27, 2015 10:26
Show Gist options
  • Save kozy4324/ab75bda214dcc1068faa to your computer and use it in GitHub Desktop.
Save kozy4324/ab75bda214dcc1068faa to your computer and use it in GitHub Desktop.
[Solr] ResponseWriterで書き込んでみるJRuby版
java_import Java::java.io.ByteArrayOutputStream
java_import Java::java.io.OutputStreamWriter
java_import Java::java.util.HashMap
java_import Java::org.apache.solr.common.SolrDocument
java_import Java::org.apache.solr.common.SolrDocumentList
java_import Java::org.apache.solr.common.params.SolrParams
java_import Java::org.apache.solr.common.util.NamedList
java_import Java::org.apache.solr.request.SolrQueryRequestBase
java_import Java::org.apache.solr.response.SolrQueryResponse
java_import Java::org.apache.solr.response.XMLResponseWriter
class MySolrQueryRequest < SolrQueryRequestBase; end
class MySolrParams < SolrParams
def get param
nil
end
def getParams param
[]
end
def getParameterNamesIterator
HashMap.new.keySet.iterator
end
end
# Request
req = MySolrQueryRequest.new nil, nil
req.setParams MySolrParams.new
# Response
rsp = SolrQueryResponse.new
rh = NamedList.new
rh.add "status", 0
rh.add "QTime", 0
rsp.add "responseHeader", rh
docList = SolrDocumentList.new
rsp.add "response", docList
doc = SolrDocument.new
doc.setField "poi_code", "G0438231113-001"
doc.setField "category1_code", ["M01"]
docList.add doc
docList.setNumFound 1
docList.setStart 0
docList.setMaxScore 1.3
# ResponseWriter
responseWriter = XMLResponseWriter.new
outputStream = ByteArrayOutputStream.new
writer = OutputStreamWriter.new outputStream
# write!
responseWriter.write writer, req, rsp
writer.flush
puts Java::java.lang.String.new outputStream.toByteArray, "utf-8"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment