我正在尝试使用solr PHP Client。当我尝试添加文档时,它返回400状态。我不知道我做错了什么,我是Solr的新手。下面是我的代码和schema.xml文件。
我正在使用solr 4.10.3和tomcat 7
提前致谢
$solr = new Apache_Solr_Service('localhost', '8080', '/solr/#/core0');
if (!$solr->ping()) {
echo 'Solr service not responding.';
exit;
} else {
$fileContent = $_GET['fileContent'];
$title = trim($_GET['title']);
$desription = $_GET['description'];
$fileId = trim($_GET['fileId']);
$docs = array(
'doc1' => array(
'id' => 1,
'fileid' => 'file1',
'title' => 'title1'
));
$documents = array();
$part = new Apache_Solr_Document();
foreach ($docs as $item => $fields) {
foreach ($fields as $key => $value) {
if (is_array($value)) {
foreach ($value as $data) {
$part->setMultiValue($key, $data);
}
}
else {
$part->$key = $value;
}
}
$documents[] = $part;
}
// Load the documents into the index
try {
$a = $solr->addDocuments($documents);
$solr->commit();
$solr->optimize();
} catch (Exception $e) {
echo $e->getMessage();
}
die;
$offset = 0;
$limit = 10;
$queries = array('id:'.$fileId);
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->id $doc->title <br />";
}
echo '<br />';
}
} else {
echo $response->getHttpStatusMessage();
}
}
}
}
schema.xml中
<schema name="example core zero" version="1.1">
<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="id" type="long" indexed="true" stored="true"/>
<field name="fileid" type="string" indexed="true" stored="true" required="true"/>
<field name="title" type="string" indexed="true" stored="true" />
<uniqueKey>id</uniqueKey>
</schema>
答案 0 :(得分:0)
至少有一个问题是:
Apache_Solr_Service中的参数:/ solr /#/ core0应为:
/ solr的/ CORE0