2.1.5.8-distributed-search.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. require(__DIR__.'/init.php');
  3. htmlHeader();
  4. // create a client instance
  5. $client = new Solarium\Client($config);
  6. // get a select query instance
  7. $query = $client->createSelect();
  8. // add distributed search settings
  9. // see http://wiki.apache.org/solr/DistributedSearch#Distributed_Search_Example for setting up two solr instances
  10. $distributedSearch = $query->getDistributedSearch();
  11. $distributedSearch->addShard('shard1', 'localhost:8983/solr');
  12. $distributedSearch->addShard('shard2', 'localhost:7574/solr');
  13. // this executes the query and returns the result
  14. $resultset = $client->select($query);
  15. // display the total number of documents found by solr
  16. echo 'NumFound: '.$resultset->getNumFound();
  17. // show documents using the resultset iterator
  18. foreach ($resultset as $document) {
  19. echo '<hr/><table>';
  20. // the documents are also iterable, to get all fields
  21. foreach ($document as $field => $value) {
  22. // this converts multivalue fields to a comma-separated string
  23. if (is_array($value)) {
  24. $value = implode(', ', $value);
  25. }
  26. echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
  27. }
  28. echo '</table>';
  29. }
  30. htmlFooter();