SOLR-6304 wildcard fix

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1617424 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Noble Paul 2014-08-12 07:00:55 +00:00
parent e9ae2ca249
commit f6d0fe58ed
2 changed files with 41 additions and 11 deletions

View File

@ -98,7 +98,7 @@ public class JsonRecordReader {
if ("".equals(paths.get(0).trim())) if ("".equals(paths.get(0).trim()))
paths.remove(0); paths.remove(0);
rootNode.build(paths, fieldName, multiValued, isRecord, path); rootNode.build(paths, fieldName, multiValued, isRecord, path);
// rootNode.buildOptimise(null); rootNode.buildOptimize();
} }
/** /**
@ -154,8 +154,6 @@ public class JsonRecordReader {
String fieldName; // the fieldname in the emitted record (key of the map) String fieldName; // the fieldname in the emitted record (key of the map)
String splitPath; // the full path from the forEach entity attribute String splitPath; // the full path from the forEach entity attribute
final LinkedHashMap<String ,Node> childNodes = new LinkedHashMap<>(); // List of immediate child Nodes of this node final LinkedHashMap<String ,Node> childNodes = new LinkedHashMap<>(); // List of immediate child Nodes of this node
List<Node> wildCardNodes; // List of '//' style decendants of this Node
Node wildAncestor; // ancestor Node containing '//' style decendants
Node parent; // parent Node in the tree Node parent; // parent Node in the tree
boolean isLeaf = false; // flag: store/emit streamed text for this node boolean isLeaf = false; // flag: store/emit streamed text for this node
boolean isRecord = false; //flag: this Node starts a new record boolean isRecord = false; //flag: this Node starts a new record
@ -179,14 +177,13 @@ public class JsonRecordReader {
/** /**
* Walk the Node tree propagating any wildDescentant information to * Walk the Node tree propagating any wildDescentant information to
* child nodes. This allows us to optimise the performance of the * child nodes.
* main getInst method.
*/ */
private void buildOptimise(Node wa) { private void buildOptimize() {
wildAncestor = wa; if(parent != null && parent.recursiveWildCardChild !=null && this.recursiveWildCardChild ==null){
if (wildCardNodes != null) wa = this; this.recursiveWildCardChild = parent.recursiveWildCardChild;
if (childNodes != null) }
for (Node n : childNodes.values()) n.buildOptimise(wa); for (Node n : childNodes.values()) n.buildOptimize();
} }
static final String WILDCARD_PATH = "*"; static final String WILDCARD_PATH = "*";
static final String RECURSIVE_WILDCARD_PATH = "**"; static final String RECURSIVE_WILDCARD_PATH = "**";
@ -259,7 +256,7 @@ public class JsonRecordReader {
* deep-copied for thread safety * deep-copied for thread safety
*/ */
private static Map<String, Object> getDeepCopy(Map<String, Object> values) { private static Map<String, Object> getDeepCopy(Map<String, Object> values) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : values.entrySet()) { for (Map.Entry<String, Object> entry : values.entrySet()) {
if (entry.getValue() instanceof List) { if (entry.getValue() instanceof List) {
result.put(entry.getKey(), new ArrayList((List) entry.getValue())); result.put(entry.getKey(), new ArrayList((List) entry.getValue()));

View File

@ -156,4 +156,37 @@ public class TestJsonRecordReader extends SolrTestCaseJ4 {
} }
public void testRecursiveWildcard2() throws Exception{
String json = "{\n" +
" \"first\": \"John\",\n" +
" \"last\": \"Doe\",\n" +
" \"grade\": 8,\n" +
" \"exams\": [\n" +
" {\n" +
" \"subject\": \"Maths\",\n" +
" \"test\" : \"term1\",\n" +
" \"marks\":90},\n" +
" {\n" +
" \"subject\": \"Biology\",\n" +
" \"test\" : \"term1\",\n" +
" \"marks\":86}\n" +
" ]\n" +
"}";
JsonRecordReader streamer;
List<Map<String, Object>> records;
streamer = JsonRecordReader.getInst("/exams", Collections.singletonList("/**"));
records = streamer.getAllRecords(new StringReader(json));
assertEquals(2, records.size());
for (Map<String, Object> record : records) {
assertEquals(6,record.size());
}
streamer = JsonRecordReader.getInst("/", Collections.singletonList("txt:/**"));
records = streamer.getAllRecords(new StringReader(json));
assertEquals(1, records.size());
assertEquals(9, ((List)records.get(0).get("txt")).size() );
}
} }