<?php
require_once 'Zend/Controller/Action.php';
class Admin_SearchController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$search = $this->getRequest()->getParam('keywords');
		$article=new Article();
		$totalRows = $article->fetchAll()->toArray(); 
		
		//echo '<pre>';
		//print_r($totalRows);
		//echo '</pre>';
		
		$index = Zend_Search_Lucene::create('data/my-index');
		
		$stopWords = array('a', 'an', 'at', 'the', 'and', 'or', 'is', 'am'); 
		
        $stopWordsFilter = new Zend_Search_Lucene_Analysis_TokenFilter_StopWords($stopWords);
        
		$analyzer=new Zend_Search_Lucene_Analysis_Analyzer_Common_Myanlalysis();
		
        $cnStopWords = array("的","得","地","了","啊"); 
        $analyzer->setCnStopWords($cnStopWords);
        $analyzer->addFilter($stopWordsFilter); 
		
		$analyzer->addFilter($stopWordsFilter); 
		
		Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer); //根据你自己需要配置字符集定的位<导致>报}错是这)原因
		
		foreach($totalRows as $Rows){
			
		$doc = new Zend_Search_Lucene_Document();
		
		$doc->addField(Zend_Search_Lucene_Field::UnIndexed('key', md5($Rows['id']),'utf-8'));
		
        //$search = array(",", "/", "\\", ".", ";", ":", '"', "!", "~", "`", "^", "(", ")", "?", "-", "t", "n", "'", "<", ">", "r", "rn", "$", "&", "%", "#", "@", "+", "=", "{", "}", "[", "]", "：", "）", "（", "．", "。", "，", "！", "；", "“", "”", "‘", "’", "［", "］", "、", "—", "　", "《", "》", "－", "…", "【", "】",);
 
		$doc->addField(Zend_Search_Lucene_Field::Text('title',$Rows['content'],'utf-8'));
		
		$doc->addField(Zend_Search_Lucene_Field::Text('content',$Rows['title'],'utf-8'));
		
		$index->addDocument($doc);
		
		}

		$index->optimize();
		// Write changes to the index.
		$index->commit();
		
		
		echo $search = $this->getRequest()->getParam('keywords');
		$index = Zend_Search_Lucene::open('data/my-index');
	
		Zend_Search_Lucene_Analysis_Analyzer::setDefault($analyzer); //根据你自己需要配置字符集
	
	echo "Index contains {$index->count()} documents.\n";
	
	$query=Zend_Search_Lucene_Search_QueryParser::parse($search,'utf-8');

	$hits = $index->find($query);
	//var_dump($hits);
	echo "Search for \"$search\" returned " .count($hits). " hits.\n\n";
	//$query->highlightMatches(iconv('UTF-8', 'ASCII//TRANSLIT', $content)); 
	foreach ($hits as $hit) {
	echo str_repeat('-', 80) . "\n";
	echo 'ID: ' .$hit->title."\n";
	echo 'Score: ' . sprintf('%.2f', $hit->score) ."\n\n";
	foreach ($hit->getDocument()->getFieldNames() as $field) {
	echo "$field: \n";
	echo ' ' . trim(substr($hit->$field,0,76)) . "\n";
	}
	}	
	
	 
		
	}
}