1.创建索引
public class Number001 {
public static void main(String[] args) throws IOException {
num001();
}
public static void num001() throws IOException{
FSDirectory directory = FSDirectory.open(new File("indexDIR/"));
//年分词器 对文本进行分切
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
//建立索引配置对象
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,analyzer);
//构造创建索引
IndexWriter indexWriter = new IndexWriter(directory, config);
//IndexableField intfield = new IntField("title", "王乾坤", Stored.);
IndexableField intfield=new IntField("title",1,Store.YES);
StringField StringField = new StringField("title", "王乾坤教授", Store.YES);
TextField textField = new TextField("count", "一九五二年生。一九八二年大学毕业后被分配在湖北省政府机关工作",Store.YES);
Document document = new Document();
document.add(intfield);
document.add(textField);
document.add(StringField);
indexWriter.addDocument(document);
indexWriter.close();
}
}解析
2.解析索引
/**
* 通过索引查询内容
* @throws IOException
*/
public static void getIndex() throws IOException{
FSDirectory directory =FSDirectory.open(new File("indexDIR/"));
DirectoryReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Query query=new TermQuery(new Term("count","651518854"));
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println("记录总数->"+topDocs.totalHits);
}