final Boolean positionIncrementsEnabled = this.getQueryConfigHandler().get(ConfigurationKeys.ENABLE_POSITION_INCREMENTS);
if (positionIncrementsEnabled != null) {
this.positionIncrementsEnabled = positionIncrementsEnabled;
}
final FieldQueryNode fieldNode = ((FieldQueryNode) node);
final String text = fieldNode.getTextAsString();
final String field = fieldNode.getFieldAsString();
final String datatype = (String) fieldNode.getTag(DatatypeQueryNode.DATATYPE_TAGID);
if (datatype == null) {
return node;
}
final Analyzer analyzer = this.getQueryConfigHandler()
.get(KeywordConfigurationKeys.DATATYPES_ANALYZERS)
.get(datatype);
if (analyzer == null) {
throw new QueryNodeException(new MessageImpl(
QueryParserMessages.INVALID_SYNTAX, "No analyzer associated with " + datatype));
}
PositionIncrementAttribute posIncrAtt = null;
int numTokens = 0;
int positionCount = 0;
boolean severalTokensAtSamePosition = false;
final TokenStream source;
try {
source = analyzer.tokenStream(field, new StringReader(text));
source.reset();
} catch (final IOException e1) {
throw new RuntimeException(e1);
}
final CachingTokenFilter buffer = new CachingTokenFilter(source);
if (buffer.hasAttribute(PositionIncrementAttribute.class)) {
posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);
}
try {
while (buffer.incrementToken()) {
numTokens++;
final int positionIncrement = (posIncrAtt != null) ? posIncrAtt
.getPositionIncrement() : 1;
if (positionIncrement != 0) {
positionCount += positionIncrement;
} else {
severalTokensAtSamePosition = true;
}
}
} catch (final IOException e) {
// ignore
}
try {
// rewind the buffer stream
buffer.reset();
// close original stream - all tokens buffered
source.close();
} catch (final IOException e) {
// ignore
}
if (!buffer.hasAttribute(CharTermAttribute.class)) {
return new NoTokenFoundQueryNode();
}
final CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);
if (numTokens == 0) {
if (nbTwigs != 0) { // Twig special case
return new WildcardNodeQueryNode();
}
return new NoTokenFoundQueryNode();
}
else if (numTokens == 1) {
String term = null;
try {
boolean hasNext;
hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
} catch (final IOException e) {
// safe to ignore, because we know the number of tokens
}
fieldNode.setText(term);
return fieldNode;
}
else {
// no phrase query:
final LinkedList<QueryNode> children = new LinkedList<QueryNode>();
int position = -1;
for (int i = 0; i < numTokens; i++) {
String term = null;
final int positionIncrement = 1;
try {
final boolean hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
} catch (final IOException e) {
// safe to ignore, because we know the number of tokens
}
final FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);
if (this.positionIncrementsEnabled) {
position += positionIncrement;
newFieldNode.setPositionIncrement(position);
} else {
newFieldNode.setPositionIncrement(i);
}
children.add(new FieldQueryNode(field, term, -1, -1));
}
if (node.getParent() instanceof TokenizedPhraseQueryNode) {
throw new QueryNodeException(new MessageImpl("Cannot build a MultiPhraseQuery"));
}