新手学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);
}
}
}
------解决方案--------------------
研究研究,学习学习
------解决方案--------------------
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
谁有google地图校正的数据库或文件?
谁有google地图校正的数据库或文件?
或算法也行
QQ: 15537931
有同样问题的,加我QQ讨论下啊
...
-
请教google map v3自适应容器(浏览器,div等)大小的解决方法?
RT, 谢谢了!!!
------解决方案--------------------
#map_canvas ...
-
求救:MyEclipse6.0开发Android,提示无法找到AVD
我用的MyEclipse6.0搭建android开发环境,创建AVD之后在AVD管理器里面可以看到,运...
-
网站集成google网页搜索的问题…
想在站点中利用google的api提供搜索,高手给指点一下啊,能不能在服务端(asp.net)利用google搜索结果??
...
-
google map服务器部署在局域网,用户联网;用google map API可以开发的系统可以正常运行吗
google map服务器部署在局域网,用户联网;用g...
-
蛋疼~我的chrome居然打不开12306.cn的购票网页
同事的chrome都能打开12306.cn的购票网页,而我的却打不开。
我本本上的chrome版本...
-
google map局域网应用
用google map写了一个项目,内网有的有电脑不能联网,可以访问地图吗?
------解决方案--------------------
...
-
求助GMAP.NET?
RT,为什么自己搭建的WINFORM,加载不了GMAP.NET? 求熟悉者告知 万分感谢
------解决方案--------------------
1、...
-
求google maps api 源代码
有没有google maps api3 的源代码可以下载
------解决方案--------------------
api是谷歌对开发...
-
Google地图接口,怎么算两地的距离
最近做一个项目,需要根据用户填写的两个地名,算出两地之间的距离,我打算使用Google地图,我已经申请...