use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class BoostQueryNodeBuilder method build.
@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
BoostQueryNode boostNode = (BoostQueryNode) queryNode;
QueryNode child = boostNode.getChild();
if (child == null) {
return null;
}
Query query = (Query) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
return new BoostQuery(query, boostNode.getValue());
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class PhraseQueryNodeBuilder method build.
@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
TokenizedPhraseQueryNode phraseNode = (TokenizedPhraseQueryNode) queryNode;
PhraseQuery.Builder builder = new PhraseQuery.Builder();
List<QueryNode> children = phraseNode.getChildren();
if (children != null) {
for (QueryNode child : children) {
TermQuery termQuery = (TermQuery) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
FieldQueryNode termNode = (FieldQueryNode) child;
builder.add(termQuery.getTerm(), termNode.getPositionIncrement());
}
}
return builder.build();
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class QueryNodeOperation method logicalAnd.
/**
* perform a logical and of 2 QueryNode trees. if q1 and q2 are ANDQueryNode
* nodes it uses head Node from q1 and adds the children of q2 to q1 if q1 is
* a AND node and q2 is not, add q2 as a child of the head node of q1 if q2 is
* a AND node and q1 is not, add q1 as a child of the head node of q2 if q1
* and q2 are not ANDQueryNode nodes, create a AND node and make q1 and q2
* children of that node if q1 or q2 is null it returns the not null node if
* q1 = q2 = null it returns null
*/
public static final QueryNode logicalAnd(QueryNode q1, QueryNode q2) {
if (q1 == null)
return q2;
if (q2 == null)
return q1;
ANDOperation op = null;
if (q1 instanceof AndQueryNode && q2 instanceof AndQueryNode)
op = ANDOperation.BOTH;
else if (q1 instanceof AndQueryNode)
op = ANDOperation.Q1;
else if (q2 instanceof AndQueryNode)
op = ANDOperation.Q2;
else
op = ANDOperation.NONE;
try {
QueryNode result = null;
switch(op) {
case NONE:
List<QueryNode> children = new ArrayList<>();
children.add(q1.cloneTree());
children.add(q2.cloneTree());
result = new AndQueryNode(children);
return result;
case Q1:
result = q1.cloneTree();
result.add(q2.cloneTree());
return result;
case Q2:
result = q2.cloneTree();
result.add(q1.cloneTree());
return result;
case BOTH:
result = q1.cloneTree();
result.add(q2.cloneTree().getChildren());
return result;
}
} catch (CloneNotSupportedException e) {
throw new QueryNodeError(e);
}
return null;
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class SynonymQueryNodeBuilder method build.
@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
// TODO: use SynonymQuery instead
SynonymQueryNode node = (SynonymQueryNode) queryNode;
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (QueryNode child : node.getChildren()) {
Object obj = child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
if (obj != null) {
Query query = (Query) obj;
builder.add(query, Occur.SHOULD);
}
}
return builder.build();
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class AnalyzerQueryNodeProcessor method postProcessNode.
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof TextableQueryNode && !(node instanceof WildcardQueryNode) && !(node instanceof FuzzyQueryNode) && !(node instanceof RegexpQueryNode) && !(node.getParent() instanceof RangeQueryNode)) {
FieldQueryNode fieldNode = ((FieldQueryNode) node);
String text = fieldNode.getTextAsString();
String field = fieldNode.getFieldAsString();
CachingTokenFilter buffer = null;
PositionIncrementAttribute posIncrAtt = null;
int numTokens = 0;
int positionCount = 0;
boolean severalTokensAtSamePosition = false;
try {
try (TokenStream source = this.analyzer.tokenStream(field, text)) {
buffer = new CachingTokenFilter(source);
buffer.reset();
if (buffer.hasAttribute(PositionIncrementAttribute.class)) {
posIncrAtt = buffer.getAttribute(PositionIncrementAttribute.class);
}
try {
while (buffer.incrementToken()) {
numTokens++;
int positionIncrement = (posIncrAtt != null) ? posIncrAtt.getPositionIncrement() : 1;
if (positionIncrement != 0) {
positionCount += positionIncrement;
} else {
severalTokensAtSamePosition = true;
}
}
} catch (IOException e) {
// ignore
}
// rewind the buffer stream
//will never through on subsequent reset calls
buffer.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (!buffer.hasAttribute(CharTermAttribute.class)) {
return new NoTokenFoundQueryNode();
}
CharTermAttribute termAtt = buffer.getAttribute(CharTermAttribute.class);
if (numTokens == 0) {
return new NoTokenFoundQueryNode();
} else if (numTokens == 1) {
String term = null;
try {
boolean hasNext;
hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
} catch (IOException e) {
// safe to ignore, because we know the number of tokens
}
fieldNode.setText(term);
return fieldNode;
} else if (severalTokensAtSamePosition || !(node instanceof QuotedFieldQueryNode)) {
if (positionCount == 1 || !(node instanceof QuotedFieldQueryNode)) {
if (positionCount == 1) {
// simple case: only one position, with synonyms
LinkedList<QueryNode> children = new LinkedList<>();
for (int i = 0; i < numTokens; i++) {
String term = null;
try {
boolean hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
} catch (IOException e) {
// safe to ignore, because we know the number of tokens
}
children.add(new FieldQueryNode(field, term, -1, -1));
}
return new GroupQueryNode(new SynonymQueryNode(children));
} else {
// multiple positions
QueryNode q = new BooleanQueryNode(Collections.<QueryNode>emptyList());
QueryNode currentQuery = null;
for (int i = 0; i < numTokens; i++) {
String term = null;
try {
boolean hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
} catch (IOException e) {
// safe to ignore, because we know the number of tokens
}
if (posIncrAtt != null && posIncrAtt.getPositionIncrement() == 0) {
if (!(currentQuery instanceof BooleanQueryNode)) {
QueryNode t = currentQuery;
currentQuery = new SynonymQueryNode(Collections.<QueryNode>emptyList());
((BooleanQueryNode) currentQuery).add(t);
}
((BooleanQueryNode) currentQuery).add(new FieldQueryNode(field, term, -1, -1));
} else {
if (currentQuery != null) {
if (this.defaultOperator == Operator.OR) {
q.add(currentQuery);
} else {
q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
}
}
currentQuery = new FieldQueryNode(field, term, -1, -1);
}
}
if (this.defaultOperator == Operator.OR) {
q.add(currentQuery);
} else {
q.add(new ModifierQueryNode(currentQuery, Modifier.MOD_REQ));
}
if (q instanceof BooleanQueryNode) {
q = new GroupQueryNode(q);
}
return q;
}
} else {
// phrase query:
MultiPhraseQueryNode mpq = new MultiPhraseQueryNode();
List<FieldQueryNode> multiTerms = new ArrayList<>();
int position = -1;
int i = 0;
int termGroupCount = 0;
for (; i < numTokens; i++) {
String term = null;
int positionIncrement = 1;
try {
boolean hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
if (posIncrAtt != null) {
positionIncrement = posIncrAtt.getPositionIncrement();
}
} catch (IOException e) {
// safe to ignore, because we know the number of tokens
}
if (positionIncrement > 0 && multiTerms.size() > 0) {
for (FieldQueryNode termNode : multiTerms) {
if (this.positionIncrementsEnabled) {
termNode.setPositionIncrement(position);
} else {
termNode.setPositionIncrement(termGroupCount);
}
mpq.add(termNode);
}
// Only increment once for each "group" of
// terms that were in the same position:
termGroupCount++;
multiTerms.clear();
}
position += positionIncrement;
multiTerms.add(new FieldQueryNode(field, term, -1, -1));
}
for (FieldQueryNode termNode : multiTerms) {
if (this.positionIncrementsEnabled) {
termNode.setPositionIncrement(position);
} else {
termNode.setPositionIncrement(termGroupCount);
}
mpq.add(termNode);
}
return mpq;
}
} else {
TokenizedPhraseQueryNode pq = new TokenizedPhraseQueryNode();
int position = -1;
for (int i = 0; i < numTokens; i++) {
String term = null;
int positionIncrement = 1;
try {
boolean hasNext = buffer.incrementToken();
assert hasNext == true;
term = termAtt.toString();
if (posIncrAtt != null) {
positionIncrement = posIncrAtt.getPositionIncrement();
}
} catch (IOException e) {
// safe to ignore, because we know the number of tokens
}
FieldQueryNode newFieldNode = new FieldQueryNode(field, term, -1, -1);
if (this.positionIncrementsEnabled) {
position += positionIncrement;
newFieldNode.setPositionIncrement(position);
} else {
newFieldNode.setPositionIncrement(i);
}
pq.add(newFieldNode);
}
return pq;
}
} finally {
if (buffer != null) {
try {
buffer.close();
} catch (IOException e) {
// safe to ignore
}
}
}
}
return node;
}
Aggregations