Lucene4.6 学习 001

package com.zhangzhanlei.lucene;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

 

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.Version;

 

public class TestIndexer

{

private String fieldName;

private String endStr;

 

public TestIndexer(String fieldName,String endStr)

{

this.fieldName = fieldName;

this.endStr = endStr;

}

/**

* lucene 索引创建 主方法

* @param indexDir

* @param dataDir

* @return

* @throws IOException

*/

public int index(File indexDir,File dataDir) throws IOException

{

if(!dataDir.exists()||!dataDir.isDirectory())

{

throw new IOException(dataDir+":does not exist or is not a directory");

}

Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_46,true);

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_46,analyzer);

Directory directory = FSDirectory.open(indexDir);

if(IndexWriter.isLocked(directory))

{

IndexWriter.unlock(directory);

}

IndexWriter writer = new IndexWriter(directory,indexWriterConfig);

writer.deleteAll();

indexDirectory(writer,dataDir);

int numIndexed = writer.numDocs();

writer.close();

 

return numIndexed;

 

}

 

public void indexDirectory(IndexWriter writer,File dir) throws IOException

{

File [] fiels = dir.listFiles();

for(File file : fiels)

{

if(file.isDirectory())

{

indexDirectory(writer,file);

}

else if (file.getName().endsWith(this.endStr))

{

indexFile(writer,file);

}

}

}

 

/**

* 对文件创建索引

* @param writer

* @param f

* @throws IOException

*/

public void indexFile(IndexWriter writer,File f) throws IOException

{

if(f.isHidden()||!f.exists()||!f.canRead())

{

return;

}

System.out.println("Indexing: "+f.getCanonicalPath());

getTXT (writer,f,"GBK");

}

 

/***

* 读取文件,为单行加入索引

* @param file

* @param charset

* @return

* @throws IOException

*/

public void getTXT (IndexWriter writer,File file,String charset) throws IOException

{

FileInputStream fileInputStream = new FileInputStream(file);

InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,charset);

BufferedReader reader = new BufferedReader(inputStreamReader);

String line = new String();

while((line=reader.readLine())!=null)

{

Document doc = new Document();

doc.add(new Field("line",line,Field.Store.YES,Field.Index.ANALYZED));

writer.addDocument(doc);

}

reader.close();

}

 

/**

* @param args

*/

public static void main(String[] args)

{

TestIndexer indexer = new TestIndexer("filepath",".txt");

try

{

File indexDir = new File ("d:\\lucenetest\\index");

File dataDir = new File ("d:\\lucenetest\\file");

int result = indexer.index(indexDir, dataDir);

System.out.println("indexing : " +result + " files.");

}

catch (Exception e)

{

e.printStackTrace();

}

}

 

}

 

Indexing: D:\lucenetest\file\bwpf814.txt

Indexing: D:\lucenetest\file\bwpf815.txt

Indexing: D:\lucenetest\file\bwpf816.txt

Indexing: D:\lucenetest\file\bwpf817.txt

Indexing: D:\lucenetest\file\bwpf818.txt

indexing : 45550 files.

 

 

package com.zhangzhanlei.lucene;

 

import java.io.File;

import java.io.IOException;

 

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;

import org.apache.lucene.document.Document;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.queryparser.classic.ParseException;

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;

 

public class TestSearcher

{

 

private File indexDir;

private String fieldName;

 

public TestSearcher (File indexDir,String fieldName)

{

this.indexDir = indexDir;

this.fieldName = fieldName;

}

 

public void searcher(String keywords) throws IOException, ParseException

{

Directory fsDir = FSDirectory.open(indexDir);

IndexReader reader = IndexReader.open(fsDir);

IndexSearcher is = new IndexSearcher(reader);

Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_46,true);

QueryParser queryParser = new QueryParser(Version.LUCENE_46,fieldName,analyzer);

Query query = queryParser.parse(keywords);

 

TopDocs docs = is.search(query, 1000);

ScoreDoc [] scoreDoc = docs.scoreDocs;

System.out.println("Found "+docs.totalHits+" documents that matched query '"+keywords +"'");

for(int i = 0 ;i<scoreDoc.length;i++)

{

Document miDoc = reader.document(scoreDoc[i].doc);

System.out.println(miDoc.get(fieldName));

}

reader.close();

}

/**

* @param args

*/

public static void main(String[] args)

{

TestSearcher searcher = new TestSearcher(new File("d:\\lucenetest\\index"),"line");

try

{

searcher.searcher("查询超时");

}

catch(Exception e)

{

e.printStackTrace();

}

}

 

}

 

Found 750 documents that matched query '查询超时'

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 查询超时。

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 查询超时。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值