用于从solr检索数据的Web服务

时间:2011-10-19 10:47:05

标签: web-services rest solr

如何编写一个休息Web服务来从java中的solr服务器查询数据。我有一个从solr

查询的java代码
CommonsHttpSolrServer server = null;

        try
        {
            server = new CommonsHttpSolrServer("http://localhost:8080/solr/");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        SolrQuery query = new SolrQuery();
        query.setQuery(solrquery);
        query.set("rows",1000);
       // query.setQueryType("dismax");
      //  query.setFacet(true);
     //   query.addFacetField("lastname");
     //   query.addFacetField("locality4");
     //   query.setFacetMinCount(2);
      //  query.setIncludeScore(true);

        try
        {
            QueryResponse qr = server.query(query);

            SolrDocumentList sdl = qr.getResults();

我需要通过将id作为查询参数来获得Web服务中的相同功能。

1 个答案:

答案 0 :(得分:1)

如果您只想查询作为参数传递给webservice的id -

    String id = "100145";
    String url = "http://localhost:8080/solr/core_name"; // core name needed if using multicore support
    CommonsHttpSolrServer solrServer;
    try {
        solrServer = new CommonsHttpSolrServer(url);
        ModifiableSolrParams qparams = new ModifiableSolrParams();
        qparams.add("q", "id:"+id);
        QueryResponse qres = solrServer.query(qparams);
        SolrDocumentList results = qres.getResults();
        SolrDocument doc = results.get(0);
        System.out.println(doc.getFieldValue("id"));
    } catch (Exception e) {
        e.printStackTrace();
    }