Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetching data from Solr using Solarium

    • 0
    • 2
    • 3
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.97k
    Comment on it

    We seldom need to fetch result from solr based on limit and order by clause similar to sql queries , the same thing can be achieved using the below given code packet with solarium :

    // create a client instance
    $client = new Solarium\Client($config);
    
    // get a select query instance
    $query = $client->createSelect();
    // set a query (all prices starting from 12)
    $limit=10;
    $start=0;
    $search_for="*";
    if(isset($_GET["search_for"])){
     $search_for=$_GET["search_for"];
    }
    $query->setQuery('book:'.$search_for);
    
    if(isset($_GET["start"])){
     $start=$_GET["start"];
    }
    // set start and rows param (comparable to SQL limit) using fluent interface
    $query->setStart($start)->setRows($limit);
    
    // set fields to fetch (this overrides the default setting 'all fields')
    $query->setFields(array('id','name','description_book','isbn','isbn13','eisbn10','eisbn13','authors','publishers','image_book','listprice_book','rentalprice_book','semesterrentalprice_book','producturl_book','inventoryflag'));
    
    // sort the results by price ascending
    $query->addSort('inventoryflag', $query::SORT_DESC);
    
    // this executes the query and returns the result
    try{
        $resultset = $client->select($query);
        // display the total number of documents found by solr
        echo 'NumFound: '.$resultset->getNumFound();
    
        // show documents using the resultset iterator
            foreach ($resultset as $document) {
                //print_r($document);
    
                echo '
    '; // the documents are also iterable, to get all fields foreach ($document as $field => $value) { // this converts multivalue fields to a comma-separated string if (is_array($value)) { $value = implode(', ', $value); } echo ''; } echo '
    ' . $field . '' . $value . '
    '; } } //catch exception catch(Exception $e){ echo 'Message: ' .$e->getMessage(); }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: