Solr全文搜尋伺服器
阿新 • • 發佈:2018-12-26
Solr全文搜尋伺服器
示意圖
Solr和Lucene的區別
Solr下載與安裝
下載地址:http://archive.apache.org/dist/lucene/solr/4.10.3/
單個solr.war是可以獨立執行的,但我們想在tomcat裡執行,因此解壓Solr是在tomcat的webapps下解壓的(注意)
主頁:
點選collection1,點選documents,往裡填加內容,然後Submit Doucment。
提交結果:
點選側邊欄的Query,點選頁面的Execute Query查詢新增的結果
java操作Solr伺服器HelloWorld
新增資料(修改資料)
//新增資料,修改資料的話,可以根據id直接自動覆蓋掉舊的內容資訊 public void testAdd() throws Exception{ String URL = "192.168.179.200:8080/solr"; //例項化solr物件 SolrServer solrServer = new HttpSolrServer(URL); //例項化新增資料 SolrInputDocument doc1 = newSolrInputDocument(); doc1.setFiled("id", "1001"); doc1.setFiled("name", "iphone6s手機"); doc1.setFiled("price", "6000"); doc1.setFiled("url", "/images/001.jpg"); SolrInputDocument doc2 = newSolrInputDocument(); doc2.setFiled("id", "1002"); doc2.setFiled("name", "三星s6手機"); doc2.setFiled("price", "5000"); doc2.setFiled("url", "/images/002.jpg"); //設定伺服器儲存資訊並提交 solrServer.add(doc1); solrServer.add(doc2); solrServer.commit(); }
執行查詢
//查詢 public void testSearch() throws Exception{ String URL = "192.168.179.200:8080/solr"; //例項化solr物件 SolrServer solrServer = new HttpSolrServer(URL); //查詢類 SolrQuery solrQuery = new SolrQuery(); //查詢關鍵詞 solrQuery.set("q", "name:手機"); //查詢資料 QueryResponse response = solrServer.query(solrQuery); //取資料 SolrDocumentList solrList = response.getResults(); long num = solrList.getNumFound(); System.out.println("條數:" + num); for (SolrDocument sd : solrList) { String id = (String)sd.get("id"); String name = (String)sd.get("name"); String url = (String)sd.get("url"); Float price = (Float)sd.get("price"); System.out.println("id:" + id); } }
刪除
public void testDel() throws Exception {
SolrServer solrServer = new HttpSolrServer(URL);
//deleteById形式
solrServer.deleteById("1");
//deleteByQuery形式
solrServer.deleteByQuery("id:1001 id:1002");
solrServer.commit();
}
全文檢索基礎
IK的下載地址:https://code.google.com/archive/p/ik-analyzer/downloads
新增的配合:
<fieldType name="text_ik" class="solr.TextField">
<!--index add IKAnalyzer-->
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<!--search add IKAnalyzer-->
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
重啟tomcat,然後測試
Solr的基礎
Solr的索引操作
Solr搜尋
Solr新增物件
java物件加註解
直接server新增bean
類的轉換
Solr的查詢操作
public void queryCase() throws Exception {
SolrServer solrServer = new HttpSolrServer(URL);
SolrQuery params = new SolrQuery();
//AND、OR、NOT條件
params.set("q", "name:手 AND price:9");
params.set("q", "name:手 OR price:9");
params.set("q", "name:手機 NOT price:9");
//TO條件: 6 <= price <= 9
params.set("q", "name:手機 AND price:[6 TO 9]");
//TO條件:6 < price < 9 新增過濾器提高查詢效率
params.set("q", "price:{6 TO 9}");
params.addFilterQuery("name:電腦");
QueryResponse response = solrServer.query(params);
//取資料
SolrDocumentList solrList = response.getResults();
//顯示設定
}
public void testSearchMulti() throws Exception {
ModifiableSolrParams params = new ModifiableSolrParams();
//查詢關鍵詞,*:* 代表所有屬性,所有值,即所有index
params.set("q", "*:*");
params.set("start", 0);
params.set("rows", 20);
params.set("sort", "price desc");
QueryResponse response = server.query(params);
SolrDocumentList solrList = response.getResults();
}
Solr管理員命令
Solr叢集搭建
案例實戰說明