0

我试图从下面的 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");
4

3 回答 3

2

我建议使用SolrJ客户端:

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

于 2013-07-25T11:27:21.150 回答
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++;
}
于 2013-07-25T18:12:01.787 回答
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:您可以使用Map<Integer, String>例如...而不是使用 2 个对象

于 2013-07-25T11:27:13.000 回答