从SOLR解析响应

时间:2012-05-29 12:41:31

标签: java json solr

{responseHeader={status=0,QTime=0,params={q=ketan,wt=[javabin,javabin],version=2.2}},response={numFound=1,start=0,docs=[SolrDocument[{user_first_name=Ketan, customerid=5,killname=Hockey, areaname=Paud, categoryname=Sports, last_name=Kore, statename=Maharashtra, cityname=Pune, contactno=124555698}]]}}

我收到了这个响应SOLR,并希望从中获取JSONObject SOlr Document。 请帮我解析一下这个回复。

3 个答案:

答案 0 :(得分:0)

您提到的字符串是json字符串(json是Javascript Object Notation)。尝试使用java-json lib对其进行解码。 Here is an example

答案 1 :(得分:0)

你需要使用哈希映射来制作JSONObject,solrj不支持json格式作为现在的输出

答案 2 :(得分:0)

我发现这篇文章非常有用。虽然在提出这个问题的时候很顺利,但是当我遇到同样的问题时,它也可以帮助其他人。我使用Google API for JSON - GSON解决了这个问题。

Solr提供的响应不是JSON格式。我们需要转换它。 我从solr检索了响应并将其转换为json对象。

最初,我的回答是:

   {docs=[SolrDocument{id=1, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=1.0.0, packDate=May 22, 2014, 3:10 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468794552324194304}, SolrDocument{id=2, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack New, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=2.0.0, packDate=May 22, 2014, 3:13 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468794760693022720}, SolrDocument{id=3, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Packs updated, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=3.0.0, packDate=May 22, 2014, 3:32 pm, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1468795898385399808}, SolrDocument{id=5, uuid=7fe08f79-e184-454d-908f-08b32583a822, packName=Workflow Logger-Pack New, packDescription=Workflow Logger-Pack, packAuthor=NetApp, packVersion=2.0.0, packDate=June 17, 2014, 7:02 am, entityName=[Workflow Logger, Wait, Logging cmd, Test cmd for multiple Parameter Types], entityDescription=[Workflow Logger, Wait Command, Logging command, Test cmd for multiple Parameter Types], entityType=[Workflow, Command, Command, Command], _version_=1471140110510587904}]}

我的代码:

 HttpSolrServer server= new HttpSolrServer("http://localhost:8983/solr");
        server.setParser(new XMLResponseParser());

        Gson gson= new Gson();

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");

        solrQuery.setHighlight(true);

        solrQuery.setHighlightRequireFieldMatch(true);
        QueryResponse response= server.query(solrQuery);
        SolrDocumentList docs = response.getResults();

        String res= gson.toJson(docs);
        System.out.println(res);

现在,它将转换为JSON:

[
  {
    "id": "1",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "1.0.0",
    "packDate": "May 22, 2014, 3:10 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468794552324194300
  },
  {
    "id": "2",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack New",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "2.0.0",
    "packDate": "May 22, 2014, 3:13 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468794760693022700
  },
  {
    "id": "3",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Packs updated",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "3.0.0",
    "packDate": "May 22, 2014, 3:32 pm",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1468795898385399800
  },
  {
    "id": "5",
    "uuid": "7fe08f79-e184-454d-908f-08b32583a822",
    "packName": "Workflow Logger-Pack New",
    "packDescription": "Workflow Logger-Pack",
    "packAuthor": "NetApp",
    "packVersion": "2.0.0",
    "packDate": "June 17, 2014, 7:02 am",
    "entityName": [
      "Workflow Logger",
      "Wait",
      "Logging cmd",
      "Test cmd for multiple Parameter Types"
    ],
    "entityDescription": [
      "Workflow Logger",
      "Wait Command",
      "Logging command",
      "Test cmd for multiple Parameter Types"
    ],
    "entityType": [
      "Workflow",
      "Command",
      "Command",
      "Command"
    ],
    "_version_": 1471140110510588000
  }
]

希望它有所帮助。