使用lucene3.0索引txt文本时搜索索引报错

41488 2010-04-29 02:15:39
创建索引:--------------------------------------------
private File baseDir;

public File getBaseDir() {
return baseDir;
}

public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
}

public LogFileIndexer(File baseDir) {
super();
this.baseDir = baseDir;
}

// 读取分词内容方法
public void readContext(String indexName,IndexWriter iwriter,String strLine) throws CorruptIndexException, IOException{
String [] logs=strLine.split(" ");//分隔
for (int j = 0; j < logs.length; j++) {
Document doc = new Document();
Field fileDate=new Field("fielDate",logs[j],Field.Store.YES,Field.Index.NO);//日期不分词
Field fileContext=new Field(indexName,logs[j+1],Field.Store.YES,Field.Index.ANALYZED);

//System.out.println("indexName:"+indexName);////
//System.out.println(logs[j+1]);
doc.add(fileDate);
doc.add(fileContext);
iwriter.addDocument(doc);
break;
}
}
//创建索引
public int index(String indexName, File logFile)throws IOException{
int count=0;
Directory directory = FSDirectory.open(this.getBaseDir());
Analyzer analyzer=new ChineseAnalyzer();
IndexWriter iwriter = new IndexWriter(directory, analyzer,IndexWriter.MaxFieldLength.LIMITED);
File f = new File("D:\\suqingdiWORK\\SS");
File[] files=f.listFiles();// files

for (int i = 0; i < files.length; i++) {
BufferedReader br=new BufferedReader(new FileReader(files[i]));
int aa=br.read();
System.out.println("length:"+aa);//文件条数
String strLine=null;
while (br.readLine()!=null) {//如果还有下一条数据继续读取
strLine=br.readLine();
readContext(indexName,iwriter, strLine);//read
}
}
count= iwriter.numDocs();
// iwriter.optimize();
iwriter.close();
return count;
}
//这里计数索引条数为34条,并且可以把分词的内容输出无误
----------------------------------------------------

搜索索引:-----------------------------------------------
private File baseDir;

public File getBaseDir() {
return baseDir;
}

public void setBaseDir(File baseDir) {
this.baseDir = baseDir;
}

public LogFileSearcher(File baseDir) {
super();
this.baseDir = baseDir;
}
public List search(String indexName, String text) throws IOException, ParseException{
List logContexts=new ArrayList();
Directory dir=FSDirectory.open(this.getBaseDir());//索引位置
IndexSearcher searcher=new IndexSearcher(dir);
QueryParser parser=new QueryParser(Version.LUCENE_30, indexName,new ChineseAnalyzer());
Query query=parser.parse(text);
TopDocs dd=searcher.search(query,null,5000);
ScoreDoc[] hits=dd.scoreDocs;
// System.out.println(hits.length);
for (int i = 0; i < hits.length; i++) {
Document hitDoc = searcher.doc(i);
DocBean db=new DocBean();
db.setContext(hitDoc.get(indexName));
db.setDate(hitDoc.get("fielDate"));
logContexts.add(db);
// System.out.println(hitDoc.get(indexName));
}
searcher.close();
return logContexts;
}

---------------------------------------------------
测试:
private static final File INDEX_DIR=new File("D:\\suqingdiWORK\\cc\\2");
public static void main(String[] args) {

LogFileIndexer log=new LogFileIndexer(INDEX_DIR);
try {
int indexCount=log.index("fielContext", INDEX_DIR);
System.out.println("indexCount:"+indexCount);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


LogFileSearcher searcher=new LogFileSearcher(INDEX_DIR);
try {
List logContexts=searcher.search("fielContext", "仙");
System.out.println("listSize:"+logContexts.size()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


//
这里打印出listSize:2,而且如果打出来的话每次都是前2条数据,如果传入的是 “ ”会报错(org.apache.lucene.queryParser.ParseException: Cannot parse ' ': Encountered "<EOF>" at line 1, column 1.),
调试不出哪里出现问题,放到WEB上也是如此,
请各位帮我看看,在线等,谢谢。。。





...全文
334 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
筱韩 2011-02-24
  • 打赏
  • 举报
回复
这个分词器不好使啊,3.0之后的使用SmartChineseAnalyzer(Version.LUCENE_30);这个比较好,还有就是IKAnalyzer();这个分词器是唯一一个支持3.0版本以上的,这是我这几天看的
秦π 2010-04-29
  • 打赏
  • 举报
回复
lucene 初级的学习过程 看src包里的demo包 或是demo.html demo.pdf很多应用介绍的很详细了 只不过是英文的 有代码看起来也很方便的
41488 2010-04-29
  • 打赏
  • 举报
回复
不知道怎么做判断啊,现在只是一个初步的索引,可以折分但不会判断,也不知道上哪找点资源来学学,如果哪位有的话,是否可以提供一些学习lucene网址,谢谢
秦π 2010-04-29
  • 打赏
  • 举报
回复
用 luke 看看你创建的索引里都是些什么东西
秦π 2010-04-29
  • 打赏
  • 举报
回复
,如果传入的是 “ ”会报错(org.apache.lucene.queryParser.ParseException: Cannot parse ' ': Encountered "<EOF>" at line 1, column 1.), 这说明luncene 不能将空格作为关键词查询 也不符合parser的规范 比如 搜索context field 内容为 ‘xy’ 可以是context:xy 其他的什么range啊可以使number:1-100最好使用 query的子类进行查询 比如termquery,termrangequery,booleanquery 等等
41488 2010-04-29
  • 打赏
  • 举报
回复
谢了,资源共享。。。。。

欢乐极客 2010-04-29
  • 打赏
  • 举报
回复
也是正在学Lucene

楼主好运!!!
41488 2010-04-29
  • 打赏
  • 举报
回复
刚才在忙没注意到,suqingdi@163.com,这是我的邮箱,QQ:530755261,谢谢你了,
ladybirds2008 2010-04-29
  • 打赏
  • 举报
回复
你给我私信发个邮箱,我让我朋友给你发个小例子吧。。。我这上不了其他网站。。。嘿。。
41488 2010-04-29
  • 打赏
  • 举报
回复

2.9的呢,我用的是2.9,但要求是3.0,先搞定这个先,指点下
zuoguodang 2010-04-29
  • 打赏
  • 举报
回复
这个3.0没用过啊
41488 2010-04-29
  • 打赏
  • 举报
回复
没得积分下了,可怜可怜穷人吧,好心人在哪里,帮我找找错。。。。。
ladybirds2008 2010-04-29
  • 打赏
  • 举报
回复
http://download.csdn.net/source/2081722 这有个简单小的例子,你可以去看下,希望有帮助我也是初学 lucene

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧