简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM