solr学习笔记

32次阅读

共计 3737 个字符,预计需要花费 10 分钟才能阅读完成。

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 分词了, 为 gname 创建个

6 导入 MYSQL 数据
首先在 D:serversolr-7.4.0dist 目录下复制如下的 jar 包

复制到 D:serversolr-7.4.0serversolr-webappwebappWEB-INFlib

再到 https://search.maven.org/sear…
复制到 D:serversolr-7.4.0serversolr-webappwebappWEB-INFlib 目录下

在 D:serversolr-7.4.0serversolrfgouconfsolrconfig.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&amp;serverTimezone=UTC&amp;verifyServerCertificate=false&amp;allowMultiQueries=true&amp;useUnicode=true&amp;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); }

正文完
 0