FredSuvn
FredSuvn

Reputation: 2087

How to parse search string without fields limit in lucene?

I was writing an indexer interface and designed a method:

List<MyDocument> search(String query, int start, int end);

This method just like jdbc search method: pass an sql string and return.

But when I tried to use lucene to implement, i didn't find a way to parse query string into an Query Object.

I know QueryParser and MultiFieldQueryParser but they need pre-preparedly specify the fields to be searched. In my interface, which fields will be searched doesn't pre-know.

For example:

(title: help) AND (author: me) AND (content: plz)

Field title, author and content is pre-unknown. How to build a Query for these pre-unknown fields searching in lucene?

Is there any way can help me?

Upvotes: 1

Views: 712

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

The field argument to QueryParser is the default field. That is, when a field isn't explicitly provided in the query, it will use that field. So, if I gave it "default" as my default field, and then passed it: title:help AND stuff That is, like this:

QueryParser parser = new QueryParser("default", new StandardAnalyzer());
Query query = parser.parse("title:help AND stuff");

It would interpret that as: title:help AND default:stuff

The syntax you've provided will work as you intend, regardless of what default field you pass the QueryParser.

Upvotes: 3

Related Questions