最近在学习luence时,按照帮助文档以及各位前人的帖子,
能够实现如下功能:
一,能够真确的搜索出文章中含有的英文单词的文件。
存在的问题:
一,对中文文件不支持,想实现这一功能,看了一些文档及说明,基本上都是说自己重写analysis和search两个类,但是不会写,不知道从何入手。
二,想对数据库进行全文检索,但是不知道如何设置路径。
在此先谢谢大家!
能够实现如下功能:
一,能够真确的搜索出文章中含有的英文单词的文件。
存在的问题:
一,对中文文件不支持,想实现这一功能,看了一些文档及说明,基本上都是说自己重写analysis和search两个类,但是不会写,不知道从何入手。
二,想对数据库进行全文检索,但是不知道如何设置路径。
在此先谢谢大家!
看来你对luence的基本原理还没掌握,建议先看看luence in action.
中文问题估计是那个demo读取文本文件的时候编码设置问题。analyzer用StandardAnalyzer就可以,不过效果不太好,它是把中文按字切开的。
很高兴在此看到Jolestar(叶明)的回复!
但是目前好像还是有点问题!
不知道luence-1.4.3和luence-2.0是不是相差好多阿?
在插入记录的时候,调用Field.Keyword("filename",file.getName())方法,显示的错误信息这个方法没有定义!
不知道原因是什么?
接下来我会贴出我的代码!
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="org.apache.lucene.analysis.standard.StandardAnalyzer"%>
<%@ page import="org.apache.lucene.index.IndexWriter"%>
<%
String file_str_1 = "E:/db";
File file_1 = new File(file_str_1);
if(!file_1.isDirectory()){
file_1.mkdirs();
} try{
org.apache.lucene.index.IndexWriter indexWriter = new org.apache.lucene.index.IndexWriter(file_1,new org.apache.lucene.analysis.standard.StandardAnalyzer(),true);
indexWriter.close();
out.println("db init succ");
}catch(Exception ex){
ex.printStackTrace();
}
out.println("<br>");
out.println("input data start");
out.println("<br>");
String file_str_2 = "E:/11.txt";
File file_2 = new File(file_str_2); org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
indexWriter = new org.apache.lucene.index.IndexWriter(file_1,new org.apache.lucene.analysis.standard.StandardAnalyzer(),false); try{
doc.add(org.apache.lucene.document.Field.Keyword("filename",file_2.getName()));
String aa = "";
StringBuffer sb = new StringBuffer();
char[] c = new char[4096];
try{
Reader reader = new FileReader(file_str_2);
int n = 0;
while(true){
n = reader.read(c);
if(n > 0){
sb.append(c,0,n);
}else{
break;
}
}
reader.close();
}
catch(Exception ex){
ex.printStackTrace();
}
aa = sb.toString();
doc.add(org.apache.lucene.document.Field.Text("content",aa));
indexWriter.addDocument(doc);
indexWriter.close();
}catch(Exception ex){
ex.printStackTrace();
} out.println("input data succ"); org.apache.lucene.search.Searcher searcher = new org.apache.lucene.search.IndexSearcher(file_str_1);
org.apache.lucene.search.Query query = org.apache.lucene.queryParser.QueryParser.parse("dog","content",new org.apache.lucene.analysis.standard.StandardAnalyzer()); doc = null;
ArrayList list = null; org.apache.lucene.search.Hits hits = searcher.search(query);
try{
if(hits != null){
list = new ArrayList(); int temp_hitslength = hits.length(); for(int i = 0;i < temp_hitslength; i++){ doc = hits.doc(i); list.add(doc.get("filename")); } } }catch(Exception ex){ ex.printStackTrace(); }
for(int i=0;i<list.size();i++){
out.print("<br>");
out.print(list.size());
out.print("<br>");
out.println((String)list.get(i)); }
%>
另外一个是车东的2字分词,简单易用,但是2字分词,准确性很不好。
但是在家里的电脑,就出现了Field.Keyword和Field.Text这两个方法没有定义!
不知道原因在哪里啊?
由于在luence_1.4.3有field.keyword函数,但是在2.0中没有了!
嗨!
要吸取教训阿!
昨晚搞到三点,还是没有搞定阿!
现在终于可以了阿!
不知道是学2.0好还是1.4的版本好啊?
大家说说!
我上次去注册了!
但是好像没有通过,您可否发一些资料给我啊?