Reputation: 1239
I am trying to retrieve date and corresponding count from a json below and it turns out that I just can't do it. After some struggle, I ended with the weird code below with nested linkedlists. How can I select solr_date and count as appearing at the very end : (I welcome any library that can do this)
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"facet":"true",
"fl":" ",
"indent":"true",
"facet.query":" solr_date",
"q":"solr_body:party",
"facet.field":"solr_date",
"json.nl":"arrarr",
"wt":"json",
"fq":" "}},
"response":{"numFound":19,"start":0,"docs":[
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}]
},
"facet_counts":{
"facet_queries":{
" solr_date":0},
"facet_fields":{
"solr_date":
[
["2013-06-19T13:48:02Z",10], *********************************
["2013-07-25T13:48:02Z",2],
["2013-07-27T13:48:02Z",2],
["2013-07-24T13:48:02Z",1], I need these numbers individually. Date and corresponding number.
["2013-07-26T13:48:02Z",1],
["2013-07-28T13:48:02Z",1],
["2013-07-29T13:48:02Z",1],
["2013-07-30T13:48:02Z",1]]}, ***************************
"facet_dates":{},
"facet_ranges":{}}}
Java code below :
ObjectMapper mapper = new ObjectMapper();
// JsonNode rootNode = m.readTree(new URL("http://173.255.245.138:8983/solr/collection1/select?q=*%3A*&wt=json&indent=true"));
Map<String, Object> mapObject = mapper.readValue(new URL("http://ipa.ddr.ess.000:8983/solr/collection1/select?q=solr_body%3Aparty&fq=+++&fl=+&wt=json&json.nl=arrarr&indent=true&facet=true&facet.query=+solr_date&facet.field=solr_date"),new TypeReference<Map<String, Object>>() {});
LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,ArrayList<String>>>> list = (LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>>>) mapObject.get("facet_counts");
Upvotes: 0
Views: 2129
Reputation: 462
i did it something like this.
HttpSolrServer server = new HttpSolrServer("http://localhost:8084/apache-solr-3.6.0/");
server.setParser(new XMLResponseParser());
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("keyword");
solrQuery.setFilterQueries("keyword");
solrQuery.setHighlight(true);
solrQuery.setHighlightRequireFieldMatch(true);
solrQuery.addHighlightField("syndrome");
solrQuery.setStart(0);
solrQuery.setRows(10);
QueryResponse serverResponse = null;
try {
serverResponse = server.query(solrQuery);
} catch (SolrServerException e) {
e.printStackTrace();
}
Gson gson = new Gson();
List<SolrDocument> docs = new ArrayList<SolrDocument>();
for (SolrDocument doc:serverResponse.getResults()) {
docs.add(doc);
}
Map<String, String> pairs= new HashMap<String, String>();
Integer count = new Integer(0);
for (SolrDocument doc:docs){
pairs.put(("start_date" + count), doc.getFieldValue("start_date").toString());
pairs.put(("test_file_result_id" + count), doc.getFieldValue("test_file_result_id").toString());
pairs.put(("job_id" + count), doc.getFieldValue("job_id").toString());
pairs.put(("cluster" + count), doc.getFieldValue("cluster").toString());
pairs.put(("test_file_result_id" + count), doc.getFieldValue("test_file_result_id").toString());
count++;
}
Upvotes: 0
Reputation: 22555
I would suggest using the SolrJ Client:
Solrj is a java client to access solr. It offers a java interface to add, update, and query the solr index.
Upvotes: 2
Reputation: 18751
If you're using Gson, and you're actually only interested in the part you highlighted, you could do a manual parsing. Something like this:
//Create parser and get the root object
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
//Get the solr_date array
JsonArray solrDateArray = rootObj
.getAsJsonObject("facet_counts")
.getAsJsonObject("facet_fields")
.getAsJsonArray("solr_date");
//Create arrays to store the data you want to retrieve
List<String> datesList = new ArrayList<>();
List<Integer> countsList = new ArrayList<>();
//Iterate the solr_date array
Iterator<JsonElement> it = solrDateArray.iterator();
while (it.hasNext()) {
//The solr_date array contains in turn arrays, so we parse each
JsonArray array = it.next().getAsJsonArray();
//and store in your Lists the values
datesList.add(array.get(0).getAsString());
countsList.add(array.get(1).getAsInt());
}
Now you'll have to List
objects, one with all the dates and another with all the counts:
datesList: ["2013-06-19T13:48:02Z", "2013-07-25T13:48:02Z", 2013-07-27T13:48:02Z, ...]
countsList: [10, 2, 2, ...]
Note: instead of using 2 List
objects, you could use a Map<Integer, String>
for example...
Upvotes: 0