import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.analysis.ComplexAnalyzer;
public class IndexUtils {
private static Directory directory=null;
static{
try {
directory=FSDirectory.open(new File("E:/lucene/files"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Directory getDirectory() {
return directory;
}
/**
* 创建索引
*/
public static void createIndex(){
IndexWriter writer=null;
File file=new File("E:/lucene/resource");
Document document=null;
try {
//创建IndexWriter 使用中文分词器
Dictionary dic=Dictionary.getInstance(new File("F:/官方包/lucene-3.5.0/mmseg4j-1.8.5/data"));
writer=new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35,new ComplexAnalyzer(dic)));
for(File f:file.listFiles()){
document=new Document();
document.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("content",new FileReader(f)));
document.add(new NumericField("date", Field.Store.YES, true).setLongValue(f.lastModified()));
document.add(new NumericField("size", Field.Store.YES, false).setLongValue(f.length()/1000));
writer.addDocument(document);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
######################
package com.lucene.search;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
public class SearchUtils {
// 定义IndexReader,并使用静态块加载IndexReader
private static IndexReader reader = null;
static {
try {
reader = IndexReader.open(IndexUtils.getDirectory());
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取IndexSearcher
private IndexSearcher getSearcher() {
try {
if (reader == null) {
reader = IndexReader.open(IndexUtils.getDirectory());
} else {
IndexReader ir = IndexReader.openIfChanged(reader);
if (ir != null) {
reader.close();
reader = ir;
}
}
return new IndexSearcher(reader);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 排序查询
*
* @param querystr
* 查找匹配的字符串
* @param domain
* 查找内容的域
* @param sort
* 排序方式
*/
public void SearchBySort(String querystr, String domain, Sort sort) {
TopDocs docs = null;
IndexSearcher searcher = this.getSearcher();
try {
QueryParser parser = new QueryParser(Version.LUCENE_35, domain,
new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse(querystr);
if (sort == null) {
docs = searcher.search(query, 150);
} else {
docs = searcher.search(query, 150, sort);
}
// 输出信息
ScoreDoc[] sds = docs.scoreDocs;
Document d = null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
for (ScoreDoc s : sds) {
d = searcher.doc(s.doc);
System.out.println(s.doc+"->"
+s.score+"->"
+d.get("filename")+"->"
+d.get("size")+"->"
+sdf.format(new Date(Long.valueOf(d.get("date")))));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
############################
package com.lucene.test;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.junit.Before;
import org.junit.Test;
import com.lucene.search.SearchUtils;
public class TestSortSearch {
private SearchUtils su=null;
@Before
public void init(){
su=new SearchUtils();
}
@Test
public void testSortSearch(){
//无排序搜索,默认根据评分降序排序
//su.SearchBySort("中国", "content", null);
//通过doc的id进行排序
//su.SearchBySort("中国", "content", Sort.INDEXORDER);
//通过评分进行排序
//su.SearchBySort("中国", "content", Sort.RELEVANCE);
//根据SortField设置属性对filename进行升序排序
//su.SearchBySort("中国", "content", new Sort(new SortField("filename", SortField.STRING)));
//通过根据SortField设置最后一个属性进行降序排序
su.SearchBySort("中国", "content", new Sort(new SortField("filename", SortField.STRING,true)));
}
}