Lucene QueryParser or Query: get all effectively required terms

215 views Asked by At

Is there a way to get from a Query instance all those terms that are effectively required for documents to contain? A QueryParser is used to create the Query instance, so the "content" of the query is user driven.

A user could for example give this as a query string

+A +B

then I would like to get [A, B] (e.g. as a string array)

or

A

then just [A] as the result I would need.

1

There are 1 answers

0
mindas On

Something like this might work (untested):

Query q = ...;
Set<Term> terms = new HashSet<>();
q.extractTerms(terms);
for (Term term : terms) {
  System.out.println(term.field());
}