2010-10-30 5 views
1

J'ai développé la page de recherche en utilisant SOLR avec le conteneur de servlet Tomcat. En utilisant le code PHP, je poste la requête de recherche dans la fonction solrQuery() et dans cette fonction j'ai défini le paramètre de requête comme suit.Comment mettre en évidence les résultats de recherche en utilisant Apache SOLR avec le code PHP

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

J'ai passé mis en évidence "hl = true & hl.fl =" title paramètres. Je ne sais pas à propos de Comment analyser/affichage en surbrillance des résultats dans ma page de recherche?

peut me aider?

Répondre

3

La façon de surligner des travaux dans Solr est la suivante:

Au début de la réponse XML avec les résultats, vous voyez un nœud "résultat" avec des nœuds "doc" enfants qui contiennent vos résultats de recherche. Quelque chose comme ceci:

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

A la fin de la réponse XML avec les Resutls, vous verrez un nœud « LST » avec le nom « mettant en lumière ». Vous remarquerez qu'au sein de chaque noeud, vous verrez un noeud "lst" enfant avec le nom de l'identifiant unique que vous avez choisi pour votre document. Quelque chose comme ceci:

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

La meilleure façon pour moi de le faire est juste de traverser le nœud « résultats » d'abord et mis mes variables au contenu des résultats de la recherche en tant que tels. Ensuite, dans la boucle pour afficher chaque élément, je fais une boucle dans le nœud "surligné" et recherche l'ID de l'élément pour voir si je trouve une correspondance. Si une correspondance est trouvée, j'écraserai le contenu des variables d'origine avec le contenu en surbrillance.

De cette façon, vous allez afficher les résultats s'il y a une correspondance en surbrillance trouvée ou non.

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

         } 

        } 
       } 

Faites-moi savoir ce que vous en pensez!

+0

Merci beaucoup pour votre aimable réponse. J'ai posté pour ma réponse comme nouvelle question avec mon code. pouvez-vous s'il vous plaît veuillez le revoir. – prabu

Questions connexes