1

以下是我的代码,它使用 Solr 添加文档并触发查询。

它显示一个错误:

SolrInputDocument number 1 is not a valid SolrInputDocument instance
Catchable fatal error: Argument 1 passed to SolrClient::query() must be an instance of SolrParams, string given in /var/www/ps/test.php on line 85


<?php
require_once( 'bootstrap.php' );
$options = array( 'hostname' => SOLR_SERVER_HOSTNAME );
$client = new SolrClient($options);
  // Create two documents to represent two auto parts.
  $parts = array(
    'spark_plug' => array(
      'partno' => 1,
      'name' => 'Spark plug',
      'model' => array( 'Boxster', '924' ),
      'year' => array( 1999, 2000 ),
      'price' => 25.00,
      'inStock' => true,
    ),
    'windshield' => array(
      'partno' => 2,
      'name' => 'Windshield',
      'model' => '911',
      'year' => array( 1999, 2000 ),
      'price' => 15.00,
      'inStock' => false,
    )
  );

  $documents = array();

  foreach ( $parts as $item => $fields ) {
    $part = new SolrDocument();

    foreach ( $fields as $key => $value ) {
      if ( is_array( $value ) ) {
        foreach ( $value as $datum ) {
          $part->addField( $key, $datum );
        }
      }
      else {
        $part->$key = $value;
      }
    }

    $documents[] = $part;
  }

  // Load the documents into the index
  try {
    $client->addDocuments( $documents );
    $client->commit();
    $client->optimize();
  }
  catch ( Exception $e ) {
    echo $e->getMessage();
  }

  // Run some queries. 
  $offset = 0;
  $limit = 10;

  $queries = array(
    'partno: 1 OR partno: 2',
    'model: Boxster',
    'name: plug'
  );

  foreach ( $queries as $query ) {
    $queryResponse = $client->query($query);

    if ( $queryResponse->getHttpStatus() == 200 ) { 
      // print_r( $response->getRawResponse() );

      if ( $queryResponse->response->numFound > 0 ) {
        echo "$query <br />";

        foreach ( $queryResponse->response->docs as $doc ) { 
          echo "$doc->partno $doc->name <br />";
        }

        echo '<br />';
      }
    }
    else {
      echo $queryResponse->getHttpStatusMessage();
    }
  }
?>
4

0 回答 0