Solr入门
本文目标:了解,安装,使用
学习资料:
https://lucene.apache.org/solr/resources.html
https://lucene.apache.org/solr/guide/7_6/solr-tutorial.html
什么是Solr?
Solr is the popular, blazing-fast, open source enterprise search platform built on Apache Lucene.
Solr is highly reliable, scalable and fault tolerant, providing distributed indexing, replication and load-balanced querying, automated failover and recovery, centralized configuration and more. Solr powers the search and navigation features of many of the world's largest internet sites.
我做实验下载的是7.6.0版本,官网下载的。
启动和配置solr集群
以SolrCloud模式启动:./solr.cmd start -e cloud
出现了第一个交互式回话问题:要在本地集群启动几个节点?默认是2。
然后默认会使用两个端口创建节点,并且创建Solr home目录。
启动了第一个节点。
然后启动另一个节点,从日志里看到Zookeeper的信息,Solr Cloud模式默认使用ZK做Solr节点服务的注册,发现,协调。
给collection起名
看来不需要一个个解释每一步,很傻。跟着官方文档来就好了,一步一步写解释速度太慢,后面不这样写了。
创建好的数据集,有两个分片,这两个分片在每个节点上都有,分片按索引分的,索引两个分片的数据构成一个数据整体。
逻辑上每个分片有两个副本,实际在不同节点上存储。(具体见下面Gragh(Radial)展开的图更清晰)
选中侧边栏Gragh(Radial)能清楚的看到Solr存储的网络结构(理解这幅图很重要):
下面这幅图,显示了分片的索引范围和所在的节点:
小结:
通过上面的步骤,使用solr cloud模式创建了两个node,一个数据集,每个node有一个shard,每个shard有两个replica。
shard的作用是索引分片,举个例子,将0-499的索引分到一个节点,将500到999的索引分到另一个节点。
replica是索引的副本,每个shard有两个副本,目的是为了故障恢复。
导入模拟数据
搜索
实验1:查询所有数据
实验2:查询关键词为foundation的数据
实验3:在实验2的基础上只查询属性id
有一个小问题,发现我导的模拟数据和官网文档不一样。查询的结果不一样。
然后后面直接记录下例子中的查询姿势吧:
http://localhost:8983/solr/techproducts/select?q=cat:electronics
查询cat关键字和electronics关键字关联的数据
http://localhost:8983/solr/techproducts/select?q=\"CAS+latency\"
查询一个由两个关键字组成的短语或者词组。因为英文分词可以按空格分词,但是现在这个需求就是想查CAS latency这个词也是可以的。
http://localhost:8983/solr/techproducts/select?q=+electronics%20-music
使用+-进行组合查询,表示包含不包含
更多关于搜索的,点击这里。
收尾:
use the bin/solr
script we started out with to delete this collection:
bin/solr delete -c techproducts
And then create a new collection:
bin/solr create -c <yourCollection> -s 2 -rf 2
To stop both of the Solr nodes we started, issue the command:
bin/solr stop -all
For more information on start/stop and collection options with bin/solr
, see Solr Control Script Reference.