使用java for autocomplete从apache solr获取json对象

时间:2014-06-18 10:55:19

标签: java mysql json apache solr

我做了apache solr索引(mysql)数据,它通过select和suggest进行搜索,如何从key apache solr动态获取json对象,这里我给出了apache solr URL及其相应的结果

URL

localhost:8983/solr/collection1/select?q=hotelName%3A%22Royal+Orchid%22&wt=json&indent=true

输出

{
  "responseHeader": {
    "status": 0,
    "QTime": 0,
    "params": {
      "indent": "true",
      "q": "hotelName:\"Royal Orchid\"",
      "_": "1403088370128",
      "wt": "json"
    }
  },
  "response": {
    "numFound": 20,
    "start": 0,
    "docs": [
      {
        "hotelName": [
          "Royal Orchid Metropole Mysore"
        ],
        "cityName": "Mysore",
        "id": "224433",
        "_version_": 1471227815192952800
      },
      {
        "hotelName": [
          "Royal Orchid Central Bangalore"
        ],
        "cityName": "Bengaluru (and vicinity)",
        "id": "240388",
        "_version_": 1471227815235944400
      },
      {
        "hotelName": [
          "Royal Orchid Central Jaipur"
        ],
        "cityName": "Jaipur",
        "id": "258200",
        "_version_": 1471227815290470400
      },
      {
        "hotelName": [
          "Royal Orchid Brindavan Gardens"
        ],
        "cityName": "Mysore",
        "id": "258917",
        "_version_": 1471227815293616000
      },
      {
        "hotelName": [
          "Royal Orchid Central Pune"
        ],
        "cityName": "Pune",
        "id": "267814",
        "_version_": 1471227815330316300
      },
      {
        "hotelName": [
          "Royal Orchid Suites"
        ],
        "cityName": "Bengaluru (and vicinity)",
        "id": "309427",
        "_version_": 1471227815379599400
      },
      {
        "hotelName": [
          "Royal Orchid Central Ahmedabad"
        ],
        "cityName": "Ahmedabad",
        "id": "326301",
        "_version_": 1471227815444611000
      },
      {
        "hotelName": [
          "Royal Orchid Fort Resort"
        ],
        "cityName": "Mussoorie",
        "id": "327797",
        "_version_": 1471227815467679700
      },
      {
        "hotelName": [
          "Royal Orchid Resort Pattaya"
        ],
        "cityName": "Pattaya",
        "id": "344270",
        "_version_": 1471227815546323000
      },
      {
        "hotelName": [
          "Royal Orchid Central Grazia"
        ],
        "cityName": "Navi Mumbai",
        "id": "350799",
        "_version_": 1471227815558905900
      }
    ]
  }
}

是否可以从JQuery调用apache solr url进行自动完成,或者我可以编写RESTful客户端来获取JSON数据,请帮助我。

2 个答案:

答案 0 :(得分:0)

是的,如果您的JavaScript也在localhost:8983中运行。如果不是,则该呼叫将违反浏览器的跨域策略。

如果不是,则需要在与您的网站相同的域上创建一个简单的后端保镖。因此,如果您的SOLR在localhost:8983运行且您的网站在localhost:80运行,则需要JavaScript的后端帮助程序。

有问题的JS非常简单,您可以使用$.get$.getJSON。我建议使用getJSON,请参阅http://api.jquery.com/jquery.getjson/上的文档和示例。

我不知道你的后端是如何构建的,但是用于执行弹跳的c#mvc ajax控制器动作看起来像这样。请注意,我只是在没有测试的情况下编写它,这对您不起作用,但您可以将其用作指南。

    public JsonResult Search(string searchTerm)
    {
        // if your searchterm is the value for "q". You might have to encode it.
        var url = "localhost:8983/solr/collection1/select?q=" + searchTerm + "&wt=json&indent=true";

        DoxLog.Info("Contacting Sovelia API url: " + url);
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = "Dox";

        // Fire request
        using (var response = request.GetResponse())
        {
            var dataStream = response.GetResponseStream();
            using (var reader = new StreamReader(dataStream))
            {
                var responseAsString = reader.ReadToEnd();

                return Json(responseAsString, JsonRequestBehavior.AllowGet);
            }
        }
    }

答案 1 :(得分:0)

要在SOLR中实施自动完成,您必须使用SOLR的条款组件。这是在SOLR中实现自动完成功能的最有效方式。

https://wiki.apache.org/solr/TermsComponent

为了从Java代码获取JSON rsponse,我将使用以下代码:

String callbackResponse = "";
ObjectMapper mapper = new ObjectMapper();

SearchResponse searchResponse = new SearchResponse();
searchResponse = <response from SOLR >
callbackResponse = mapper.writeValueAsString(searchResponse);

return callbackResponse;

这两件事对我来说都很合适。

希望这可以帮助您解决问题。