use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class MultiPhraseQueryNodeBuilder method build.
@Override
public MultiPhraseQuery build(QueryNode queryNode) throws QueryNodeException {
MultiPhraseQueryNode phraseNode = (MultiPhraseQueryNode) queryNode;
MultiPhraseQuery.Builder phraseQueryBuilder = new MultiPhraseQuery.Builder();
List<QueryNode> children = phraseNode.getChildren();
if (children != null) {
TreeMap<Integer, List<Term>> positionTermMap = new TreeMap<>();
for (QueryNode child : children) {
FieldQueryNode termNode = (FieldQueryNode) child;
TermQuery termQuery = (TermQuery) termNode.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
List<Term> termList = positionTermMap.get(termNode.getPositionIncrement());
if (termList == null) {
termList = new LinkedList<>();
positionTermMap.put(termNode.getPositionIncrement(), termList);
}
termList.add(termQuery.getTerm());
}
for (int positionIncrement : positionTermMap.keySet()) {
List<Term> termList = positionTermMap.get(positionIncrement);
phraseQueryBuilder.add(termList.toArray(new Term[termList.size()]), positionIncrement);
}
}
return phraseQueryBuilder.build();
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class SpanOrQueryNodeBuilder method build.
@Override
public SpanOrQuery build(QueryNode node) throws QueryNodeException {
// validates node
BooleanQueryNode booleanNode = (BooleanQueryNode) node;
List<QueryNode> children = booleanNode.getChildren();
SpanQuery[] spanQueries = new SpanQuery[children.size()];
int i = 0;
for (QueryNode child : children) {
spanQueries[i++] = (SpanQuery) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
}
return new SpanOrQuery(spanQueries);
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class QueryParserHelper method parse.
/**
* Parses a query string to an object, usually some query object.<br>
* <br>
* In this method the three phases are executed: <br>
* <br>
* 1st - the query string is parsed using the
* text parser returned by {@link #getSyntaxParser()}, the result is a query
* node tree <br>
* <br>
* 2nd - the query node tree is processed by the
* processor returned by {@link #getQueryNodeProcessor()} <br>
* <br>
* 3th - a object is built from the query node
* tree using the builder returned by {@link #getQueryBuilder()}
*
* @param query
* the query string
* @param defaultField
* the default field used by the text parser
*
* @return the object built from the query
*
* @throws QueryNodeException
* if something wrong happens along the three phases
*/
public Object parse(String query, String defaultField) throws QueryNodeException {
QueryNode queryTree = getSyntaxParser().parse(query, defaultField);
QueryNodeProcessor processor = getQueryNodeProcessor();
if (processor != null) {
queryTree = processor.process(queryTree);
}
return getQueryBuilder().build(queryTree);
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class QueryTreeBuilder method process.
private void process(QueryNode node) throws QueryNodeException {
if (node != null) {
QueryBuilder builder = getBuilder(node);
if (!(builder instanceof QueryTreeBuilder)) {
List<QueryNode> children = node.getChildren();
if (children != null) {
for (QueryNode child : children) {
process(child);
}
}
}
processNode(node, builder);
}
}
use of org.apache.lucene.queryparser.flexible.core.nodes.QueryNode in project lucene-solr by apache.
the class QueryNodeProcessorImpl method processChildren.
/**
* This method is called every time a child is processed.
*
* @param queryTree
* the query node child to be processed
* @throws QueryNodeException
* if something goes wrong during the query node processing
*/
protected void processChildren(QueryNode queryTree) throws QueryNodeException {
List<QueryNode> children = queryTree.getChildren();
ChildrenList newChildren;
if (children != null && children.size() > 0) {
newChildren = allocateChildrenList();
try {
for (QueryNode child : children) {
child = processIteration(child);
if (child == null) {
throw new NullPointerException();
}
newChildren.add(child);
}
List<QueryNode> orderedChildrenList = setChildrenOrder(newChildren);
queryTree.set(orderedChildrenList);
} finally {
newChildren.beingUsed = false;
}
}
}
Aggregations