出於某種原因,我無法從我的有效索引3552項目中找到任何結果。無法從PhraseQuery或WildcardQuery的有效索引中找到任何結果?
請看下面的代碼,當我運行它時,後面跟着程序的控制檯輸出。 是索引文件的數量。 /c:/test/stuff.txt是作爲測試從文檔5中檢索的正確的索引路徑。底部的所有文本都是測試文件的全文(在XML類型輸出中)。我錯過了什麼,我的簡單查詢不會產生結果?
也許我的WildcardQuery語法不好?我認爲這將是低效的(由於在開始和結束的通配符),但它至少會返回從該指數文件...
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總的結果:\索引2: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
</p>
</body>
</html>
由於您的Luke建議,我能夠弄清楚這一點!您是正確的 - 沒有實際標記化的二進制字段。對於一個新手來說很令人困惑。 – asteroid 2011-05-26 18:59:56