用过Lucene全文检索的请进 最近在学习Lucene全文检索,我检索的对象是一个文档库,主要检索里面的内容,要是有符合我所设置的关键字的文档,就将文档名称列出来,我已写好一个例子,但是当我输入英文的时候,可以检索,输入中午时就不行了。我用的是Lucene2.3.2。大家有什么好的解决办法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主去下一个极易分词器 je-analysis-1.5.1.jar换成Analyzer zh2 = new MMAnalyzer(); // 词库分词 你用的是哪个分词器,我用的是StandardAnalyzer standardAnalyzer=new StandardAnalyzer(Version.LUCENE_30);没问题的啊 我用的是StandardAnalyzer,但你括号的(Version.LUCENE_30);这个lucene2.3.2中没有吧?我把代码贴下吧: public int createDateBase(File file){//创建索引 int returnValue=0; if(!file.isDirectory()){ file.mkdirs(); } try { IndexWriter indexWriter=new IndexWriter(file,new StandardAnalyzer(),true); indexWriter.close(); returnValue=1; } catch (Exception e) { e.printStackTrace(); } return returnValue; }public int insertRecord(String dbpath,File file){ int returnValue=0; try { IndexWriter indexWriter=new IndexWriter(dbpath,new StandardAnalyzer(),false); this.addFile(indexWriter,file); returnValue=1; } catch (Exception e) { e.printStackTrace(); } return returnValue; }public ArrayList queryRecords(String searchkey,String dbpath,String searchField){ ArrayList list=null; try { IndexSearcher searcher=new IndexSearcher(dbpath); Hits hits=null;//是用来保存搜索的结果的。 Query query=null; QueryParser queryParser=new QueryParser(searchField,new StandardAnalyzer()); query=queryParser.parse(searchkey); hits=searcher.search(query); if(hits!=null){ list=new ArrayList(); int hitLength=hits.length(); Document doc =null; for(int i=0;i<hitLength;i++){ doc=hits.doc(i); list.add(doc.get("filePath")); } } } catch (Exception e) { e.printStackTrace(); } return list; } 少了这个方法public int insertRecord(String dbpath,String filePath){ return this.insertRecord(dbpath, new File(filePath)); } private void addFile(IndexWriter indexWriter, File file) { Document doc=new Document(); try{ doc.add(new Field("filePath",file.getAbsolutePath(),Field.Store.YES,Field.Index.TOKENIZED)); doc.add(new Field("contents", new FileReader(file))); indexWriter.addDocument(doc); indexWriter.close(); }catch(Exception e){ e.printStackTrace(); } } 我把所有的StandardAnalyzer换成了MMAnalyzer,但还是不行 用IK分词器吧而且你的lucene版本有点低 性能也不会太好http://85600367.javaeye.com/admin/blogs/855535这是我跑过的例子 你可以改一下 建议楼主看一下这篇文章。http://www.javaeye.com/topic/839504这位牛人给了一个例子。中英文都能检索到。 跟楼主一样,我最近也在研究这玩意儿,官网上最新版本是:3.0.3的。我自己写了个demo,也不好使,纠结。 怎样管理java的所有的包 org.apache.jasper.JasperException: The absolute uri: 求解 关于frameset和myeclipse....急求解答! Struts2 上传照片 请教jsp中Cookies用法 深入研究Struts 1.1(连载附图,十分经典!) js如何获得struts表单中的值(有答案直接给分) 请问在servlet中如何引用一个htm页面? 编译jsp用j2ee/j2me/j2se哪一个?? 很急!! 有关<form:select 相关的diable问题 sql读取不存在的mysql表的时候,怎么没有异常报告? i的问题
换成
Analyzer zh2 = new MMAnalyzer(); // 词库分词
StandardAnalyzer standardAnalyzer=new StandardAnalyzer(Version.LUCENE_30);
没问题的啊
public int createDateBase(File file){//创建索引
int returnValue=0;
if(!file.isDirectory()){
file.mkdirs();
}
try {
IndexWriter indexWriter=new IndexWriter(file,new StandardAnalyzer(),true);
indexWriter.close();
returnValue=1;
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public int insertRecord(String dbpath,File file){
int returnValue=0;
try {
IndexWriter indexWriter=new IndexWriter(dbpath,new StandardAnalyzer(),false);
this.addFile(indexWriter,file);
returnValue=1;
} catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
public ArrayList queryRecords(String searchkey,String dbpath,String searchField){
ArrayList list=null;
try {
IndexSearcher searcher=new IndexSearcher(dbpath);
Hits hits=null;//是用来保存搜索的结果的。
Query query=null;
QueryParser queryParser=new QueryParser(searchField,new StandardAnalyzer());
query=queryParser.parse(searchkey);
hits=searcher.search(query);
if(hits!=null){
list=new ArrayList();
int hitLength=hits.length();
Document doc =null;
for(int i=0;i<hitLength;i++){
doc=hits.doc(i);
list.add(doc.get("filePath"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public int insertRecord(String dbpath,String filePath){
return this.insertRecord(dbpath, new File(filePath));
} private void addFile(IndexWriter indexWriter, File file) {
Document doc=new Document();
try{
doc.add(new Field("filePath",file.getAbsolutePath(),Field.Store.YES,Field.Index.TOKENIZED));
doc.add(new Field("contents", new FileReader(file)));
indexWriter.addDocument(doc);
indexWriter.close();
}catch(Exception e){
e.printStackTrace();
}
}
而且你的lucene版本有点低 性能也不会太好
http://85600367.javaeye.com/admin/blogs/855535
这是我跑过的例子 你可以改一下
http://www.javaeye.com/topic/839504这位牛人给了一个例子。中英文都能检索到。
我自己写了个demo,也不好使,纠结。