A passion for web development

Share

Category
Articles

Yacy Solr search engine client in symfony 2

This is YACY client  to access the YACY server and retrieving the results via RSS feed, We first access the url and get the response then we process the response in an xml element, iterating through the loop to get the details we need. Note that the code here is for symfony but with little modification it can work on any php based application.

The code bellow is an improved version to the one in the video. The start index is 0 and first and second page exception are handled. Please feel free to improve the code and let me know of the results.

The following is the main action that displays the search page it is called the searchAction()

    public function searchAction(Request $request)
    {
        $page_request = $request->query->get('page');
        $results = "";
        $record = 0;
        $limit = 5;   
        $pages = "";
        $end = $limit;
        $query_string = $request->query->get('search-input');
        $query = str_replace(" ", "+", $query_string);
           if($query=="a"){
              $query = "nothing";
           }
           
           if($page_request){
               
            /**
             * We get th page request which is the page number
             * We multibly the page number with the search limit per page
             *In order to get the index record we then take away the limit
             * from the result. The result in this case is $end.
             * $end is the heighest value of the page returned result
             * and therefor record is the start index in the new page.
             * For example page 3 will give us $end = 3 * 5 = 15
             * therefore the start $record = 15 - 5 = 10 + 1 = 11
             * Hence the start item is 11 and the end item is 15
             */
              $end = $page_request * $limit;
              $record = $end - $limit + 1;
              
              if($page_request == 2){
                   $record = $end - $limit;
              }
              
           }
           if(($page_request == 1) || (!isset($page_request))){
               $record = $end - $limit;
               $end = $limit - 1;
           }
   
           //$response is the searchpage() returned response. Check the function searchpage()
           //for more details
           
        $response = $this->searchpage($query, $limit, $record);

        $xml= new \SimpleXMLElement($response);
        foreach($xml->children() as $item){
             $items = $item->item;
             $count = $item->item->count();
             $dc = $item->children('http://a9.com/-/spec/opensearch/1.1/');
             $allResults = $dc->totalResults;
     
                foreach($items as $result){
          
                  $title = $result->title;
                  $link = $result->link;
                  $description = $result->description;
                  $results .= "<div class='search-results'><a href=\"$link\">"
                           . "<h3>$title</h3></a><p>$description</p>"
                           . "</div>";
                }
        }   
 
        $pagination = $allResults/$limit;
        if($pagination > 1){
         // here we check if the division doesn't have reminder, if it does then we
         // add a page to the pagination to cater for the reminder items.
             if($allResults % $limit !=0){
             $pagination = $pagination + 1;
             }
         
             $pages = $pagination;
        }
        
        return $this->render('PagesBundle:page:search-results.html.twig', array(
            'response'=> $results,
            'limit'=>$count,
            'all' => $allResults,
            'pages'=> $pages,
            'end' => $end,
            'record'=>$record,
            'searchTerm'=> $query,));
    }
   

 

 

 

This is the RSS reader

 public function searchpage($query, $limit, $record){
        
           $YaCyURL="http://symvc.com:8090/";  
  $cu=$YaCyURL."yacysearch.rss?query=$query&maximumRecords=$limit&startRecord=$record";
  $queryServer = curl_init($cu);     
  curl_setopt($queryServer, CURLOPT_HEADER, 0);
  curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
  $response= curl_exec($queryServer);
 
  curl_close($queryServer);
  return $response;
    }
   

The following is the search page twig file

search-results.html.twig

{% extends 'base.html.twig' %}
{% block body %}
    
   <div id="main" class="container">
       <div id="search-form">
       {% include 'PagesBundle:page:nav-search.html.twig' %}
       </div>
       <div style="position:relative; float: left;">
           {{ response|raw }}
           
           <div style="text-align:center;">
               
           {% if end > all %}
                   {% set end = all %}
            {% endif %}
       
                   <small><div style=" margin-top:50px;">Limit {{ limit }}</div>
           {% if limit > 1 %}
               
                <div>Records {{ record }} - {{ end }} </div>
               {% endif %}
           <div>Found records {{ all }}</div>
           
          
           <div>Search: {{ searchTerm }}</div>
                   </small>
       </div>
           <div style="background: #cae1ae; padding: 5px; margin-top: 20px; text-align: center;">
               {% if pages > 1 %}
               {% for i in 1..pages %}
                   <a href="?search-input={{searchTerm}}&submit-search=Search&page={{ i }}">{{i}}</a>
               {% endfor %}
               {% endif %}
           </div>
       </div>
       
      
    </div>
   
     
 
{% endblock %}    
    
   

the following is the search form twig file. The form can be created in the twig or in a form type format. It is better to develop the form in the form type rather plain html form in the twig file. The following file is included in the search form. Make sure to include it if you want or remove the include in the search-results.html.twig in the previous section.

search-nav.html.twig

<form action="{{ path('pages_search') }}" method="get">
    <input type="text" id="search-input" name="search-input" min="2">
    <input type="submit" id="submit-search" name="submit-search" value="Search">
</form> 

 

And finally the routing

pages_search:
    path:     /search
    defaults: { _controller: PagesBundle:Page:search } 

Watch this video

Contact
01902 637 708