LuceneIndexNumber.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:4k
- package chapter5;
- import java.util.Date;
- import java.io.*;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.RangeQuery;
- import org.apache.lucene.search.TermQuery;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.index.Term;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.analysis.SimpleAnalyzer;
- import java.text.DecimalFormat;
- public class LuceneIndexNumber {
-
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
-
- static protected Integer[] keywords = {120,2,30};
- static protected String[] textdetail = {"数字一","数字二", "数字三"} ;
-
- /*================================================================
- * 名 称:QueryNumberIndex
- * 功 能:构造检索查询器,对指定的索引进行查询,找到指定区间值,并输出相应结果。
- ===============================================================*/
- public static void QueryNumberIndex(){
-
- try {
- IndexSearcher searcher = new IndexSearcher(Dest_Index_Path); // 生成检索器
- Term termstart = new Term("id","0006");
- Term termend = new Term("id","0105");
- RangeQuery query = new RangeQuery(termstart,termend,false); // 生成区间检索对象
- System.out.println(query.toString());
- Hits hits = searcher.search(query); // 提交检索
-
- System.out.println("Search result:");
-
- for(int i=0; i < hits.length(); i++) // 输出结果
- {
- System.out.println(hits.doc(i).getField("id").stringValue());
- }
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Search finaish");
-
- }
- /*================================================================
- * 名 称:IndexNumberBuilder
- * 功 能:构造数字类型信息的磁盘索引,添加内容到指定目录,为后续检索查询做好准备。
- ===============================================================*/
- public static void IndexNumberBuilder(){
-
- try {
-
- Analyzer TextAnalyzer = new SimpleAnalyzer(); // 生成分析器
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true); // 索引器对象
- TextIndex.setUseCompoundFile(true); // 使用符合索引
- for(int i = 0; i < 3 ; i++){
- Document document = new Document(); // 空文档
- DecimalFormat df = new DecimalFormat("0000"); // 数字化格式
- String idkey = df.format(keywords[i]); // 数字关键字转换成定长字符串
-
- Field field_id = new Field("id", idkey, // 添加关键字域
- Field.Store.YES,Field.Index.UN_TOKENIZED);
- System.out.println( idkey );
-
- document.add(field_id);
- Field field_content = new Field("content", textdetail[i], // 添加内容域
- Field.Store.YES,Field.Index.TOKENIZED);
-
- document.add(field_content);
-
- TextIndex.addDocument(document);
- }
- TextIndex.optimize();
- TextIndex.close();
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("Index success");
- }
-
- /*================================================================
- * 名 称:main
- * 功 能:测试Lucene对数字类型内容的索引建立和检索查询功能。
- ===============================================================*/
- public static void main(String[] args) {
-
- IndexNumberBuilder(); // 创建索引
- QueryNumberIndex(); // 检索内容
- }
- }