solr簡介
Solr是Apache下的一個頂級開源項目,采用Java開發(fā),它是基于Lucene的全文搜索服務(wù)器。Solr提供了比Lucene更為豐富的查詢語言,同時實現(xiàn)了可配置、可擴展,并對索引、搜索性能進(jìn)行了優(yōu)化
solr安裝
- 官網(wǎng)下載安裝包,最新的版本是7.5,點擊下載地址進(jìn)行下載,并解壓文件,進(jìn)入所解壓的安裝目錄(window同理)
~$ ls solr*
solr-7.5.0.zip
~$ unzip -q solr-7.5.0.zip
~$ cd solr-7.5.0/
- 啟動solr
Unix or MacOS:
windows:~$ bin/solr start -e cloudbin\solr.cmd start -e cloud
輸入本地集群啟動安裝的節(jié)點數(shù)量,默認(rèn)為2solr-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]:
輸入第一個節(jié)點的服務(wù)端口號,默認(rèn)8983Ok, let's start up 2 Solr nodes for your example SolrCloud cluster. Please enter the port for node1 [8983]:
輸入第二個節(jié)點的端口號,默認(rèn)7574Please enter the port for node2 [7574]:Starting 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
創(chuàng)建collection(索引的邏輯概念)
這個時候,一個兩個節(jié)點的solr集群就已經(jīng)啟動完成,solr依靠zookeeper協(xié)調(diào)管理各個節(jié)點,solr會默認(rèn)啟動一個嵌套的zookeeper,啟動完成后,會提示你創(chuàng)建集合,熟悉lucene的可以理解為創(chuàng)建document,用來創(chuàng)建數(shù)據(jù)的索引
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,和官網(wǎng)一致
How many shards would you like to split techproducts into? [2]
這里默認(rèn)值為2,為了讓索引均勻的分布再每個節(jié)點上
How many replicas per shard would you like to create? [2]
這里輸入所以備份數(shù)量
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文件夾下創(chuàng)建相應(yīng)的文件夾
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創(chuàng)建techproduct已經(jīng)完成,可以訪問solr的管理界面http://localhost:8983/solr,接下來可以向solr添加需要搜索的數(shù)據(jù)
Linux/Mac:
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提供測試演示的文檔,正式使用的時候可以是我們自己的所需要分析的數(shù)據(jù)
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加入了分析的數(shù)據(jù),并且solr會根據(jù)文件的特性,生成默認(rèn)的field
小試牛刀
solr提供界面進(jìn)行搜索,操作簡單,同事提供接口對外進(jìn)行搜索,這里展示接口的方式搜索
-
Search for a Single Term
根據(jù)關(guān)鍵字foundation進(jìn)行搜索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\""