最新消息: 新版网站上线了!!!

php使用solr对关键词多字段查询和全文检索

一、说明

1、solr版本是7.7.0

2、系统环境windows

3、solr启动 直接进入solr文件夹下的bin目录,执行solr start

D:\solr-7.7.0\bin>solr start

4、由于这篇文章说的是php调用solr扩展时,一个关键词如何在多个字段查询搜索,所以就省略了solr的安装和solr core的生成,对于安装过程和配置如有疑问请加   QQ:1914421469

一、solr的安装省略

二、生成core省略

三、我这里已经生成了一个core名称为bug

在solr中配置bug对应数据源,我的数据源配置文件为db-data-config.xml

db-data-config.xml :

<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/testlink_quality" user="root" password="123456" />
<document>
<entity name="mantis_bug" query="select * from mantis_bug" pk="id">
<field column='id' name='id' />
<field column='summary' name='summary' />
<field column='project_id' name='project_id' />
<field column='description' name='description' />
</entity>
</document>
</dataConfig>

四、在php代码中调用solr进行搜索查询

$options = array(
    'hostname' => 'localhost',
    'path'     => 'solr/bug',
    'port'     => '8983'
);
$solrClient = new SolrClient($options);
$keyword = $_REQUEST['keyword'];    //搜索关键词,以“库存”为例
$query = new SolrQuery();
//$query->setQuery('summary:库存 or description:库存'); 在summary和description2个字段中搜索‘库存’关键词
$query->setQuery('summary:'.$keyword.' or description:'.$keyword); //看到了把在多个字段中查询同一个关键词时,可以使用or进行拼接
$query->setStart(0);
$query->setRows(50);
$query->addField('summary');
$query->addField('description');
$query->addField('project_id');
$query->addField('id');
$query_response = $solrClient->query($query);
$response = $query_response->getResponse();
echo "<pre>";
print_r($response);


五、结果

SolrObject Object
(
    [responseHeader] => SolrObject Object
        (
            [status] => 0
            [QTime] => 1
            [params] => SolrObject Object
                (
                    [q] => summary:库存 or description:库存
                    [indent] => on
                    [fl] => summary,description,project_id,id
                    [start] => 0
                    [rows] => 50
                    [version] => 2.2
                    [wt] => xml
                )

        )

    [response] => SolrObject Object
        (
            [numFound] => 13
            [start] => 0
            [docs] => Array
                (
                    [0] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 凭证库存查询查询结果为空
                                )

                            [description] => 凭证库存查询交易查询不到数据
                            [project_id] => 1375
                            [id] => 7
                        )

                    [1] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 查询库存凭证报错
                                )

                            [description] => 1.进入“查询库存凭证”交易
2.点击【查询】按钮,报错,如图:01.png
                            [project_id] => 1427
                            [id] => 512
                        )

                    [2] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 查询库存凭证-查询条件凭证种类无效
                                )

                            [description] => 1.进入“查询库存凭证”,凭证种类选择“116-现金支票”,点击查询
2.查询结果未筛选出现金支票的结果,如图:01.png
                            [project_id] => 1427
                            [id] => 607
                        )

                    [3] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20350重要物品出入库:物品入库成功后,库存余额一直是0
                                )

                            [description] => 重要物品入库:提交交易后,返回信息处库存余额显示为0
                            [project_id] => 1427
                            [id] => 1200
                        )

                    [4] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20350重要物品出入库:库存为0时,进行出库交易时,系统应该给出相应提示,而不是出库成功
                                )

                            [description] => 前提:金银首饰的库存为0
1.选择操作标志出库
2.点击添加,选择重要物品种类:金银首饰 ,输入数量4,点击提交
3.点击提交,系统应该给出相应提示“库存为0”
                            [project_id] => 1427
                            [id] => 1553
                        )

                    [5] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20163查询库存凭证-凭证种类显示名称与其他地方不一样
                                )

                            [description] => 1.柜员号:069501,进入查询库存凭证交易
2.凭证种类选择 184-IC卡,其他模块该种凭证叫,184-金农IC卡。如图:01.png
需要修改为一致
                            [project_id] => 1427
                            [id] => 1279
                        )

                    [6] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20102现金出入库,资金去向为存放人行时的现金出库自动回显的账号为库存现金内部账
                                )

                            [description] => 20102现金出入库,资金去向为存放人行时的现金出库自动回显的账号为库存现金内部账
                            [project_id] => 1427
                            [id] => 2265
                        )

                    [7] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20350重要物品出入库:当输入的物品数量大于实际库存数量时,系统应该给出错误提示
                                )

                            [description] => 金银首饰实际库存5
1.选择操作标志出库
2.点击添加,选择重要物品种类金银首饰,输入数量6,点击提交
3.点击提交,提交成功
                            [project_id] => 1427
                            [id] => 1564
                        )

                    [8] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 凭证入库冲回
                                )

                            [description] => 凭证入库冲回后查询库存凭证状态依然正常,未将库存凭证状态更改
                            [project_id] => 1375
                            [id] => 443
                        )

                    [9] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 查询打印表外凭证销号清单
                                )

                            [description] => 1、测试结果为库存凭证,而不是销号使用的凭证;2、管理柜员不能查询普通柜员销号凭证;3、管理柜员不能查询本机构的销号凭证。
                            [project_id] => 1375
                            [id] => 68
                        )

                    [10] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 查询库存现金交易测试报查找柜员尾箱定义表出错
                                )

                            [description] => FDB0001:查找柜员尾箱定义表出错,100+3401010003
                            [project_id] => 1375
                            [id] => 1
                        )

                    [11] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 证明书补打,提交后报凭证号已使用,但凭证状态时正常
                                )

                            [description] => 证明书补打,当存款证明编号为DPZM0000000025,凭证起号为1000100060000022提交报错,提示1000100060000022已被使用,查询库存凭证1000100060000022凭证状态正常
                            [project_id] => 1427
                            [id] => 2045
                        )

                    [12] => SolrObject Object
                        (
                            [summary] => Array
                                (
                                    [0] => 20054/机构查库登记查询,机构查库查询结果打印错误
                                )

                            [description] => 20054/机构查库登记查询,机构查库登记查询结果打印错误(查看详情,打印预览界面,“币种”“检查结果”列表中缺少字符)and(库存现金检查界面,”币种“列表下也缺少字符)
                            [project_id] => 1427
                            [id] => 2707
                        )

                )

        )

)


转载请注明:谷谷点程序 » php使用solr对关键词多字段查询和全文检索