Solr Json在客户端解析?

时间:2013-07-25 08:30:25

标签: json solr jackson gson

我正在尝试从下面的json中检索日期和相应的计数,结果发现我不能这样做。经过一番努力,我用下面的奇怪代码结束了嵌套的链表。我如何选择solr_date并计算最终出现:(我欢迎任何可以执行此操作的库)

    {
  "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代码:

  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");

3 个答案:

答案 0 :(得分:2)

我建议使用SolrJ客户端:

  

Solrj是一个访问solr的java客户端。它提供了一个java接口来添加,更新和查询solr索引。

答案 1 :(得分:0)

如果您使用的是 Gson ,并且您实际上只对您突出显示的部分感兴趣,则可以执行手动解析。像这样:

//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()); 
}

现在你需要List个对象,一个包含所有日期,另一个包含所有计数:

datesList: ["2013-06-19T13:48:02Z", "2013-07-25T13:48:02Z", 2013-07-27T13:48:02Z, ...]
countsList: [10, 2, 2, ...]

注意:您可以使用List来代替使用2个Map<Integer, String>个对象...

答案 2 :(得分:0)

我这样做了。

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++;
}