org.apache.solr.search.join.ChildFieldValueSourceParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* 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.search.join;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.LeafFieldComparator;
import org.apache.lucene.search.Pruning;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.search.join.QueryBitSetProducer;
import org.apache.lucene.search.join.ToParentBlockJoinSortField;
import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.FunctionQParser;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.ValueSourceParser;
import org.apache.solr.search.join.BlockJoinParentQParser.AllParentsAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ChildFieldValueSourceParser extends ValueSourceParser {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final class BlockJoinSortFieldValueSource extends ValueSource {
private static final class BytesToStringComparator extends FieldComparator {
private final FieldComparator byteRefs;
private BytesToStringComparator(FieldComparator byteRefs) {
this.byteRefs = byteRefs;
}
@Override
public int compare(int slot1, int slot2) {
return byteRefs.compare(slot1, slot2);
}
@Override
public void setTopValue(String value) {
byteRefs.setTopValue(new BytesRef(value));
}
@Override
public String value(int slot) {
final BytesRef value = byteRefs.value(slot);
return value != null ? value.utf8ToString() : null;
}
@Override
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
return byteRefs.getLeafComparator(context);
}
}
private final BitSetProducer childFilter;
private final BitSetProducer parentFilter;
private final SchemaField childField;
private BlockJoinSortFieldValueSource(
BitSetProducer childFilter, BitSetProducer parentFilter, SchemaField childField) {
this.childFilter = childFilter;
this.parentFilter = parentFilter;
this.childField = childField;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((childField == null) ? 0 : childField.hashCode());
result = prime * result + ((childFilter == null) ? 0 : childFilter.hashCode());
result = prime * result + ((parentFilter == null) ? 0 : parentFilter.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof BlockJoinSortFieldValueSource)) return false;
BlockJoinSortFieldValueSource other = (BlockJoinSortFieldValueSource) obj;
return Objects.equals(childField, other.childField)
&& Objects.equals(childFilter, other.childFilter)
&& Objects.equals(parentFilter, other.parentFilter);
}
@Override
public String toString() {
return "BlockJoinSortFieldValueSource [childFilter="
+ childFilter
+ ", parentFilter="
+ parentFilter
+ ", childField="
+ childField
+ "]";
}
@Override
public SortField getSortField(boolean reverse) {
final Type type = childField.getSortField(reverse).getType();
return new ToParentBlockJoinSortField(
childField.getName(), type, reverse, parentFilter, childFilter) {
@SuppressWarnings("unchecked")
@Override
public FieldComparator getComparator(int numHits, Pruning pruning) {
final FieldComparator comparator = super.getComparator(numHits, pruning);
return type == Type.STRING
? new BytesToStringComparator((FieldComparator) comparator)
: comparator;
}
};
}
@Override
public String description() {
return NAME + " for " + childField.getName() + " of " + query(childFilter);
}
private String query(BitSetProducer bits) {
return (bits instanceof QueryBitSetProducer)
? ((QueryBitSetProducer) bits).getQuery().toString()
: bits.toString();
}
@Override
public FunctionValues getValues(Map