mirror of https://github.com/apache/lucene.git
LUCENE-5099 - QueryNode should have the ability to detach from its node parent
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1560245 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1e5f5795dc
commit
97cf55d3dd
|
@ -104,6 +104,10 @@ New Features
|
|||
using SortField.setMissingValue(SortField.STRING_FIRST), or last,
|
||||
using SortField.setMissingValue(SortField.STRING_LAST). (Rob Muir,
|
||||
Mike McCandless)
|
||||
|
||||
* LUCENE-5099: QueryNode should have the ability to detach from its node
|
||||
parent. Added QueryNode.removeFromParent() that allows nodes to be
|
||||
detached from its parent node. (Adriano Crestani)
|
||||
|
||||
|
||||
Build
|
||||
|
|
|
@ -91,4 +91,8 @@ public interface QueryNode {
|
|||
*/
|
||||
public Map<String, Object> getTagMap();
|
||||
|
||||
/**
|
||||
* Removes this query node from its parent.
|
||||
*/
|
||||
public void removeFromParent();
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.apache.lucene.queryparser.flexible.core.nodes;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
@ -102,18 +103,19 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
|
|||
|
||||
// reset parent value
|
||||
for (QueryNode child : children) {
|
||||
|
||||
((QueryNodeImpl) child).setParent(null);
|
||||
|
||||
child.removeFromParent();
|
||||
}
|
||||
|
||||
|
||||
ArrayList<QueryNode> existingChildren = new ArrayList<QueryNode>(getChildren());
|
||||
for (QueryNode existingChild : existingChildren) {
|
||||
existingChild.removeFromParent();
|
||||
}
|
||||
|
||||
// allocate new children list
|
||||
allocate();
|
||||
|
||||
|
||||
// add new children and set parent
|
||||
for (QueryNode child : children) {
|
||||
add(child);
|
||||
}
|
||||
add(children);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -154,7 +156,7 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
|
|||
if (isLeaf() || this.clauses == null) {
|
||||
return null;
|
||||
}
|
||||
return this.clauses;
|
||||
return new ArrayList<QueryNode>(this.clauses);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -181,7 +183,10 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
|
|||
private QueryNode parent = null;
|
||||
|
||||
private void setParent(QueryNode parent) {
|
||||
this.parent = parent;
|
||||
if (this.parent != parent) {
|
||||
this.removeFromParent();
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -241,5 +246,21 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
|
|||
public Map<String, Object> getTagMap() {
|
||||
return (Map<String, Object>) this.tags.clone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeFromParent() {
|
||||
if (this.parent != null) {
|
||||
List<QueryNode> parentChildren = this.parent.getChildren();
|
||||
Iterator<QueryNode> it = parentChildren.iterator();
|
||||
|
||||
while (it.hasNext()) {
|
||||
if (it.next() == this) {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
|
||||
this.parent = null;
|
||||
}
|
||||
}
|
||||
|
||||
} // end class QueryNodeImpl
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.lucene.queryparser.flexible.core.nodes;
|
|||
*/
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
|
@ -44,4 +45,23 @@ public class TestQueryNode extends LuceneTestCase {
|
|||
|
||||
}
|
||||
|
||||
/* LUCENE-5099 - QueryNodeProcessorImpl should set parent to null before returning on processing */
|
||||
public void testRemoveFromParent() throws Exception {
|
||||
BooleanQueryNode booleanNode = new BooleanQueryNode(Collections.<QueryNode>emptyList());
|
||||
FieldQueryNode fieldNode = new FieldQueryNode("foo", "A", 0, 1);
|
||||
assertNull(fieldNode.getParent());
|
||||
|
||||
booleanNode.add(fieldNode);
|
||||
assertNotNull(fieldNode.getParent());
|
||||
|
||||
fieldNode.removeFromParent();
|
||||
assertNull(fieldNode.getParent());
|
||||
|
||||
booleanNode.add(fieldNode);
|
||||
assertNotNull(fieldNode.getParent());
|
||||
|
||||
booleanNode.set(Collections.<QueryNode>emptyList());
|
||||
assertNull(fieldNode.getParent());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.apache.lucene.queryparser.util;
|
|||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
@ -1066,6 +1065,7 @@ public abstract class QueryParserTestBase extends LuceneTestCase {
|
|||
assertTrue(bq.getClauses()[1].getQuery() instanceof MatchAllDocsQuery);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void assertHits(int expected, String query, IndexSearcher is) throws Exception {
|
||||
String oldDefaultField = getDefaultField();
|
||||
setDefaultField("date");
|
||||
|
|
Loading…
Reference in New Issue