LuceneDefaultAnalyzerText.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:1k
- package chapter8;
- import java.io.IOException;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.SimpleAnalyzer;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.Token;
- import java.util.*;
- import java.io.*;
- public class LuceneDefaultAnalyzerText {
-
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
- static protected String textdetail = "Lucene是一个非常有效的开发工具,它可以实现全文检索功能。" ;
-
- public static void main(String[] args) {
- try {
- Analyzer TextAnalyzer = new SimpleAnalyzer();
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
- Document document = new Document();
- Field field_content = new Field("content", textdetail,
- Field.Store.YES,Field.Index.TOKENIZED);
- document.add(field_content);
- TextIndex.addDocument(document);
-
- ArrayList ItemList = new ArrayList();
-
- TokenStream stream = TextAnalyzer.tokenStream("content", new StringReader(textdetail));
- while(true)
- {
- Token item = stream.next();
- if(null == item ) break;
- System.out.print("{"+item.termText()+"} ");
- }
- TextIndex.optimize();
- TextIndex.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("");
- System.out.println("Index success");
- }
- }