如何使用 Apache SOLR 和 PHP 代码突出显示搜索结果 [英] How to Highlighting Search Results Using Apache SOLR with PHP code

查看:32
本文介绍了如何使用 Apache SOLR 和 PHP 代码突出显示搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SOLR 和 Tomcat servlet 容器开发了搜索页面.我使用 PHP 代码将搜索查询发布到 solrQuery() 函数中,并在此函数中定义了如下查询参数.

I developed search page using SOLR with Tomcat servlet container. Using PHP code I post the search query into solrQuery() function and In this function have defined query parameter as follows.

$query = "?q=".trim(urlencode($q))&version=2.2&start=0&rows=10&indent=on&hl=true&hl.fl=title";

$query = "?q=".trim(urlencode($q)) &version=2.2&start=0&rows=10&indent=on&hl=true&hl.fl=title";

我传递了突出显示的hl=true&hl.fl=title"参数.我不知道如何在我的搜索页面中解析/显示突出显示的结果?

I have passed highlighted "hl=true&hl.fl=title" parameters. I dont know about How to parse/display highlighted results in my search page?

有人可以帮我吗?

推荐答案

Solr 中突出显示的工作方式如下:

The way highlighting works in Solr is the following:

在带有结果的 XML 响应开始时,您会看到一个结果"节点和包含您的搜索结果的子文档"节点.像这样:

At the beggining of the XML response with the results, you see a "result" node with child "doc" nodes which contain your search results. Something like this:

 <doc>
  <str name="body">Merge transfer will merge one item with another. The new item can be either from same location or from different location. Merge transfer directions:  Open your Paper Tiger Online and select the database. Select item(s) you want to merge except for the one you want to merge the items into, click on Transfer and select Merge. A pop up will be opened asking New Location, once you select the location, the items in the location will be loaded in to the "File to Merge".  Select a file in the "File to Merge" list. Choose whether you want to be reminded to move this file or not  Click Merge File  Add any additional keywords, notes, action date, change category (if necessary)  Click Merge Item button  If you chose to be reminded, you'll need to click the Confirm box to confirm the merge, then the merge will happen. (You can also cancel the merge from the Confirm page) </str>
  <str name="current-tags"/>
  <str name="id">141156</str>
  <str name="title">What is a merge transfer? How do I merge files?</str>
 </doc>

在带有结果的 XML 响应的末尾,您将看到一个名为highlighting"的lst"节点.您会注意到,在每个节点中,您会看到一个子lst"节点,其中包含您为文档选择的唯一标识符的名称.像这样:

At the end of the XML response with the resutls, you will see a "lst" node with the name "highlighting". You will notice that within each node, you'll see a child "lst" node with the name of the unique identifier that you've chosen for your document. Something like this:

<lst name="141154">
  <arr name="body">
    <str>Transfers are used to move or &lt;em&gt;merge&lt;/em&gt; the items from one location and another location and creating duplicates items in the locations. You might want to move and item from Action to Reference or Archive to an off-site location. You would want to move the item in Paper Tiger to ensure you can find it</str>
  </arr>
</lst>

对我来说,最简单的方法就是先遍历结果"节点,然后将我的变量设置为搜索结果的内容.然后,在显示每个项目的循环中,我遍历突出显示"节点并搜索项目的 ID 以查看是否找到匹配项.如果找到匹配项,我将用突出显示的内容覆盖原始变量的内容.

The easiest way for me to do it is just to traverse the "results" node first and set my variables to the content of the search results as such. Then, within the loop to display each item, I loop through the "highlighted" node and search for the item's ID to see if I find a match. If a match is found, I will overwrite the content of the original variables with highlighted content.

这样,无论是否找到突出显示的匹配项,您都会显示结果.

This way, you will display results wether there is a highlighted match found or not.

    cURL_address($curl_url);

$xml = new SimpleXMLElement($data);

    foreach ($xml->children() as $node) {

        $arr = $node->attributes();   // returns an array
        $no_results = FALSE;

        //When no results are found

        if ($arr["name"] == "response" && $arr["numFound"] == 0) {

            echo "No results found for '". $query ."'";
            $no_results = TRUE;

        }

        if ($arr["name"] == "response") {

            if ($no_results != TRUE) {
                echo "<h4 id="search_results">Search results for '".$query."'</h4>";
            } 

            foreach ($node->doc as $response) {

                //Initially set all the variables to the non-highlighted content

                $entry_title = $response->str[3];
                $entry_body = substr($response->str[0], 0, 300)."&#8230;";
                $entry_id = $response->str[2];
                $entry_tags = $response->str[1];

                //logic to see if we need to add ellipsis to start/end of body
                $orig_body_beggining = substr($response->str[0], 0, 10);
                $orig_body_end = substr($response->str[0], -10);

                //Now loop through every highlighted field to see if we have a node that matches the original item's ID

                foreach ($xml->lst[1]->lst as $hl_data) {

                    $arr2 = $hl_data->attributes();   // returns an array

                    $hl_arr = $arr2["name"];

                    if ((string)$entry_id == (string)$hl_arr) {

                        foreach ($hl_data->arr as $hl_content) {

                            $arr3 = $hl_content->attributes();

                            //Use a switch to overwrite existing variables if a new one exists that is highlighted

                            switch($arr3['name']) { // Get attributes as element indices
                                case 'body':
                                    $f_ellip = NULL;
                                    $l_ellip = NULL;
                                    if ($orig_body_beggining != substr((string)$hl_content->str, 0, 10)) {
                                        $f_ellip = "&#8230; ";
                                    }
                                    if ($orig_body_end != substr((string)$hl_content->str, 0, -10)) {
                                        $l_ellip = " &#8230;";
                                    }
                                    $entry_body = $f_ellip.(string)$hl_content->str.$l_ellip;
                                    break;
                                case 'title':
                                    $entry_title = (string)$hl_content->str;
                                    break;
                                case 'current-tags':
                                    $entry_tags = (string)$hl_content->str;
                                    break;
                                }

                        }

                    }
                }

告诉我你的想法!

这篇关于如何使用 Apache SOLR 和 PHP 代码突出显示搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆