繁体   English   中英

Lucene查询无法按预期工作

[英]Lucene query not working as expected

运行Lucene的5.2.1版本。

这是我正在运行的代码:

Path indexPath = Paths.get("index");
Directory dir = NIOFSDirectory.open(indexPath);

IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

IndexWriter indexWriter = new IndexWriter(dir, iwc);
IndexReader indexReader = DirectoryReader.open(indexWriter, false);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
QueryParser queryParser = new QueryParser("defaultField", new StandardAnalyzer());
String query = "field:value";

indexSearcher.search(queryParser.parse(query), new SimpleCollector() {
    @Override
    public void collect(int doc) throws IOException {
        System.out.println("result " + doc);
    }

    @Override
    public boolean needsScores() {
        return false;
    }
});

我希望将文档ID打印到控制台,但是没有任何结果。

如果使用Luke来查看索引,我可以通过“文档”选项卡在索引中看到它应该返回的结果,但是如果使用“搜索”选项卡,则它再也不会返回任何内容,这使我觉得它有点问题与我的查询有关?

可能出什么问题了?

它不返回文档ID,而是返回文档索引。

您需要致电以获取该索引处的文档。 查看JavaDoc for Collector了解更多详细信息。

我索引为field:Value并通过field:value搜索... lucene区分大小写。

我现在将所有内容都转换为小写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM