lucene 实例2
博客专区 > 满风 的博客 > 博客详情
lucene 实例2
满风 发表于6年前
lucene 实例2
  • 发表于 6年前
  • 阅读 258
  • 收藏 1
  • 点赞 1
  • 评论 0

移动开发云端新模式探索实践 >>>   

package com.lucene;

import java.io.File;
import java.io.FileReader;
import java.util.Date;

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;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

/**
* 简单的搜索
*
* 给指定的文件夹下的文件建立索引 ,为指定的的文件创建索引 基本搜索功能
*
* @author dengyang
*
*/
public class TestIndex2 {

// static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);//内置分词器
static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);

public static void createIndexByPath(File indexDir, File dataDir) {
try {
Directory dir = new SimpleFSDirectory(indexDir);
IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_35,analyzer);
iwConf.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir, iwConf);
long startTime = new Date().getTime();
File[] files = dataDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
// System.out.println(files[i].getCanonicalPath());
// System.out.println(files[i].getName());
Document document = new Document();
document.add(new Field("path", files[i].getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("filename", files[i].getName(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("contents", new FileReader(files[i])));
indexWriter.addDocument(document);
}
}
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("创建索引功耗时:" + (endTime - startTime) / 1000 + "s");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void searchIndexByString(String searchStr, String fileName,File indexDir) {
try {
// fileName:根据某个域搜索 new MultiFieldQueryParser 可以用多个域
QueryParser queryParser = new QueryParser(Version.LUCENE_35,fileName, analyzer);
Query query = queryParser.parse(searchStr);
System.out.println("query = "+query);
IndexReader reader = IndexReader.open(new SimpleFSDirectory(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
long startTime = new Date().getTime();
TopDocs topDocs = searcher.search(query, 10);//默认的搜索方法
// searcher.setDefaultFieldSortScoring(true, false);//评分
// TopDocs topDocs = searcher.search(query, null, 20, Sort.RELEVANCE);//搜索排序 , 不需要过滤
// Filter filter = new QueryWrapperFilter(new TermQuery(new Term("filename", "stylesheet")));
// TopDocs topDocs = searcher.search(query, filter, 20, Sort.RELEVANCE);//搜索排序 , 过滤
System.out.println("共有文件:" + topDocs.totalHits);
for (int i = 0; i < topDocs.scoreDocs.length; i++) {
Document document = searcher.doc(topDocs.scoreDocs[i].doc);
System.out.println("=" + i);
System.out.println("path=" + document.get("path"));
System.out.println("filename=" + document.get("filename"));
}
reader.close();
searcher.close();
long endTime = new Date().getTime();
System.out.println("搜索功耗时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @param args
*/
public static void main(String[] args) {
File indexDir = new File("D:\\luceneIndex");
File dataDir = new File("F:\\面试与就业");

createIndexByPath(indexDir, dataDir);
// searchIndexByString("面试与就业", "path", indexDir);
searchIndexByString("面试","filename",indexDir);
}
}

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
满风
粉丝 81
博文 155
码字总数 167321
×
满风
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: