devefx 2019-11-19
1 下载地址 http://lucene.apache.org/solr/
wget https://mirrors.tuna.tsinghua...
tar -zxvf solr-8.1.0.tgz
2 运行 停止
切换到目录(solr-8.1.0/bin)
./solr start -force
./solr start -p 9529 -force (指定端口)
./solr stop -all
启动错误提示 Your Max Processes Limit is currently 将solr-8.1.0/bin/solr.in.sh文件中,SOLR_ULIMIT_CHECKS设置为false
浏览器输入http://127.0.0.1:9529/solr
3 修改时区
修改文件 solr-8.1.0/bin/solr.in.sh
SOLR_TIMEZONE=PRC
4 创建core实例
首先去目录solr-8.1.0/server/solr/ 创建一个名字为new_core的文件夹(如:fgou)
然后拷贝把目录solr-8.1.0/server/configsets/basic_configs下的conf目录拷贝到fgou目录下
cp -r /usr/local/src/solr-8.1.0/server/solr/configsets/_default/conf /usr/local/src/solr-8.1.0/server/solr/fgou/
然后再点击创建即可
5 配置中文分词
下载IK分词器 https://pan.baidu.com/s/1fZ52... 提取码:f76c
IKAnalyzer下载后解压会有如下文件把核心jar文件复制到solr WEB应用的lib文件夹下
把配置文件和词库等文件复制到WEB应用的classes文件夹下,如果子WEB-INF下没有这个文件夹自己创建即可
在配置文件managed-schema中增加如下配置
<fieldType name="fgou_ik" class="solr.TextField"> <analyzer type="index"> <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false"/> </analyzer> <analyzer type="query"> <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true"/> </analyzer> </fieldType>
重启服务 这样就可以选择到ik分词了,创建个
6 导入MYSQL数据
首先在D:/server/solr-7.4.0/dist目录下复制如下的jar包
复制到 D:/server/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib
再到 https://search.maven.org/search?q=mysql-connector下载mysql-connector
复制到D:/server/solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib目录下
在D:/server/solr-7.4.0/server/solr/fgou/conf/solrconfig.xml配置数据库文件信息
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">fgou-config.xml</str> </lst> </requestHandler>
创建fgou-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <dataSource typ="JdbcDataSource" driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://127.0.0.1/fgou?useSSL=true&serverTimezone=UTC&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8" user="root" password="hufeng" /> <document> <entity pk="gid" dataSource="JdbcDataSource" name="goods" query="select gid,gname,group_price,sell_num,status from goods where status=1" deltaQuery="select gid from goods where status = 1 and addtime > '${dataimporter.last_index_time}'" #新增修改索引 deletedPkQuery="select gid from goods where status != 1 and addtime > '${dataimporter.last_index_time}'" #删除索引 deltaImportQuery="select gid,gname,group_price,sell_num,status from goods where gid='${dataimporter.delta.gid}'" > <field column="gid" name="gid"/> <field column="gname" name="gname"/> <field column="group_price" name="group_price"/> <field column="sell_num" name="sell_num"/> <field column="status" name="status"/> </entity> </document> </dataConfig>
在managed-schema中配置字段检索使用的分词器
修改:<uniqueKey>gid</uniqueKey> 新增: <field name="goods" type="fgou_ik" uninvertible="true" indexed="true" stored="true"/> <field name="gid" type="string" multiValued="false" indexed="true" required="true" stored="true"/> <field name="gname" type="fgou_ik" indexed="true" stored="true"/> <field name="group_price" type="pdouble" indexed="true" stored="true"/> <field name="sell_num" type="pint" indexed="true" stored="true"/> <field name="status" type="pint" indexed="true" stored="true"/>
重启服务
建立全量索引
建立增量索引
查询
7 php api调用
//solr 查询 public function solrQuery($core='fgou',$fl='gid',$fq='status:1',$q='gid:1',$sort='gid desc,sell desc',$start=0,$rows=10){ $fl = urlencode($fl); $fq = urlencode($fq); $q = urlencode($q); $sort = urlencode($sort); $url = 'http://127.0.0.1:9529/solr/'.$core.'/select?fl='.$fl.'&fq='.$fq.'&q='.$q.'&sort='.$sort.'&rows='.$rows.'&start='.$start; $res = file_get_contents($url); $resArr = json_decode($res,true); return $resArr['response']; } //solr 更新索引 public function solrIndex($core='fgou'){ $url = 'http://127.0.0.1:9529/solr/fgou/dataimport?indent=on&wt=json'; $param = [ 'command'=>'delta-import', 'verbose'=>'false', 'clean'=>'false', 'commit'=>'true', 'core'=>$core, 'name'=>'dataimport', ]; $this->httpCurl($url,$param); } }
参考资料:
https://blog.csdn.net/u010510107/article/details/81051795 https://www.jianshu.com/p/4e86df9532d9 https://blog.csdn.net/lhc0512/article/details/82315354 https://www.jianshu.com/p/9ec946fb5be7 https://www.cnblogs.com/davidwang456/p/4744415.html