What is an alternative for Lucene Query's extractTerms?

耗尽温柔 提交于 2019-12-11 18:32:29

问题


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.


回答1:


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



回答2:


I have temporarely solved my problem with the following code. Smarter alternatives will be well accepted:

QueryParser qp = new QueryParser("title", a);
Query q = qp.parse(query);
Set<Term> termQuerySet = new HashSet<Term>();
Weight w = searcher.createWeight(q, true, 3.4f);
w.extractTerms(termQuerySet);


来源:https://stackoverflow.com/questions/50948894/what-is-an-alternative-for-lucene-querys-extractterms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!