在php中尝试Solr时出现了一个问题
我在PHP中运行示例时遇到一个问题,代码如下:在php中尝试Solr时出现了一个问题,php,solr,Php,Solr,我在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-
<?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();
}
}
?>
有什么问题吗
非常感谢您的帮助。您可能(a)查询语法错误,或者(b)索引配置错误
检查您的索引
看起来您有零件号、型号等字段。您需要确保这些字段已在中配置。我可能会猜测您并没有在那个里创建它们,所以您的文档提交失败了,或者您有一个模型字段,但它不是多值的
如果它通过了文档提交,那么您可能会遇到查询问题
调试查询
调试查询的最简单方法是在发送查询之前回显查询。在这种情况下:
echo $query
从中获取输出并点击:
http://localhost:8983/solr/select?q=<query>
http://localhost:8983/solr/select?q=
(其中是来自echo语句的查询)
您可能会得到一个400错误。我猜您的字段名不好,或者在查询中使用了无效语法。如果您可以发布$query,将更容易帮助解决此问题。非常感谢!问题是配置错误的索引,我是Solr的新手。这里也一样,谢谢。Solr也是新手,看起来我正在尝试添加默认“demo”索引中不存在的字段。对我来说,它插入数据,但在
$Solr->optimize()处引发异常代码>
http://localhost:8983/solr/select?q=<query>