无法使用短语查询或通配符查询的有效索引找到任何结果?



由于某种原因,我无法从 3552 个项目的有效索引中找到任何结果。

请参阅下面的代码,然后是我运行时程序的控制台输出。3552 是索引文档的数量。/c:/test/stuff.txt 是从文档 5 作为测试检索的正确索引路径。底部的所有文本都是测试文件的全文(XML类型输出)。我的简单查询没有产生结果,我错过了什么?

也许我的通配符查询语法不好?我认为这会效率低下(由于开头和结尾的通配符),但它至少会从索引中返回此文档......

import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.FSDirectory;

public class Searcher
{
    /**
    * @param args
    * @throws IOException 
    * @throws CorruptIndexException 
    */
    public static void main(String[] args) throws CorruptIndexException, IOException
    {
        System.out.println("Begin searching test...");
        IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(args[0])));
        // termContainsWildcard is shown to be true here when debugging
        // numberOfTerms is 0
        WildcardQuery query = new WildcardQuery(new Term("contents", "*stuff*"));
        System.out.println("Query field is: " + query.getTerm().field());
        System.out.println("Query field contents is: " + query.getTerm().text());
        TopDocs results = searcher.search(query, 5000);
        // no results returned :(
        System.out.println("Total results from index " + args[0] + ": " + results.totalHits);
        for (ScoreDoc sd : results.scoreDocs)
        {
            System.out.println("Document matched. Number: " + sd.doc);
        }
        System.out.println();
        System.out.println("Begin reading test...");
        // now read from the index to see if I am crazy
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(args[0])));
        // correctly shows the number of documents in the local index
        System.out.println("Number of indexed documents: " + reader.numDocs());
        // pick out a random, small document and check its fields
        Document d = reader.document(5);
        for (Fieldable f : d.getFields())
        {
            System.out.println("Field name is: " + f.name());
            System.out.println(new String(f.getBinaryValue()));
        }
    }
}  

运行时的控制台输出

开始搜索测试...
查询字段为:内容
查询字段内容为:*stuff*
索引 C:\INDEX2 的总结果: 0

开始阅读测试...
索引文档数量:3552
字段名称为:路径
/c:/test/stuff.txt
字段名称为:内容

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="Content-Length" content="8"/>
<meta name="Content-Encoding" content="UTF-8"/>
<meta name="Content-Type" content="text/plain"/>
<meta name="resourceName" content="stuff.txt"/>
<title/>
</head>
<body>
<p>stuff &#13;
</p>
</body> </html>

你可以尝试使用Luke来运行你的查询并测试一些不同的查询。您还可以使用 Luke 浏览索引术语,这可能会为您提供有关正在发生的事情的线索。用于索引文档的代码也可能提供一些提示:例如,您的字段是否已编制索引?您正在从内容中获取二进制值,这可能意味着它从未被标记化并因此被索引。

默认情况下,前缀通配符查询(带有前导 * 的通配符查询)在 Lucene 中处于禁用状态。 有关详细信息,请参阅 Lucene 常见问题解答。 如果要启用前缀通配符查询,请尝试:

QueryParser.setAllowLeadingWildcard(true)

最新更新