public PhraseQuery getPhraseQuery(String text, String fieldName) throws IOException, ParseException {
PhraseQuery pq=new PhraseQuery();
pq.setSlop(0);
TokenStream ts=new StandardAnalyzer().tokenStream(fieldName, text);
ts.addAttribute(CharTermAttribute.class);
ts.addAttribute(PositionIncrementAttribute.class);
ts.reset();
int pos=0;
while (ts.incrementToken()) {
CharTermAttribute charTermAttr=ts.getAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncrAttr=ts.getAttribute(PositionIncrementAttribute.class);
pos+=posIncrAttr.getPositionIncrement();
pq.add(new Term(fieldName, charTermAttr.toString()), pos-1);
}
return pq;
}
PhraseQuery pq=new PhraseQuery();
pq.setSlop(0);
TokenStream ts=new StandardAnalyzer().tokenStream(fieldName, text);
ts.addAttribute(CharTermAttribute.class);
ts.addAttribute(PositionIncrementAttribute.class);
ts.reset();
int pos=0;
while (ts.incrementToken()) {
CharTermAttribute charTermAttr=ts.getAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncrAttr=ts.getAttribute(PositionIncrementAttribute.class);
pos+=posIncrAttr.getPositionIncrement();
pq.add(new Term(fieldName, charTermAttr.toString()), pos-1);
}
return pq;
}
No comments:
Post a Comment