[lucene] 初学lucene遇到的问题,请大家看一下
changxin
2009-09-04
1、对文件进行检索,文件名如果不含汉字,则必须输入文件名+类型,方可检索到
2、对文件类容进行检索,.txt文件可检索到内容,其它则不可以 请问这是为什么? public class Demo { public static void createIndex(Directory dir,Analyzer analyzer) throws CorruptIndexException, LockObtainFailedException, IOException{ IndexWriter writer=new IndexWriter(dir,analyzer,true,IndexWriter.MaxFieldLength.UNLIMITED); File file=new File("E:\\1231313.txt"); Document doc=new Document(); doc.add(new Field("name",file.getName(),Field.Store.YES,Field.Index.ANALYZED)); doc.add(new Field("content",new FileReader(file))); writer.addDocument(doc); writer.close(); } public static void search(Directory dir,Analyzer analyzer) throws CorruptIndexException, IOException, ParseException{ Searcher searcher=new IndexSearcher(dir); Query query=new QueryParser("name",analyzer).parse("1231313"); ScoreDoc[] docs=searcher.search(query, searcher.maxDoc()).scoreDocs; System.out.println(docs.length); Document doc; for(int i=0;i<docs.length;i++){ doc=searcher.doc(docs[i].doc); System.out.println(doc.get("name")); } query=new QueryParser("content",analyzer).parse("范德 飞洒"); docs=searcher.search(query, searcher.maxDoc()).scoreDocs; System.out.println(docs.length); for(int i=0;i<docs.length;i++){ doc=searcher.doc(docs[i].doc); System.out.println(doc.get("name")); } } public static void main(String[] args) throws IOException, ParseException { Analyzer analyzer=new StandardAnalyzer(); FSDirectory dir=FSDirectory.getDirectory(new File("e://iii")); createIndex(dir, analyzer); search(dir, analyzer); dir.close(); } } |