Solr是Apache下的一個頂級開源項目,採用Java開發,它是基於Lucene的全文搜索服務器。Solr提供了比Lucene更爲豐富的查詢語言,同時實現了可配置、可擴展,並對索引、搜索性能進行了優化html
~$ ls solr*
solr-7.5.0.zip
~$ unzip -q solr-7.5.0.zip
~$ cd solr-7.5.0/
複製代碼
~$ bin/solr start -e cloud
複製代碼
windows:bin\solr.cmd start -e cloud
複製代碼
solr-7.5.0:$ ./bin/solr start -e cloud
Welcome to the SolrCloud example!
This interactive session will help you launch a SolrCloud cluster on your local workstation.
To begin, how many Solr nodes would you like to run in your local cluster? (specify 1-4 nodes) [2]:
複製代碼
輸入本地集羣啓動安裝的節點數量,默認爲2Ok, let's start up 2 Solr nodes for your example SolrCloud cluster. Please enter the port for node1 [8983]: 複製代碼
輸入第一個節點的服務端口號,默認8983Please enter the port for node2 [7574]:
複製代碼
輸入第二個節點的端口號,默認7574Starting up 2 Solr nodes for your example SolrCloud cluster.
Creating Solr home directory /solr-7.5.0/example/cloud/node1/solr
Cloning /solr-7.5.0/example/cloud/node1 into
/solr-7.5.0/example/cloud/node2
Starting up Solr on port 8983 using command:
"bin/solr" start -cloud -p 8983 -s "example/cloud/node1/solr"
Waiting up to 180 seconds to see Solr running on port 8983 [\]
Started Solr server on port 8983 (pid=34942). Happy searching!
Starting up Solr on port 7574 using command:
"bin/solr" start -cloud -p 7574 -s "example/cloud/node2/solr" -z localhost:9983
Waiting up to 180 seconds to see Solr running on port 7574 [\]
Started Solr server on port 7574 (pid=35036). Happy searching!
INFO - 2017-07-27 12:28:02.835; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready
複製代碼
這個時候,一個兩個節點的solr集羣就已經啓動完成,solr依靠zookeeper協調管理各個節點,solr會默認啓動一個嵌套的zookeeper,啓動完成後,會提示你建立集合,熟悉lucene的能夠理解爲建立document,用來建立數據的索引java
Now let's create a new collection for indexing documents in your 2-node cluster. Please provide a name for your new collection: [gettingstarted] 複製代碼
我這裏輸入techproducts,和官網一致node
How many shards would you like to split techproducts into? [2]
複製代碼
這裏默認值爲2,爲了讓索引均勻的分佈再每一個節點上shell
How many replicas per shard would you like to create? [2]
複製代碼
這裏輸入因此備份數量apache
Please choose a configuration for the techproducts collection, available options are:
_default or sample_techproducts_configs [_default]
複製代碼
輸入配置文件夾名稱,主要的配置文件有schema.xml和solrconfig.xml,並在solr-7.5.0\server\solr\configsets文件夾下建立相應的文件夾json
Uploading /solr-7.5.0/server/solr/configsets/_default/conf for config techproducts to ZooKeeper at localhost:9983
Connecting to ZooKeeper at localhost:9983 ...
INFO - 2017-07-27 12:48:59.289; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready
Uploading /solr-7.5.0/server/solr/configsets/sample_techproducts_configs/conf for config techproducts to ZooKeeper at localhost:9983
Creating new collection 'techproducts' using command:
http://localhost:8983/solr/admin/collections?action=CREATE&name=techproducts&numShards=2&replicationFactor=2&maxShardsPerNode=2&collection.configName=techproducts
{
"responseHeader":{
"status":0,
"QTime":5460},
"success":{
"192.168.0.110:7574_solr":{
"responseHeader":{
"status":0,
"QTime":4056},
"core":"techproducts_shard1_replica_n1"},
"192.168.0.110:8983_solr":{
"responseHeader":{
"status":0,
"QTime":4056},
"core":"techproducts_shard2_replica_n2"}}}
Enabling auto soft-commits with maxTime 3 secs using the Config API
POSTing request to Config API: http://localhost:8983/solr/techproducts/config
{"set-property":{"updateHandler.autoSoftCommit.maxTime":"3000"}}
Successfully set-property updateHandler.autoSoftCommit.maxTime to 3000
SolrCloud example running, please visit: http://localhost:8983/solr
複製代碼
至此,solr建立techproduct已經完成,能夠訪問solr的管理界面http://localhost:8983/solr,接下來能夠向solr添加須要搜索的數據windows
Linux/Mac:bash
solr-7.5.0:$ bin/post -c techproducts example/exampledocs/*
複製代碼
windows:服務器
C:\solr-7.5.0> java -jar -Dc=techproducts -Dauto example\exampledocs\post.jar example\exampledocs\*
複製代碼
example\exampledocs下的文件是solr提供測試演示的文檔,正式使用的時候能夠是咱們本身的所須要分析的數據session
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/techproducts/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file books.csv (text/csv) to [base]
POSTing file books.json (application/json) to [base]/json/docs
POSTing file gb18030-example.xml (application/xml) to [base]
POSTing file hd.xml (application/xml) to [base]
POSTing file ipod_other.xml (application/xml) to [base]
POSTing file ipod_video.xml (application/xml) to [base]
POSTing file manufacturers.xml (application/xml) to [base]
POSTing file mem.xml (application/xml) to [base]
POSTing file money.xml (application/xml) to [base]
POSTing file monitor.xml (application/xml) to [base]
POSTing file monitor2.xml (application/xml) to [base]
POSTing file more_books.jsonl (application/json) to [base]/json/docs
POSTing file mp500.xml (application/xml) to [base]
POSTing file post.jar (application/octet-stream) to [base]/extract
POSTing file sample.html (text/html) to [base]/extract
POSTing file sd500.xml (application/xml) to [base]
POSTing file solr-word.pdf (application/pdf) to [base]/extract
POSTing file solr.xml (application/xml) to [base]
POSTing file test_utf8.sh (application/octet-stream) to [base]/extract
POSTing file utf8-example.xml (application/xml) to [base]
POSTing file vidcard.xml (application/xml) to [base]
21 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/techproducts/update...
Time spent: 0:00:00.822
複製代碼
到這裏,咱們的solr加入了分析的數據,而且solr會根據文件的特性,生成默認的field
solr提供界面進行搜索,操做簡單,同事提供接口對外進行搜索,這裏展現接口的方式搜索
Search for a Single Term
根據關鍵字foundation進行搜索
curl "http://localhost:8983/solr/techproducts/select?q=foundation"
複製代碼
Field Searches
搜索字段cat並等於electronics的文件
curl "http://localhost:8983/solr/techproducts/select?q=cat:electronics"
複製代碼
Phrase Search
搜索包含CAS latency的文件
curl "http://localhost:8983/solr/techproducts/select?q=\"CAS+latency\""
複製代碼