查不出数据 什么原因?
我用的lucene2.9
创建的方法
public static void createIndex(File src, File destDir) {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); // 创建一个语法分析器
IndexWriter iwriter = null;
Directory directory = null;
try {
directory = FSDirectory.open(destDir); // 把索引文件存储到磁盘目录
// 创建一个IndexWriter(存放索引文件的目录,分析器,Field的最大长度)
iwriter = new IndexWriter(directory, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
// iwriter.setUseCompoundFile(true);//使用复合文件 Document doc = new Document(); // 创建一个Document对象
// 把文件路径作为"path"域:不分词,索引,保存
doc.add(new Field("path", src.getCanonicalPath(), Field.Store.YES,
Field.Index.NOT_ANALYZED)); StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(src));
for (String str = null; (str = br.readLine()) != null;) {
sb.append(str).append(System.getProperty("line.separator"));
}
// 文件内容作为"content"域:分词,索引,保存
doc.add(new Field("contents", sb.toString(), Field.Store.YES,
Field.Index.ANALYZED)); iwriter.addDocument(doc); // 把Document存放到IndexWriter中
iwriter.optimize(); // 对索引进行优化
} catch (IOException e) {
e.printStackTrace();
} finally {
if (iwriter != null) {
try {
iwriter.close(); // 关闭IndexWriter时,才把内存中的数据写到文件
} catch (IOException e) {
e.printStackTrace();
}
}
if (directory != null) {
try {
directory.close(); // 关闭索引存放目录
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
搜索的方法:public static void searcher(String keyword, File indexDir) {
IndexSearcher isearcher = null;
Directory directory = null;
try {
// 创建一个分析器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
// 存放路劲设置为硬盘
directory = FSDirectory.open(indexDir);
// 创建解析器
// 3个参数 (版本,索引域,分析器)
QueryParser parser = new QueryParser(Version.LUCENE_29, "contents",
analyzer);
// 获取查询对象
Query query = parser.parse(keyword);
// 可以利用term查询对象参数是(域名,关键字)
// Query query1 =new TermQuery(new Term("contents",keyword));
// 创建索引搜索器
isearcher = new IndexSearcher(directory, true);
// 执行搜索 获取查询结果集对象
TopDocs ts = isearcher.search(query, null, 100);
// 获取命中数量
int totalHits = ts.totalHits;
System.out.println("命中数:" + totalHits);
// 获取命中的文档信息对象
ScoreDoc[] hits = ts.scoreDocs;
for (int i = 0; i < hits.length; i++) {
// 根据命中的文档的内部编号获取该文档
Document hitdoc = isearcher.doc(hits[i].doc);
System.out.println(hitdoc.getField("contents").stringValue());
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} catch (ParseException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
// 检测indexwriter是否存在
if (isearcher != null) {
try {
// 注意,关闭IndexWriter时才把内存中数据写入到文件
isearcher.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 检测Directory是否存在
if (directory != null) {
try {
// 关闭索引存放目录
directory.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}main方法:public static void main(String[] args) {
// TODO Auto-generated method stub
TestFileIndexer.createIndex(new File("E:\\s\\1.txt"),
new File("E:\\s"));
TestFileSearcher.searcher("a", new File("E:\\s")); }控制台输出:abcdefghijklmnopqrstuvwxyz (是我在s文件夹下有个1.txt 里面的内容)命中数:0内容也输出不了 急死我了 谁帮帮我~~~

解决方案 »

  1.   

    MLGB我太傻了。
    lucene对英文是不能自己分词检索的!
    改成public static void main(String[] args) {
            // TODO Auto-generated method stub
            TestFileIndexer.createIndex(new File("E:\\s\\1.txt"),
                    new File("E:\\s"));
            TestFileSearcher.searcher("abcdefghijklmnopqrstuvwxyz", new File("E:\\s"));    }
    就他么的出来了1条了。但是又出来一个问题。。
    如果1.txt中如果有中文的话获取的是乱码。
    必须给1.txt编码改成UTF-8正常 但是怎么控制外面文本文件的编码?难道都得自己手工改吗?
      

  2.   

    还有 怎么用lucene 实现doc pdf--> text 之间的转换呢?
      

  3.   

     public static void main(String[] args)