owen42087743 2008-08-04 20:34
浏览 322
已采纳

lucene不能查询出结果(初级问题)

在data文件夹下有4个文件,a.txt,b.txt,c.txt,d.txt.每个文件的内容就是它的文件名,比如a.txt内容就是a.
idx文件夹就是索引存放的文件夹

lucene的索引:
[code="java"]public class Indexer {
public static void main(String[] args) throws Exception {
Indexer indexer = new Indexer();
indexer.index(new File("idx"), new File("data"));
}

public void index(File index, File data) throws Exception {
    IndexWriter indexWriter = new IndexWriter(index,
            new StandardAnalyzer(), true);
    indexWriter.setUseCompoundFile(false);
    indexDirectory(indexWriter, data);
    indexWriter.optimize();
    indexWriter.close();
}

public void indexDirectory(IndexWriter indexWriter, File data)
        throws IOException {
    if (data.isFile()) {
        indexFile(indexWriter, data);
    } else if (data.isDirectory()) {
        File[] files = data.listFiles();
        for (File file : files) {
            indexDirectory(indexWriter, file);
        }
    }
}

public void indexFile(IndexWriter indexWriter, File data)
        throws IOException {
    Document doc = new Document();
    doc.add(Field.Text("contents", new FileReader(data)));
    doc.add(Field.Keyword("filename", data.getCanonicalPath()));
    indexWriter.addDocument(doc);
}

}
[/code]
lucene的search:
[code="java"]public class Searcher {
public static void main(String[] args) throws Exception {
Searcher searcher = new Searcher();
searcher.search(new File("idx"), "a");
}

public void search(File index, String str) throws Exception {
    Directory directory = FSDirectory.getDirectory(index, false);
    IndexSearcher indexSearcher = new IndexSearcher(directory);
    Query query = QueryParser
            .parse(str, "contents", new StandardAnalyzer());
    Hits hits = indexSearcher.search(query);
    System.out.println(hits.length());
    for (int i = 0; i < hits.length(); i++) {
        Document document = hits.doc(i);
        System.out.println(document.getField("contents"));
        System.out.println(document.getFields("filename"));
    }
}

}
[/code]
索引文件和数据文件的路径都没有问题,已经经过测试,并且经过索引过后都可以产生索引文件,但是在searcher时,始终hit.length=0,不知道为什么?
(使用的是lucene-1.4)
[b]问题补充:[/b]
      现在比较常用的analyzer是什么?a是stopword,但是假如写成cat,就应该能识别出这是2个词是c和t啊.可事实并非如此.lucene1.4虽然老了点,可仍然是使用较多的版本啊.

  • 写回答

1条回答 默认 最新

  • jolestar 2008-08-05 16:38
    关注

    a 是StandardAnalyzer的默认stopword,会被过滤掉,所以你搜索不出来内容。
    要么换个analyzer,要么用个别的词做测试。
    ps:你的lucene版本也太老了点。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体