簡體   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