Lucene全文检索样例(解决大文本建索引)
来源:互联网 发布:加密狗软件图片 编辑:程序博客网 时间:2024/06/07 02:59
建索引:
- package com.pccw;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- 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.IndexWriter;
- /** */ /**
- * author Shane in PCCW
- *
- */
- public class TextFileIndexer {
- public static void main(String[] args) throws Exception {
- /**/ /* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */
- File fileDir = new File( "c://s" );
- /**/ /* 这里放索引文件的位置 */
- File indexDir = new File( "c://index" );
- Analyzer luceneAnalyzer = new StandardAnalyzer();
- IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
- true );
- indexWriter.setMaxFieldLength(99999999);//增加内存域长度限制(非常重要)
- File[] textFiles = fileDir.listFiles();
- long startTime = new Date().getTime();
- // 增加document到索引去
- for ( int i = 0 ; i < textFiles.length; i ++ ) {
- if (textFiles[i].isFile()
- && textFiles[i].getName().endsWith( ".txt" )) {
- System.out.println( " File " + textFiles[i].getCanonicalPath()
- + " 正在被索引. " );
- String temp = FileReaderAll(textFiles[i].getCanonicalPath(),
- " GBK " );
- System.out.println(temp);
- Document document = new Document();
- Field FieldPath = new Field( " path " , textFiles[i].getPath(),
- Field.Store.YES, Field.Index.NO);
- Field FieldBody = new Field( " body " , temp, Field.Store.YES,
- Field.Index.TOKENIZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
- document.add(FieldPath);
- document.add(FieldBody);
- indexWriter.addDocument(document);
- }
- }
- // optimize()方法是对索引进行优化
- indexWriter.optimize();
- indexWriter.close();
- // 测试一下索引的时间
- long endTime = new Date().getTime();
- System.out
- .println( " 这花费了 "
- + (endTime - startTime)
- + " 毫秒来把文档增加到索引里面去! "
- + fileDir.getPath());
- }
- public static String FileReaderAll(String FileName, String charset)
- throws IOException {
- BufferedReader reader = new BufferedReader( new InputStreamReader(
- new FileInputStream(FileName), charset));
- String line = new String();
- String temp = new String();
- while ((line = reader.readLine()) != null ) {
- temp += line + "/n";
- }
- reader.close();
- return temp;
- }
- }
查询:
- package com.pccw;
- import java.io.IOException;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.queryParser.QueryParser;
- import org.apache.lucene.search.Hits;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- public class TestQuery {
- public static void main(String[] args) throws IOException, ParseException {
- Hits hits = null ;
- String queryString = "中华" ;
- Query query = null ;
- IndexSearcher searcher = new IndexSearcher( " c://index " );
- Analyzer analyzer = new StandardAnalyzer();
- try {
- QueryParser qp = new QueryParser( " body " , analyzer);
- query = qp.parse(queryString);
- } catch (ParseException e) {
- }
- if (searcher != null ) {
- hits = searcher.search(query);
- if (hits.length() > 0 ) {
- System.out.println( " 找到: " + hits.length() + " 个结果! " );
- }
- }
- }
- }
- Lucene全文检索样例(解决大文本建索引)
- 全文检索(lucene)
- 全文检索之lucene的优化篇 建索引库
- 全文检索Lucene(二)---索引库维护
- 全文检索(lucene)开发
- 【Lucene】Apache Lucene全文检索引擎架构之构建索引
- 【Lucene】Apache Lucene全文检索引擎架构之构建索引
- 全文检索Lucene(一)--Document操作与文本域加权
- Lucene全文检索 之创建索引、增删改查(纯代码)
- 基于Java的全文索引/检索引擎——Lucene
- 基于Java的全文索引/检索引擎——Lucene
- 使用Lucene进行全文检索(一)---处理索引
- 基于Java的全文索引/检索引擎——Lucene
- 基于Java的全文索引/检索引擎——Lucene
- 全文检索Lucene入门之创建索引及简单搜索
- Lucene 全文检索的基本原理-如何创建索引
- 全文检索之lucene的优化篇--创建索引库
- 全文检索之lucene的优化篇--创建索引库
- Linux安装命令小结
- CString和int的转换
- 论语中的仁德思想和从学态度
- LINUX下tar.gz包的安装方法
- 函数指针
- Lucene全文检索样例(解决大文本建索引)
- M/T法测速
- 软件兴趣者的机遇
- 集成JPA环境
- [转贴]new 一个数组,然后对数组pp越界访问,delete []pp 时出错,为什么?
- MSComm串行通讯控件----2进制发送、接收数据(网上转载)
- lucene索引word/pdf/html/txt文件及检索(搜索引擎)
- PDFBox下载及将pdf提取转换为txt文档
- 随笔 1