PHP code example of lionear / solr-client

1. Go to this page and download the library: Download lionear/solr-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lionear / solr-client example snippets


$client = new SolrClient('localhost', '8983');
$client->setCore('gettingstarted');

// Adding document
$client->add(
    new SolrDocument([
        'id' => 1,
        'title_s' => 'Title'
    ]);
);
$client->commit();

// Searching document
$result = $client->query('title_s:title');
echo $result->getNumFound(); // 1
foreach ($result->getDocs() as $doc) {
    echo $doc->title_s; // 'Title'
}

// Getting a single document
$doc = $client->get(1);
echo $doc->title_s; // 'Title'

// Search with custom parameters
$result = $client->search([
    'q' => '+title:fish -description:shark',
    'rows' => 15,
    'start' => 0
]);

// Pagination Support
$nextPage = $result->next(15, $client);

// Facet support
$result = $solr->setFacet('subject')->query('*:*');
$subjectFacet = $result->getFacet('subject');

// autocommit enabled to commit after every add
$doc = new SolrDocument;
$doc->id = 2;
$doc->title_s = "Second Document";
$doc->description_s = "Some description";
$client->add($doc);

// delete by id field
$client->remove(2);

// delete by query
$client->removeByQuery('id:2');

// search for all documents that has a subject field match aquatic, by 10 at a time
$search = [
    'q' => 'subject:aquatic'
];

while($payload->getNextCursorMark() != $payload->getCursorMark()) {
   $payload = $client->cursor($payload->getNextCursorMark(), 10, $search);
   print_r($payload);
}

composer