jens
jens

Reputation:

Lucene (Java) - How to specify default search field programatically?

I have the following code and would appreciate your advice.

   QueryParser queryParser = new QueryParser(searchTerm, analyzer);
   Query query = queryParser.parse(searchTerm);

My first question is, this "doubled"? As I have the "String to search for (=searchTerm)" in the constructor as well as in the parse() method. Is this really required? (For further usage i need a Query object). If i do it this way, does this maybe even introduce some negative side effects?

And I am not able to specify programmatically the "default field" to search for. In my queries I write "content:House" and it searches in the field "content". But how can I specify this programmatically that "content:" is my default field and a user only has to enter "House" (and lucene then automatically searches in the "content" field).

Thank you so much

jan

Upvotes: 1

Views: 2965

Answers (1)

skaffman
skaffman

Reputation: 403471

The first argument to the QueryParser constructor is the default search field, even if the javadoc doesn't make that obvious.

So you want this:

QueryParser queryParser = new QueryParser("content", analyzer);
Query query = queryParser.parse(searchTerm);

Upvotes: 5

Related Questions