当我在 php 中尝试 Solr 时出现问题

发布于 2024-11-13 02:44:42 字数 3264 浏览 2 评论 0 原文

当我在 PHP 中运行示例时遇到问题,代码如下:

<?php
  require_once( 'SolrPhpClient/Apache/Solr/Service.php' );

  // 
  // 
  // Try to connect to the named server, port, and url
  // 
  $solr = new Apache_Solr_Service( 'localhost', '8983', '/solr/' );

  if ( ! $solr->ping() ) {
    echo 'Solr service not responding.';
    exit;
  }

  //
  //
  // Create two documents to represent two auto parts.
  // In practice, documents would likely be assembled from a 
  //   database query. 
  //
  $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 Apache_Solr_Document();

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

    $documents[] = $part;
  }

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

  //
  // 
  // Run some queries. Provide the raw path, a starting offset
  //   for result documents, and the maximum number of result
  //   documents to return. You can also use a fourth parameter
  //   to control how results are sorted and highlighted, 
  //   among other options.
  //
  $offset = 0;
  $limit = 10;

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

  foreach ( $queries as $query ) {
    $response = $solr->search( $query, $offset, $limit );

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

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

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

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

但是一旦运行,就会显示以下错误:

'400' Status: Bad Request
Fatal error: Uncaught exception 'Apache_Solr_HttpTransportException' with message ''400' Status: Bad Request' in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php:338 Stack trace: #0 C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php(1170): Apache_Solr_Service->_sendRawGet('http://localhos...') #1 C:\software\study\php\xampp\xampp\htdocs\solr1.php(98): Apache_Solr_Service->search('partno: 1 OR pa...', 0, 10) #2 {main} thrown in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php on line 338

可能是什么问题。

感谢所有帮助。

I have a problem when I run an example in PHP, the code is below:

<?php
  require_once( 'SolrPhpClient/Apache/Solr/Service.php' );

  // 
  // 
  // Try to connect to the named server, port, and url
  // 
  $solr = new Apache_Solr_Service( 'localhost', '8983', '/solr/' );

  if ( ! $solr->ping() ) {
    echo 'Solr service not responding.';
    exit;
  }

  //
  //
  // Create two documents to represent two auto parts.
  // In practice, documents would likely be assembled from a 
  //   database query. 
  //
  $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 Apache_Solr_Document();

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

    $documents[] = $part;
  }

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

  //
  // 
  // Run some queries. Provide the raw path, a starting offset
  //   for result documents, and the maximum number of result
  //   documents to return. You can also use a fourth parameter
  //   to control how results are sorted and highlighted, 
  //   among other options.
  //
  $offset = 0;
  $limit = 10;

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

  foreach ( $queries as $query ) {
    $response = $solr->search( $query, $offset, $limit );

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

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

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

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

But once run the following error is shown:

'400' Status: Bad Request
Fatal error: Uncaught exception 'Apache_Solr_HttpTransportException' with message ''400' Status: Bad Request' in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php:338 Stack trace: #0 C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php(1170): Apache_Solr_Service->_sendRawGet('http://localhos...') #1 C:\software\study\php\xampp\xampp\htdocs\solr1.php(98): Apache_Solr_Service->search('partno: 1 OR pa...', 0, 10) #2 {main} thrown in C:\software\study\php\xampp\xampp\htdocs\SolrPhpClient\Apache\Solr\Service.php on line 338

What could be the problem.

All help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-11-20 02:44:42

您可能有 (a) 错误的查询语法,或 (b) 索引配置错误。

检查您的索引

看起来您有零件编号、型号等字段。您需要确保这些字段已在您的 schema.xml。我可能猜测您没有在那里创建它们,因此您的文档提交失败,或者您有一个模型字段,但它不是多值的。

如果它通过了文档提交,那么您可能会遇到查询问题...

调试查询

调试查询的最简单方法是在发送查询之前回显查询。在本例中:

 echo $query

获取输出并点击:(

 http://localhost:8983/solr/select?q=<query>

其中是来自 echo 语句的查询)。

您可能会收到 400 错误。我的猜测是您在某个地方有一个错误的字段名称,或者在查询中使用了无效的语法。如果您可以发布 $query,它将更容易帮助解决问题。

You likely have either (a) bad query syntax, or (b) a misconfigured index.

Checking Your Index

Looks like you have fields for partno, model, etc. You need to make sure these are configured in your schema.xml. I might guess that you don't have them created there and so your document submission is failing, or that you have a field for model but it is not multivalued.

If its getting past document submission, then you may have a querying issue...

Debugging a Query

The easiest way to debug your query is to echo the query before you send it. In this case:

 echo $query

Take the output from that and hit:

 http://localhost:8983/solr/select?q=<query>

(where is the query from the echo statement).

You will likely get a 400 error. My guess is you have a bad field name in there somewhere or are using invalid syntax in your query. If you can post $query it would make it easier to help with the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文