import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositionVector;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.TokenSources;
import org.cnos.util.Configuration;
public class CreateIndex {
Analyzer analyzer = new StandardAnalyzer();
/**
* 创建索引
*
* @throws IOException
*/
public void index() throws IOException{
Configuration rc = new Configuration("c:/test.properties");//相对路径
File INDEX_DIR = new File(rc.getValue("indexDir"));
//如果目录不存在创建目录
if (!INDEX_DIR.exists()) {
INDEX_DIR.mkdir();
}
IndexWriter indexWriter = new IndexWriter(INDEX_DIR,analyzer,true);
Document doc1 = new Document();
doc1.add(new Field("newsSecondTitle","张营长",Store.YES,Index.TOKENIZED,TermVector.WITH_POSITIONS_OFFSETS));
indexWriter.addDocument(doc1);
Document doc2 = new Document();
doc2.add(new Field("newsSecondTitle","刘舒服",Store.YES,Index.TOKENIZED,TermVector.WITH_POSITIONS_OFFSETS));
indexWriter.addDocument(doc2);
Document doc3 = new Document();
doc3.add(new Field("newsSecondTitle","梵语里",Store.YES,Index.TOKENIZED,TermVector.WITH_POSITIONS_OFFSETS));
indexWriter.addDocument(doc3);
indexWriter.optimize();
indexWriter.close();
}
// 进行搜索
public void searcher() throws IOException{
Configuration rc = new Configuration("c:/test.properties");//相对路径
String INDEX_DIR = rc.getValue("indexDir");
IndexReader reader;
Hits hits =null;
TermQuery query ;
try {
reader = IndexReader.open(INDEX_DIR);
IndexSearcher searcher = new IndexSearcher(INDEX_DIR);
query = new TermQuery(new Term("newsSecondTitle","里"));
hits = searcher.search(query);
//高亮显示设置
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Highlighter highlighter =new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
// 这个100是指定关键字字符串的context的长度,你可以自己设定,因为不可能返回整篇正文内容
highlighter.setTextFragmenter(new SimpleFragmenter(100));
for(int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
TermPositionVector termFreqVector = (TermPositionVector)reader.getTermFreqVector(hits.id(i), "newsSecondTitle");
TokenStream tokenStream = TokenSources.getTokenStream(termFreqVector);
String result = highlighter.getBestFragment(tokenStream, doc.get("newsSecondTitle"));
System.out.println(doc.get("newsSecondTitle"));
System.out.println(result);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
CreateIndex test = new CreateIndex();
test.index();
test.searcher();
}
}
package org.cnos.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* 读取properties文件
* @author Qutr
*
*/
public class Configuration
{
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;
/** *//**
* 初始化Configuration类
*/
public Configuration()
{
propertie = new Properties();
}
/** *//**
* 初始化Configuration类
* @param filePath 要读取的配置文件的路径+名称
*/
public Configuration(String filePath)
{
propertie = new Properties();
try {
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件--->失败!");
ex.printStackTrace();
}
}//end ReadConfigInfo()
/** *//**
* 重载函数,得到key的值
* @param key 取得其值的键
* @return key的值
*/
public String getValue(String key)
{
if(propertie.containsKey(key)){
String value = propertie.getProperty(key);//得到某一属性的值
return value;
}
else
return "";
}//end getValue()
/** *//**
* 重载函数,得到key的值
* @param fileName properties文件的路径+文件名
* @param key 取得其值的键
* @return key的值
*/
public String getValue(String fileName, String key)
{
try {
String value = "";
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();
if(propertie.containsKey(key)){
value = propertie.getProperty(key);
return value;
}else
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}//end getValue()
/** *//**
* 清除properties文件中所有的key和其值
*/
public void clear()
{
propertie.clear();
}//end clear();
/** *//**
* 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,
* 当key不存在时,该key的值是value
* @param key 要存入的键
* @param value 要存入的值
*/
public void setValue(String key, String value)
{
propertie.setProperty(key, value);
}//end setValue()
/** *//**
* 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。
* @param fileName 文件路径+文件名称
* @param description 对该文件的描述
*/
public void saveFile(String fileName, String description)
{
try {
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}//end saveFile()
public static void main(String[] args)
{
Configuration rc = new Configuration("c:/test.properties");//相对路径
String ip = rc.getValue("name");//以下读取properties文件的值
String host = rc.getValue("age");
String tab = rc.getValue("tab");
rc.setValue("std", "lisi");
System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下输出properties读出的值
System.out.println("ip's length = " + ip.length());
System.out.println("host = " + host);
System.out.println("tab = " + tab);
rc.saveFile("c:/test.properties", "zhayingwrite");
}//end main()
}//end class ReadConfigInfo
#zhanghouyingwrite
#Fri Jun 27 11:07:47 CST 2008
indexDir=c:/index
age=23
name=zhanghouying
tab=spring