Search code examples
lucenequery-parser

What is an alternative for Lucene Query's extractTerms?


In Lucene 4.6.0 there was the method extractTerms that provided the extraction of terms from a query (Query 4.6.0). However, from Lucene 6.2.1, it does no longer exist (Query Lucene 6.2.1). Is there a valid alternative for it?

What I'd need is to parse terms (and corrispondent fields) of a Query built by QueryParser.


Solution

  • Maybe not the best answer but one way is to use the same analyzer and tokenize the query string:

    Analyzer anal = new StandardAnalyzer();
    TokenStream ts = anal.tokenStream("title", query); // string query
    CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
    ts.reset();
    while (ts.incrementToken()) {
        System.out.println(termAtt.toString());
    }
    anal.close();