簡體   English   中英

如何使用Apache SOLR和PHP代碼突出顯示搜索結果

[英]How to Highlighting Search Results Using Apache SOLR with PHP code

我使用帶有Tomcat Servlet容器的SOLR開發了搜索頁面。 使用PHP代碼,我將搜索查詢發布到solrQuery()函數中,並在此函數中定義了如下查詢參數。

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

我已通過突出顯示的“ hl = true&hl.fl = title”參數。 我不知道如何在搜索頁中解析/顯示突出顯示的結果?

誰能幫我?

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

在以結果開始XML響應時,您會看到一個帶有子“ doc”節點的“結果”節點,其中包含您的搜索結果。 像這樣:

 <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”節點,其名稱是您為文檔選擇的唯一標識符的名稱。 像這樣:

<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,以查看是否找到匹配項。 如果找到匹配項,我將用突出顯示的內容覆蓋原始變量的內容。

這樣,無論是否找到突出顯示的匹配項,您都將顯示結果。

    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;
                                }

                        }

                    }
                }

讓我知道你的想法!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM