1)建立新用戶html
2)解壓 運行:java
bin/solr start -e cloud -nopromptnode
顯示:web
Welcome to the SolrCloud example! Starting up 2 Solr nodes for your example SolrCloud cluster. Creating Solr home directory /home/solr/solr-6.5.0/example/cloud/node1/solr Cloning /home/solr/solr-6.5.0/example/cloud/node1 into /home/solr/solr-6.5.0/example/cloud/node2 Starting up Solr on port 8983 using command: bin/solr start -cloud -p 8983 -s "example/cloud/node1/solr" Solr is already setup and running on port 8983 with status: { "solr_home":"/home/solr/solr-6.5.0/example/cloud/node1/solr", "version":"6.5.0 4b16c9a10c3c00cafaf1fc92ec3276a7bc7b8c95 - jimczi - 2017-03-21 20:47:12", "startTime":"2017-04-14T16:36:44.638Z", "uptime":"0 days, 0 hours, 6 minutes, 29 seconds", "memory":"59.6 MB (%12.2) of 490.7 MB", "cloud":{ "ZooKeeper":"localhost:9983", "liveNodes":"2", "collections":"0"}} If this is not the example node you are trying to start, please choose a different port. Starting up Solr on port 7574 using command: bin/solr start -cloud -p 7574 -s "example/cloud/node2/solr" -z localhost:9983 Solr is already setup and running on port 7574 with status: { "solr_home":"/home/solr/solr-6.5.0/example/cloud/node2/solr", "version":"6.5.0 4b16c9a10c3c00cafaf1fc92ec3276a7bc7b8c95 - jimczi - 2017-03-21 20:47:12", "startTime":"2017-04-14T16:38:02.705Z", "uptime":"0 days, 0 hours, 5 minutes, 12 seconds", "memory":"37 MB (%7.5) of 490.7 MB", "cloud":{ "ZooKeeper":"localhost:9983", "liveNodes":"2", "collections":"0"}} If this is not the example node you are trying to start, please choose a different port. INFO - 2017-04-14 12:43:16.519; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready Connecting to ZooKeeper at localhost:9983 ... INFO - 2017-04-14 12:43:17.274; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready Uploading /home/solr/solr-6.5.0/server/solr/configsets/data_driven_schema_configs/conf for config gettingstarted to ZooKeeper at localhost:9983 Creating new collection 'gettingstarted' using command: http://localhost:8983/solr/admin/collections?action=CREATE&name=gettingstarted&numShards=2&replicationFactor=2&maxShardsPerNode=2&collection.configName=gettingstarted
索引目錄中的文件(xml,json,html...):apache
bin/post -c gettingstarted example/exampledocs/*.xml
-c gettingstarted
: 要索引的集合名稱刪除數據:bin/post -c gettingstarted -d "<delete><id>SP2514N</id></delete>"json
搜索安全
訪問客戶端界面:http://localhost:8983/solr/#/gettingstarted/query服務器
基本單項搜索:curl
curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=name:Foundation"ide
語句搜索:
curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=\"CAS+latency\""
聯合搜索:
curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=%2Btwo+-one"
在url中+號會被轉化,因此會用%2B表示,+表示匹配two,-表示不匹配one
facets範圍搜索:
curl 'http://localhost:8983/solr/gettingstarted/select?q=*:*&wt=json&indent=on&rows=0'\
'&facet=true'\
'&facet.range=price'\
'&f.price.facet.range.start=0'\
'&f.price.facet.range.end=600'\
'&f.price.facet.range.gap=50'\
'&facet.range.other=after'
package com.hpe.solr; import java.io.IOException; import java.util.List; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; //http://blog.csdn.net/u012385190/article/details/53115546 public class SolrTest { private static final String SOLR_URL = "http://192.18.80.10:8983/solr/"; /** * 建立 HttpSolrClient對象 該對象有兩個能夠使用,都是線程安全的 * 一、CommonsHttpSolrServer:啓動web服務器使用的,經過http請求的 二、 * EmbeddedSolrServer:內嵌式的,導入solr的jar包就能夠使用了 三、solr * 4.0以後好像添加了很多東西,其中CommonsHttpSolrServer這個類更名爲HttpSolrClient * * @return */ public HttpSolrClient createSolrServer() { HttpSolrClient solr = null; solr = new HttpSolrClient(SOLR_URL); return solr; } public void addDoc() throws SolrServerException, IOException { // 構造一篇文檔 SolrInputDocument document = new SolrInputDocument(); // 往doc添加字段,在客戶端這邊添加的字段必須在服務端中有過定義 document.addField("id", 8); document.addField("name", "周新星"); document.addField("description", "一個灰常牛逼的軍事家"); // 得到一個solr服務端的請求,去提交 ,選擇具體的某一個solr core HttpSolrClient solr = new HttpSolrClient(SOLR_URL + "my_core"); solr.add(document); solr.commit(); solr.close(); } /** * 根據id從索引庫刪除文檔 */ public void deleteDocumentById() throws Exception { // 選擇具體的某一個solr core HttpSolrClient server = new HttpSolrClient(SOLR_URL + "my_core"); // 刪除文檔 server.deleteById("8"); // 刪除全部的索引 // solr.deleteByQuery("*:*"); // 提交修改 server.commit(); server.close(); } /** * 查詢 * * @throws Exception */ public void querySolr() throws Exception { HttpSolrClient solrServer = new HttpSolrClient(SOLR_URL + "my_core/"); SolrQuery query = new SolrQuery(); // 下面設置solr查詢參數 // query.set("q", "*:*");// 參數q 查詢全部 query.set("q", "周星馳");// 相關查詢,好比某條數據某個字段含有周、星、馳三個字 將會查詢出來 ,這個做用適用於聯想查詢 // 參數fq, 給query增長過濾查詢條件 query.addFilterQuery("id:[0 TO 9]");// id爲0-4 // 給query增長布爾過濾條件 // query.addFilterQuery("description:演員"); //description字段中含有「演員」兩字的數據 // 參數df,給query設置默認搜索域 query.set("df", "name"); // 參數sort,設置返回結果的排序規則 query.setSort("id", SolrQuery.ORDER.desc); // 設置分頁參數 query.setStart(0); query.setRows(10);// 每一頁多少值 // 參數hl,設置高亮 query.setHighlight(true); // 設置高亮的字段 query.addHighlightField("name"); // 設置高亮的樣式 query.setHighlightSimplePre("<font color='red'>"); query.setHighlightSimplePost("</font>"); // 獲取查詢結果 QueryResponse response = solrServer.query(query); // 兩種結果獲取:獲得文檔集合或者實體對象 // 查詢獲得文檔的集合 SolrDocumentList solrDocumentList = response.getResults(); System.out.println("經過文檔集合獲取查詢的結果"); System.out.println("查詢結果的總數量:" + solrDocumentList.getNumFound()); // 遍歷列表 for (SolrDocument doc : solrDocumentList) { System.out.println("id:" + doc.get("id") + " name:" + doc.get("name") + " description:" + doc.get("description")); } // 獲得實體對象 List<Person> tmpLists = response.getBeans(Person.class); if (tmpLists != null && tmpLists.size() > 0) { System.out.println("經過文檔集合獲取查詢的結果"); for (Person per : tmpLists) { System.out.println("id:" + per.getId() + " name:" + per.getName() + " description:" + per.getDescription()); } } } public static void main(String[] args) throws Exception { SolrTest solr = new SolrTest(); // solr.createSolrServer(); solr.addDoc(); solr.deleteDocumentById(); solr.querySolr(); } }