全文搜索-Lucene


package com.lucene.helloworld;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
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.junit.Test;

import com.lucene.utils.LuceneUtils;

public class HelloWorld {
private String filePath="F:\\tasks\\workspace2\\LucenePro\\luceneDatasource\\jincm_中国.txt";
private String indexPath="F:\\tasks\\workspace2\\LucenePro\\luceneIndex";
private Analyzer analyzer=new StandardAnalyzer();
@Test
public void createIndex() throws Exception{
Document doc=LuceneUtils.file2Document(filePath);
IndexWriter indexWriter=new IndexWriter(indexPath, analyzer,true,MaxFieldLength.LIMITED);
indexWriter.addDocument(doc);
indexWriter.close();
}

@Test
public void search()throws Exception {
String queryString="jincm";
String[] fields={"name","content"};
QueryParser queryParser=new MultiFieldQueryParser(fields, analyzer);
Query query=queryParser.parse(queryString);
IndexSearcher indexSearcher=new IndexSearcher(indexPath);
Filter filter=null;
TopDocs topDocs=indexSearcher.search(query,filter,10000);
System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果");
for(ScoreDoc scoreDoc:topDocs.scoreDocs){
int docIndex=scoreDoc.doc;
Document doc=indexSearcher.doc(docIndex);
System.out.println("<------------------------------");
System.out.println(doc.get("name"));
System.out.println(doc.get("content"));
System.out.println(doc.get("size"));
System.out.println(doc.get("path"));
System.out.println("------------------------------>");
}

}
}




package com.lucene.utils;

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

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.NumberTools;

public class LuceneUtils {
public static Document file2Document(String filePath){
File file=new File(filePath);
Document doc=new Document();
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("content",readFileContent(file), Store.YES, Index.ANALYZED));
doc.add(new Field("size", NumberTools.longToString(file.length()), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NOT_ANALYZED));
return doc;
}
public static String readFileContent(File file){
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
StringBuffer content=new StringBuffer();
for(String line=null;(line=reader.readLine())!=null;){
content.append(line);
}
return content.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值