索引已经创建完成了。之后查询怎么查都是空的
System.out.println(reader.document(6).toString());
也有值,请问为什么?还需要设置什么吗?
public List<Authors> searchAuthors(String queryString, String indexPath)
throws Exception {
List<Authors> list = new ArrayList<Authors>(); ScoreDoc[] hits = null;
try {
IndexReader reader = IndexReader.open(FSDirectory.open(new File(
indexPath)), true); 
Searcher searcher = new IndexSearcher(reader);
BooleanQuery booleanQuery = getBooleanQuery("name", queryString);
TopScoreDocCollector collector = TopScoreDocCollector.create(10,
false);
searcher.search(booleanQuery, collector);
hits = collector.topDocs().scoreDocs;
System.out.println(searcher.maxDoc());
System.out.println(hits.length);
System.out.println(reader.document(6).toString());
int i = 0;
while (i < hits.length) {
Document doc = searcher.doc(hits[i].doc);
System.out.println(">>content>>" + doc.get("name"));
System.out.println(">>title>>" + doc.get("title"));
i++;
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return list; } private static BooleanQuery getBooleanQuery(String field, String keyword)
throws CorruptIndexException, IOException {
QueryParser parser = null;
Query query = null;
BooleanQuery booleanQuery = new BooleanQuery();
try {
if (field.trim().length() > 0) {
parser = new QueryParser(field, new StandardAnalyzer(
Version.LUCENE_CURRENT));
parser.setDefaultOperator(QueryParser.AND_OPERATOR);
query = parser.parse(keyword);
System.out.println(query.toString("name"));
booleanQuery.add(query, BooleanClause.Occur.MUST);
}
} catch (ParseException e) {
e.printStackTrace();
}
return booleanQuery;
}