Lucence 4.6 实例代码

版本: 4.6


参考了网上相上的相关内容!先谢过了


单独新建一个类,引用Lucence 4.6的相关JAR包:


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Lucence test class 20150412
 * 
 * @author Wang
 * 
 */
public class Test {

    public class Constants {
	public final static String INDEX_STORE_PATH = "F:\\lucene\\index"; // 索引的存放位置
    }

    /**
     * 创建索引
     * 
     * @param analyzer
     * @throws Exception
     */
    public static void createIndex(Analyzer analyzer) throws Exception {
	Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
	IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
		analyzer);
	IndexWriter iw = new IndexWriter(dire, iwc);
	Test.addDoc(iw);
	iw.close();
    }

    /**
     * 动态添加Document
     * 
     * @param iw
     * @throws Exception
     */
    public static void addDoc(IndexWriter iw) throws Exception {

	Document doc = new Document();
	doc.add(new TextField("content", "我爱祖国", Store.YES));
	iw.addDocument(doc);

	doc = new Document();
	doc.add(new TextField("content", "我也爱你", Store.YES));
	iw.addDocument(doc);

	iw.commit();
    }

    /**
     * 搜索
     * 
     * @param query
     * @throws Exception
     */
    private static void search(Query query) throws Exception {
	Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
	IndexReader ir = DirectoryReader.open(dire);
	IndexSearcher is = new IndexSearcher(ir);
	TopDocs td = is.search(query, 1000);
	System.out.println("共为您查找到" + td.totalHits + "条结果");
	ScoreDoc[] sds = td.scoreDocs;
	for (ScoreDoc sd : sds) {
	    Document d = is.doc(sd.doc);
	    System.out
		    .println(d.get("content") + ":[" + d.get("content") + "]");
	}
    }

    public static void main(String[] args) throws Exception, Exception {
	Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
	Test.createIndex(analyzer);
	QueryParser parser = new QueryParser(Version.LUCENE_46, "content",
		analyzer);
	Query query = parser.parse("我");
	Test.search(query);

	Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
	IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,
		analyzer);
	IndexWriter iw = new IndexWriter(dire, iwc);
	iw.deleteDocuments(query);
	iw.commit();
	iw.close();
    }
}


以下代码用来删除已有的索引:

Directory dire = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));  
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_46,  
        analyzer);  
    IndexWriter iw = new IndexWriter(dire, iwc);  
    iw.deleteDocuments(query);  
    iw.commit();  
    iw.close();  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值