How to search across all the fields?

Adam Lee picture Adam Lee · Mar 2, 2013 · Viewed 9.3k times · Source

In Lucene, we can use TermQuery to search a text with a field. I am wondering how to search a keyword across a bunch of fields or all the searchable fields?

Answer

javanna picture javanna · Mar 2, 2013

Another approach, which doesn't require to index anything more than what you already have, nor to combine different queries, is using the MultiFieldQueryParser.

You can provide a list of fields where you want to search on and your query, that's all.

MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                Version.LUCENE_41, 
                new String[]{"title", "content", "description"},
                new StandardAnalyzer(Version.LUCENE_41));

Query query = queryParser.parse("here goes your query");

This is how I would do it with the original lucene library written in Java. I'm not sure whether the MultiFieldQueryParser is available in lucene.net too.