java實現簡單的solr查詢


SolrQuery類是實現solr查詢的類。

    @Test
    public void testSelect() {
        String url = "http://localhost:8081/solr/solrcore";
        SolrClient client = new HttpSolrClient.Builder(url).build();

        SolrQuery query = new SolrQuery();
        query.set("q", "id:*");  //查詢條件
        query.set("fl", "id");  //查詢的項目
        query.setStart(0);  //起始index
        query.setRows(50);  //終了index
        query.set("sort", "id asc"); //sort key指定
        try {
            // 返回QueryResponse
            QueryResponse response = client.query(query);
            client.close();
            // 返回Document
            SolrDocumentList docs = response.getResults();
            docs.forEach((x) -> {
                System.out.println("-------------" + x.get("id"));
            });
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }

 

q  查詢的關鍵字

fl  指定返回的字段,用逗號或空格分隔(大小寫敏感)

可以使用的查詢參數比較多,這里就不一一列舉了。

 

關於如何把solr配置到tomcat中,建議參考

https://jingyan.baidu.com/article/ff411625d56a3e12e5823759.html

配置好后,訪問solr:

http://localhost:8081/solr/index.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM