字段类型 |
保存 |
索引 |
记号化 |
二进制 |
Keyword |
是 |
是 |
否 |
否 |
UnIndexed |
是 |
否 |
否 |
否 |
Binary |
是 |
否 |
否 |
是 |
Text |
是 |
是 |
是 |
否 |
UnStored |
否 |
是 |
是 |
否 |
//创建索引
// Setting the second argument to TRUE creates a new index
$index = new Zend_Search_Lucene('/data/my-index', true);
$doc = new Zend_Search_Lucene_Document();
// Store document URL to identify it in search result.
$doc->addField(Zend_Search_Lucene_Field::Text('url', $docUrl));
// Index document content
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $docContent));
// Add document to the index.
$index->addDocument($doc);
// Write changes to the index.
$index->commit();
//更新索引
// Open existing index
$index = new Zend_Search_Lucene('/data/my-index');
$doc = new Zend_Search_Lucene_Document();
// Store document URL to identify it in search result.
$doc->addField(Zend_Search_Lucene_Field::Text('url', $docUrl));
// Index document content
$doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $docContent));
// Add document to the index.
$index->addDocument($doc);
// Write changes to
$index->commit();
//搜索索引
$index = new Zend_Search_Lucene('/data/my_index');
$index->find($query);
//搜索结果是一个 Zend_Search_Lucene_Search_QueryHit 对象数组
$index = new Zend_Search_Lucene('/data/my_index');
$hits = $index->find($query);
foreach ($hits as $hit) {
echo $hit->id;
echo $hit->score;
echo $hit->title;
echo $hit->author;
}
//原始的 Zend_Search_Lucene_Document 对象可以从 Zend_Search_Lucene_Search_QueryHit 获得
$index = new Zend_Search_Lucene('/data/my_index');
$hits = $index->find($query);
foreach ($hits as $hit) {
// return Zend_Search_Lucene_Document object for this hit
echo $document = $hit->getDocument();
// return a Zend_Search_Lucene_Field object
// from the Zend_Search_Lucene_Document
echo $document->getField('title');
// return the string value of the Zend_Search_Lucene_Field object
echo $document->getFieldValue('title');
// same as getFieldValue()
echo $document->title;
}
//Zend_Search_Lucene 使用和 Java Lucene 一样的评分算法。搜索结果是按照分值进行排序的。分值越大,相应的搜索结果点击排在排位越靠前。
$hits = $index->find($query);
foreach ($hits as $hit) {
echo $hit->id;
echo $hit->score;
}
//单项查询
$hits = $index->find('word1');//查询字串
//通过 API 创建查询:
$term = new Zend_Search_Lucene_Index_Term('word1');
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$hits = $index->find($query);
//多项查询
$hits = $index->find('+word1 author:word2 -word3');//查询字串
//通过 API 创建查询:
$query = new Zend_Search_Lucene_Search_Query_MultiTerm();
$query->addTerm(new Zend_Search_Lucene_Index_Term('word1'), true);
$query->addTerm(new Zend_Search_Lucene_Index_Term('word2'), null);
$query->addTerm(new Zend_Search_Lucene_Index_Term('word3'), false);
$hits = $index->find($query);
//短语查询
$query1 = new Zend_Search_Lucene_Search_Query_Phrase();
// Add 'word1' at 0 relative position.
$query1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
// Add 'word2' at 1 relative position.
$query1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
// Add 'word3' at 3 relative position.
$query1->addTerm(new Zend_Search_Lucene_Index_Term('word3'), 3);
Zend_Search_Lucene_Search_Query_Phrase([array $terms[, array $offsets[, string $field]]]);