Java調用solrj5.5.3介面,查詢資料

來源:互聯網
上載者:User

標籤:mem   exception   sde   ide   ipo   ext   uri   bcb   servlet   

前期準備搭建solr服務

參考上一篇,搭建solr搜尋服務。

添加依賴

maven工程的話,添加如下依賴,

<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --><dependency>    <groupId>org.apache.solr</groupId>    <artifactId>solr-solrj</artifactId>    <version>5.5.3</version></dependency>

也可以自己匯入jar包

在solr安裝目錄下,找到solr-5.5.3\dist\solrj-lib路徑,添加裡面所有的jar包到自己的工程,別忘了在外面的檔案夾還有個solr-solrj-5.5.3.jar包,一起拷貝。

編寫調用代碼

這裡簡單給個樣本(包含分頁),Spring mvc工程的:

    @ResponseBody    @RequestMapping(value = "/searchByKeyWord", method =    { RequestMethod.POST, RequestMethod.GET }, produces = "application/json; charset=utf-8")    public String search(HttpServletRequest request, HttpServletResponse response, String keyWord,        int pageSize, int pageOffset)    {        try        {            String urlString = ConfigUtil.getValueByKey("solrServiceUrl");            SolrClient solr = new HttpSolrClient(urlString);            SolrQuery query = new SolrQuery();            query.set("q", URLDecoder.decode(keyWord, "UTF-8"));            query.set("start", String.valueOf(pageOffset * pageSize));            query.set("rows", String.valueOf(pageSize));            QueryResponse queryResponse = solr.query(query);            // SolrDocumentList list = queryResponse.getResults();            Map<String, Object> result = new HashMap<String, Object>();            List<OCSearchObject> rstList = queryResponse.getBeans(OCSearchObject.class);            result.put("success", true);            result.put("key", URLDecoder.decode(keyWord, "UTF-8"));            result.put("list", rstList);            result.put("totalCount", queryResponse.getResults().getNumFound());            solr.close();            return json(result);        }        catch (Exception e)        {            LOGGER.error(e.toString(), e);            return jsonpFalse(request, response, "code_error");        }    }

以下是OCSearchObject類,注意要與data-config.xml配置的欄位對應起來,還有就是不要忘了在欄位前面加上@Field註解(org.apache.solr.client.solrj.beans.Field)。

public class OCSearchObject{    private Long id;    private String name;    private String fullText;    private String type;    /**     * @return the id     */    public Long getId()    {        return id;    }    /**     * @param id the id to set     */    @Field("id")    public void setId(Long id)    {        this.id = id;    }    /**     * @return the name     */    public String getName()    {        return name;    }    /**     * @param name the name to set     */    @Field("name")    public void setName(String name)    {        this.name = name;    }    /**     * @return the fullText     */    public String getFullText()    {        return fullText;    }    /**     * @param fullText the fullText to set     */    @Field("fullText")    public void setFullText(String fullText)    {        this.fullText = fullText;    }    /**     * @return the type     */    public String getType()    {        return type;    }    /**     * @param type the type to set     */    @Field("type")    public void setType(String type)    {        this.type = type;    }}
前端樣本

其它的根據業務需求具體擴充吧,在前端可以對查詢到的資料進行一些自訂展示(關鍵字標紅高亮,每條記錄分類,點擊跳轉到記錄詳情頁面)。

Java調用solrj5.5.3介面,查詢資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.