search with lucene's QueryParser while setting fieldName to "*" failed?

67 views Asked by At

I want to search through all fields in the index by lucene, and I learned to do this by writting the code like this:

//Create a parser that searches through all fields
QueryParser queryParser = new QueryParser("*", new StandardAnalyzer()); 

//Specify the search terms
String queryString = "search terms";

//Create the query to search through all fields
Query query = queryParser.parse(queryString); 

//Execute the query and get the results
IndexSearcher searcher = new IndexSearcher(indexReader); 
TopDocs results = searcher.search(query, 100); 
ScoreDoc[] hits = results.scoreDocs; 

//Iterate through the results
for (ScoreDoc hit : hits) { 
  Document doc = searcher.doc(hit.doc); 
  //Process the document
}

when I setted first parameter of QueryParser Constructor to "*", like the code above, and I got nothing from the TopDocs(which I had expected to search through all fields of the documents I'd write into the index and returnd all matching documents), the "hits.totalHits" returns 0.

Can anyone tell me what is wrong with my code or how to write code using QueryParser to search through all fields in the index?

Thanks!

1

There are 1 answers

1
Seasers On

Have you ever tried the MultiFieldQueryParser?
In this case you have to specify all fields where to want to search on.