Github user moshebla commented on a diff in the pull request:

    https://github.com/apache/lucene-solr/pull/416#discussion_r204227785
  
    --- Diff: 
solr/core/src/java/org/apache/solr/response/transform/DeeplyNestedChildTransformerBase.java
 ---
    @@ -0,0 +1,139 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.solr.response.transform;
    +
    +import java.util.ArrayList;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Objects;
    +import java.util.stream.Stream;
    +
    +import org.apache.lucene.index.IndexableField;
    +import org.apache.lucene.search.Sort;
    +import org.apache.lucene.search.SortField;
    +import org.apache.lucene.search.join.BitSetProducer;
    +import org.apache.lucene.util.BitSet;
    +import org.apache.solr.common.SolrDocument;
    +import org.apache.solr.request.SolrQueryRequest;
    +import org.apache.solr.schema.FieldType;
    +import org.apache.solr.schema.IndexSchema;
    +import org.apache.solr.schema.SchemaField;
    +
    +import static 
org.apache.solr.response.transform.DeeplyNestedChildDocTransformerFactory.NUM_SEP_CHAR;
    +import static org.apache.solr.schema.IndexSchema.NEST_PATH_FIELD_NAME;
    +
    +/**
    + *
    + * This base class helps create a child doc transformer which caches the 
parent query using QueryBitProducer
    + *
    + *
    + * "limit" param which provides an option to specify the number of child 
documents
    + * to be returned per parent document. By default it's set to 10.
    + *
    + * @see org.apache.solr.response.transform.DeeplyNestedChildDocTransformer
    + * @see 
org.apache.solr.response.transform.DeeplyNestedFilterChildDocTransformer
    + */
    +
    +abstract class DeeplyNestedChildDocTransformerBase extends DocTransformer {
    +  private final String name;
    +  protected final SchemaField idField;
    +  protected final SolrQueryRequest req;
    +  protected final IndexSchema schema;
    +  protected BitSetProducer parentsFilter;
    +  protected BitSet parents;
    +  protected int limit;
    +  protected final Sort pathKeySort;
    +
    +  public DeeplyNestedChildDocTransformerBase( String name, final 
BitSetProducer parentsFilter,
    +                                          final SolrQueryRequest req, int 
limit) {
    +    this.name = name;
    +    this.schema = req.getSchema();
    +    this.idField = this.schema.getUniqueKeyField();
    +    this.req = req;
    +    this.parentsFilter = parentsFilter;
    +    this.limit = limit;
    +    this.pathKeySort = new Sort(new SortField(NEST_PATH_FIELD_NAME, 
SortField.Type.STRING, false),
    +        new SortField(idField.getName(), SortField.Type.STRING, false));
    +  }
    +
    +  @Override
    +  public String getName()  {
    +    return name;
    +  }
    +
    +  @Override
    +  public String[] getExtraRequestFields() {
    +    // we always need the idField (of the parent) in order to fill out 
it's children
    +    return new String[] { idField.getName() };
    +  }
    +
    +  protected static SolrDocument getChildByPath(String[] pathAndNum, 
SolrDocument lastDoc) {
    +    List<Object> fieldsValues = (List<Object>) 
lastDoc.getFieldValues(pathAndNum[0]);
    +    int childIndex = Integer.parseInt(pathAndNum[1]);
    +    return fieldsValues.size() > childIndex ? (SolrDocument) 
fieldsValues.get(childIndex): null;
    +  }
    +
    +  protected static void addChild(SolrDocument parentDoc, String[] 
pathAndNum, SolrDocument cDoc) {
    +    if(!pathAndNum[1].equals("") && (parentDoc.get(pathAndNum[0]) == 
null)) {
    +      parentDoc.setField(pathAndNum[0], new 
NullFilteringArrayList<SolrDocument>());
    +    }
    +    NullFilteringArrayList fieldValues = (NullFilteringArrayList) 
parentDoc.getFieldValues(pathAndNum[0]);
    +    int pathNum = Integer.parseInt(pathAndNum[1]);
    +
    +    fieldValues.addWithPlaceHolder(pathNum, cDoc);
    +  }
    +
    +  protected static String[] getPathAndNum(String lastPath) {
    +    return lastPath.split(NUM_SEP_CHAR);
    +  }
    +
    +  protected static String getSolrFieldString(Object fieldVal, FieldType 
fieldType) {
    +    return fieldVal instanceof IndexableField
    +        ? fieldType.toExternal((IndexableField)fieldVal)
    +        : fieldVal.toString();
    +  }
    +
    +  protected static class NullFilteringArrayList<T> extends ArrayList<T> {
    --- End diff --
    
    I used this since the paths in the documents are stored as array indexes. 
In case some documents from the array were filtered, I insert null values, so 
the child docs that matched the filter will be in the same array index as they 
were originally. Inside the iterator method, the null values are filtered, so 
they don't get written by the response writer.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to