专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Google技术

新手学luncene 简单有关问题 建立索引后 搜索不到结果请各位帮忙看看

发布时间:2011-07-03 07:08:47 文章来源:www.iduyao.cn 采编人员:星星草
新手学luncene 简单问题 建立索引后 搜索不到结果请各位帮忙看看
建立索引后 luceneindexfile 中有文件 但是进行搜索 无论搜什么 结果总是找到0条结果  
请高手帮忙看看  


建立索引的代码 

package testlucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.mira.lucene.analysis.*;

public class LuceneIndex {
public static void main(String[] args) throws Exception {

  File indexDir = new File("./luceneindexfile");
  Analyzer luceneAnalyzer = new IK_CAnalyzer();
  IndexWriter writer = new IndexWriter(indexDir,luceneAnalyzer,true);//建立索引器
Date start = new Date();
File folder = new File("./srcdocument");
if (folder.isDirectory()) {
String[] files = folder.list();
for (int i = 0; i < files.length; i++) {
File file = new File(folder, files[i]);
// 将要建立索引的文件构造成一个Document对象,并添加域"contents""path"
Document doc = new Document();
FileInputStream is = new FileInputStream(file);
Reader reader = new BufferedReader(new InputStreamReader(is));
Field Fieldcon = new Field("contents", reader.toString() , Field.Store.YES, Field.Index.TOKENIZED);
Field FieldPath = new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index .NO );
doc.add(Fieldcon);
doc.add(FieldPath);
System.out.println("正在建立索引 : " + file + "");
writer.addDocument(doc);
}
writer.optimize();
writer.close(); 
Date end = new Date();
System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");
}
}
}

搜索代码如下:


package testlucene;

import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
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;
import org.apache.lucene.search.Searcher;
import org.mira.lucene.analysis.*;

public class LuceneSearch {

public static void main(String[] args) {
String keyword="中华人民";
Analyzer analyzer = new IK_CAnalyzer();
Hits hts = null;
Query query = null;
try{
IndexSearcher search = new IndexSearcher("./luceneindexfile");
 
 
QueryParser qp = new QueryParser("contents", analyzer);
query = qp.parse(keyword);
System.out .print( query.toString());
 
 
hts = search.search(query);
for(int i=0; i<hts.length(); i++){
Document doc = hts.doc(i);
String path = doc.get("path");
System.out.println("Find: " +i+": "+ path);
System.out.println("Find: " + doc.get("contents"));
System.out.println("Find: " + doc.get("path"));
}
System.out.println("\nFind Total: " + hts.length());
}catch(Exception e){
System.out.println(e);
}
}
}






------解决方案--------------------
研究研究,学习学习


------解决方案--------------------
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: