Java Code Examples for org.apache.solr.schema.SchemaField#multiValued()

The following examples show how to use org.apache.solr.schema.SchemaField#multiValued() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcatFieldUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public FieldMutatingUpdateProcessor.FieldNameSelector 
  getDefaultSelector(final SolrCore core) {

  return fieldName -> {
    final IndexSchema schema = core.getLatestSchema();

    // first check type since it should be fastest
    FieldType type = schema.getFieldTypeNoEx(fieldName);
    if (null == type) return false;

    if (! (TextField.class.isInstance(type)
           || StrField.class.isInstance(type))) {
      return false;
    }

    // only ask for SchemaField if we passed the type check.
    SchemaField sf = schema.getFieldOrNull(fieldName);
    // shouldn't be null since since type wasn't, but just in case
    if (null == sf) return false;

    return ! sf.multiValued();
  };
}
 
Example 2
Source File: TestRetrieveFieldsOptimizer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
IndexSchema addFields(IndexSchema schema) {

    List<SchemaField> fieldsToAdd = new ArrayList<>();

    for (RetrieveField field : fields.values()) {
      allFields.add(field);
      SchemaField schemaField = field.schemaField;
      fieldsToAdd.add(schemaField);
      if (schemaField.multiValued()) {
        multiValuedFields.add(field);
      }
      if (schemaField.hasDocValues() && schemaField.stored() == false) {
        dvNotStoredFields.add(field);
      }
      if (schemaField.hasDocValues() == false && schemaField.stored()) {
        storedNotDvFields.add(field);
      }
      if (schemaField.hasDocValues() && schemaField.stored()) {
        storedAndDvFields.add(field);
      }
      if (schemaField.stored() && schemaField.multiValued()) {
        storedMvFields.add(field);
      }
    }
    return schema.addFields(fieldsToAdd, Collections.emptyMap(), false);
  }
 
Example 3
Source File: UniqueBlockFieldAgg.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  final String fieldName = getArg();
  SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(fieldName);
  if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
    throw new IllegalArgumentException(name+"("+fieldName+
        ") doesn't allow multivalue fields, got " + sf);
  } else {
    if (sf.getType().getNumberType() != null) {
      throw new IllegalArgumentException(name+"("+fieldName+
          ") not yet support numbers " + sf);
    } else {
      return new UniqueBlockSlotAcc(fcontext, sf, numSlots);
    }
  }
}
 
Example 4
Source File: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private static SolrDocument toSolrDoc(Document doc, IndexSchema schema) {
  SolrDocument out = new SolrDocument();
  for ( IndexableField f : doc.getFields() ) {
    // Make sure multivalued fields are represented as lists
    Object existing = out.get(f.name());
    if (existing == null) {
      SchemaField sf = schema.getFieldOrNull(f.name());

      // don't return copyField targets
      if (sf != null && schema.isCopyFieldTarget(sf)) continue;

      if (sf != null && sf.multiValued()) {
        List<Object> vals = new ArrayList<>();
        vals.add( f );
        out.setField( f.name(), vals );
      }
      else{
        out.setField( f.name(), f );
      }
    }
    else {
      out.addField( f.name(), f );
    }
  }
  return out;
}
 
Example 5
Source File: CountValsAgg.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  ValueSource vs = getArg();
  if (vs instanceof FieldNameValueSource) {
    String field = ((FieldNameValueSource)vs).getFieldName();
    SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);
    if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
      if (sf.hasDocValues()) {
        if (sf.getType().isPointField()) {
          return new CountSortedNumericDVAcc(fcontext, sf, numSlots);
        }
        return new CountSortedSetDVAcc(fcontext, sf, numSlots);
      }
      if (sf.getType().isPointField()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "'countvals' aggregation not supported for PointField without docValues");
      }
      return new CountMultiValuedAcc(fcontext, sf, numSlots);
    } else {
      vs = sf.getType().getValueSource(sf, null);
    }
  }
  return new CountValSlotAcc(vs, fcontext, numSlots);
}
 
Example 6
Source File: UniqueAgg.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(getArg());
  if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
    if (sf.getType().isPointField()) {
      return new SortedNumericAcc(fcontext, getArg(), numSlots);
    } else if (sf.hasDocValues()) {
      return new UniqueMultiDvSlotAcc(fcontext, sf, numSlots, null);
    } else {
      return new UniqueMultivaluedSlotAcc(fcontext, sf, numSlots, null);
    }
  } else {
    if (sf.getType().getNumberType() != null) {
      return new NumericAcc(fcontext, getArg(), numSlots);
    } else {
      return new UniqueSinglevaluedSlotAcc(fcontext, sf, numSlots, null);
    }
  }
}
 
Example 7
Source File: VersionInfo.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Gets and returns the {@link org.apache.solr.common.params.CommonParams#VERSION_FIELD} from the specified
 * schema, after verifying that it is indexed, stored, and single-valued.  
 * If any of these pre-conditions are not met, it throws a SolrException 
 * with a user suitable message indicating the problem.
 */
public static SchemaField getAndCheckVersionField(IndexSchema schema) 
  throws SolrException {
  final String errPrefix = VERSION_FIELD + " field must exist in schema and be searchable (indexed or docValues) and retrievable(stored or docValues) and not multiValued";
  SchemaField sf = schema.getFieldOrNull(VERSION_FIELD);

  if (null == sf) {
    throw new SolrException
      (SolrException.ErrorCode.SERVER_ERROR, 
       errPrefix + " (" + VERSION_FIELD + " does not exist)");
  }
  if ( !sf.indexed() && !sf.hasDocValues()) {
    throw new SolrException
      (SolrException.ErrorCode.SERVER_ERROR, 
       errPrefix + " (" + VERSION_FIELD + " not searchable");
  }
  if ( !sf.stored() && !sf.hasDocValues()) {
    throw new SolrException
      (SolrException.ErrorCode.SERVER_ERROR, 
       errPrefix + " (" + VERSION_FIELD + " not retrievable");
  }
  if ( sf.multiValued() ) {
    throw new SolrException
      (SolrException.ErrorCode.SERVER_ERROR, 
       errPrefix + " (" + VERSION_FIELD + " is multiValued");
  }
  
  return sf;
}
 
Example 8
Source File: SumsqAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  ValueSource vs = getArg();

  if (vs instanceof FieldNameValueSource) {
    String field = ((FieldNameValueSource)vs).getFieldName();
    SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);
    if (sf.getType().getNumberType() == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          name() + " aggregation not supported for " + sf.getType().getTypeName());
    }
    if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
      if (sf.hasDocValues()) {
        if (sf.getType().isPointField()) {
          return new SumSqSortedNumericAcc(fcontext, sf, numSlots);
        }
        return new SumSqSortedSetAcc(fcontext, sf, numSlots);
      }
      if (sf.getType().isPointField()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            name() + " aggregation not supported for PointField w/o docValues");
      }
      return new SumSqUnInvertedFieldAcc(fcontext, sf, numSlots);
    }
    vs = sf.getType().getValueSource(sf, null);
  }
  return new SlotAcc.SumsqSlotAcc(vs, fcontext, numSlots);
}
 
Example 9
Source File: DocBasedVersionConstraintsProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void inform(SolrCore core) {

  if (core.getUpdateHandler().getUpdateLog() == null) {
    throw new SolrException(SERVER_ERROR,
        "updateLog must be enabled.");
  }

  if (core.getLatestSchema().getUniqueKeyField() == null) {
    throw new SolrException(SERVER_ERROR,
        "schema must have uniqueKey defined.");
  }

  useFieldCache = true;
  for (String versionField : versionFields) {
    SchemaField userVersionField = core.getLatestSchema().getField(versionField);
    if (userVersionField == null || !userVersionField.stored() || userVersionField.multiValued()) {
      throw new SolrException(SERVER_ERROR,
          "field " + versionField + " must be defined in schema, be stored, and be single valued.");
    }
    if (useFieldCache) {
      try {
        userVersionField.getType().getValueSource(userVersionField, null);
      } catch (Exception e) {
        useFieldCache = false;
        log.warn("Can't use fieldcache/valuesource: {}", e.getMessage());
      }
    }
  }
  
  canCreateTombstoneDocument(core.getLatestSchema());
}
 
Example 10
Source File: SolrDocumentFetcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean canSubstituteDvForStored(FieldInfo fieldInfo, SchemaField schemaField) {
  if (!schemaField.hasDocValues() || !schemaField.stored()) return false;
  if (schemaField.multiValued()) return false;
  DocValuesType docValuesType = fieldInfo.getDocValuesType();
  NumberType numberType = schemaField.getType().getNumberType();
  // can not decode a numeric without knowing its numberType
  if (numberType == null && (docValuesType == DocValuesType.SORTED_NUMERIC || docValuesType == DocValuesType.NUMERIC)) {
    return false;
  }
  return true;
}
 
Example 11
Source File: PercentileAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  ValueSource vs = getArg();

  if (vs instanceof FieldNameValueSource) {
    String field = ((FieldNameValueSource) vs).getFieldName();
    SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);
    if (sf.getType().getNumberType() == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          name() + " aggregation not supported for " + sf.getType().getTypeName());
    }
    if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
      if (sf.hasDocValues()) {
        if (sf.getType().isPointField()) {
          return new PercentileSortedNumericAcc(fcontext, sf, numSlots);
        }
        return new PercentileSortedSetAcc(fcontext, sf, numSlots);
      }
      if (sf.getType().isPointField()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            name() + " aggregation not supported for PointField w/o docValues");
      }
      return new PercentileUnInvertedFieldAcc(fcontext, sf, numSlots);
    }
    vs = sf.getType().getValueSource(sf, null);
  }
  return new Acc(vs, fcontext, numSlots);
}
 
Example 12
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public Map<String,Object> toObject(IndexSchema schema) {
  Map<String,Object> result = new HashMap<>();
  for (Fld fld : fields) {
    SchemaField sf = schema.getField(fld.ftype.fname);
    if (!sf.multiValued()) {
      result.put(fld.ftype.fname, fld.vals.get(0));
    } else {
      result.put(fld.ftype.fname, fld.vals);
    }
  }
  return result;
}
 
Example 13
Source File: AvgAgg.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SlotAcc createSlotAcc(FacetContext fcontext, long numDocs, int numSlots) throws IOException {
  ValueSource vs = getArg();

  if (vs instanceof FieldNameValueSource) {
    String field = ((FieldNameValueSource) vs).getFieldName();
    SchemaField sf = fcontext.qcontext.searcher().getSchema().getField(field);
    if (sf.getType().getNumberType() == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          name() + " aggregation not supported for " + sf.getType().getTypeName());
    }
    if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
      if (sf.hasDocValues()) {
        if (sf.getType().isPointField()) {
          return new AvgSortedNumericAcc(fcontext, sf, numSlots);
        }
        return new AvgSortedSetAcc(fcontext, sf, numSlots);
      }
      if (sf.getType().isPointField()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            name() + " aggregation not supported for PointField w/o docValues");
      }
      return new AvgUnInvertedFieldAcc(fcontext, sf, numSlots);
    }
    vs = sf.getType().getValueSource(sf, null);
  }
  return new SlotAcc.AvgSlotAcc(vs, fcontext, numSlots);
}
 
Example 14
Source File: SolrTestCaseHS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Map<String,Object> toObject(Doc doc, IndexSchema schema, Collection<String> fieldNames) {
  Map<String,Object> result = new HashMap<>();
  for (Fld fld : doc.fields) {
    if (fieldNames != null && !fieldNames.contains(fld.ftype.fname)) continue;
    SchemaField sf = schema.getField(fld.ftype.fname);
    if (!sf.multiValued()) {
      result.put(fld.ftype.fname, fld.vals.get(0));
    } else {
      result.put(fld.ftype.fname, fld.vals);
    }
  }
  return result;
}
 
Example 15
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
private String getFilterQuery( ResponseBuilder rb, String fieldName, ArrayList<String> valList,
                               int[] termPosRange, ArrayList<char[]> queryTokens, String suffix) {
    
  if (fieldName.indexOf( fieldDelim ) > 0) {
    return getFilterQuery( rb, fieldName.split( fieldSplitExpr ), valList, termPosRange, queryTokens, suffix );
  }
  if (valList.size() == 1) {
    // check if valList[0] is multi-term - if so, check if there is a single term equivalent
    // if this returns non-null, create an OR query with single term version
    // example "white linen perfume" vs "white linen shirt"  where "White Linen" is a brand
    String term = valList.get( 0 );

    if (term.indexOf( " " ) > 0) {
      String singleTermQuery = getSingleTermQuery( term );
      if (singleTermQuery != null) {
        StringBuilder strb = new StringBuilder( );
        strb.append( "(" ).append( fieldName ).append( ":" )
            .append( term ).append( " OR (" ).append( singleTermQuery ).append( "))" ).append( suffix );
        Log.debug( "returning composite query: " + strb.toString( ) );
        return strb.toString( );
      }
    }

    String query = fieldName + ":" + term + suffix;
    Log.debug( "returning single query: " + query );
    return query;
  }
  else {
    // Check if it is a MultiValued Field - if so, use AND internally
    SolrIndexSearcher searcher = rb.req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    SchemaField field = schema.getField(fieldName);
      
    boolean useAnd = field.multiValued() && useAndForMultiValuedFields;
    // if query has 'or' in it and or is at a position 'within' the values for this field ...
    if (useAnd) {
      for (int i = termPosRange[0] + 1; i < termPosRange[1]; i++ ) {
        char[] qToken = queryTokens.get( i );
        if (qToken.length == 2 && qToken[0] == 'o' && qToken[1] == 'r' ) {
          useAnd = false;
          break;
        }
      }
    }
      
    StringBuilder orQ = new StringBuilder( );
    for (String val : valList ) {
      if (orQ.length() > 0) orQ.append( (useAnd ? " AND " : " OR ") );
      orQ.append( val );
    }
      
    String fq = fieldName + ":(" + orQ.toString() + ")" + suffix;
    Log.debug( "fq = " + fq );
    return fq;
  }
}
 
Example 16
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
private String getFilterQuery( ResponseBuilder rb, String fieldName, ArrayList<String> valList,
                               int[] termPosRange, ArrayList<char[]> queryTokens, String suffix) {
  if (fieldName.indexOf( fieldDelim ) > 0) {
    return getFilterQuery( rb, fieldName.split( fieldSplitExpr ), valList, termPosRange, queryTokens, suffix );
  }
  if (valList.size() == 1) {
    // check if valList[0] is multi-term - if so, check if there is a single term equivalent
    // if this returns non-null, create an OR query with single term version
    // example "white linen perfume" vs "white linen shirt"  where "White Linen" is a brand
    String term = valList.get( 0 );
      
    if (term.indexOf( " " ) > 0) {
      String singleTermQuery = getSingleTermQuery( term );
      if (singleTermQuery != null) {
        StringBuilder strb = new StringBuilder( );
        // EH: possible meta-escaping problem if value includes {!field f=<fieldName>}value
        strb.append( "(" ).append( fieldName ).append( ":" )
            .append( term ).append( " OR (" ).append( singleTermQuery ).append( "))" ).append( suffix );
        Log.debug( "returning composite query: " + strb.toString( ) );
        return strb.toString( );
      }
    }
      
    String query = fieldName + ":" + term + suffix;
    Log.debug( "returning single query: " + query );
    return query;
  }
  else {
    SolrIndexSearcher searcher = rb.req.getSearcher();
    IndexSchema schema = searcher.getSchema();
    SchemaField field = schema.getField(fieldName);
    boolean useAnd = field.multiValued() && useAndForMultiValuedFields;
    // if query has 'or' in it and or is at a position 'within' the values for this field ...
    if (useAnd) {
      for (int i = termPosRange[0] + 1; i < termPosRange[1]; i++ ) {
        char[] qToken = queryTokens.get( i );
        // is the token 'or'?
        if (qToken.length == 2 && qToken[0] == 'o' && qToken[1] == 'r' ) {
          useAnd = false;
          break;
        }
      }
    }
      
    StringBuilder orQ = new StringBuilder( );
    for (String val : valList ) {
      if (orQ.length() > 0) orQ.append( (useAnd ? " AND " : " OR ") );
      orQ.append( val );
    }
    return fieldName + ":(" + orQ.toString() + ")" + suffix;
  }
}
 
Example 17
Source File: FacetField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public FacetProcessor createFacetProcessor(FacetContext fcontext) {
  SchemaField sf = fcontext.searcher.getSchema().getField(field);
  FieldType ft = sf.getType();
  boolean multiToken = sf.multiValued() || ft.multiValuedFieldCache();

  if (fcontext.facetInfo != null) {
    // refinement... we will end up either skipping the entire facet, or doing calculating only specific facet buckets
    if (multiToken && !sf.hasDocValues() && method!=FacetMethod.DV && sf.isUninvertible()) {
      // Match the access method from the first phase.
      // It won't always matter, but does currently for an all-values bucket
      return new FacetFieldProcessorByArrayUIF(fcontext, this, sf);
    }
    return new FacetFieldProcessorByArrayDV(fcontext, this, sf);
  }

  NumberType ntype = ft.getNumberType();
  // ensure we can support the requested options for numeric faceting:
  if (ntype != null) {
    if (prefix != null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Doesn't make sense to set facet prefix on a numeric field");
    }
    if (mincount == 0) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Numeric fields do not support facet mincount=0; try indexing as terms");
      // TODO if indexed=true then we could add support
    }
  }

  // TODO auto-pick ENUM/STREAM SOLR-9351 when index asc and DocSet cardinality is *not* much smaller than term cardinality
  if (method == FacetMethod.ENUM) {// at the moment these two are the same
    method = FacetMethod.STREAM;
  }
  if (method == FacetMethod.STREAM && sf.indexed() && !ft.isPointField() &&
      // wether we can use stream processing depends on wether this is a shard request, wether
      // re-sorting has been requested, and if the effective sort during collection is "index asc"
      ( fcontext.isShard()
        // for a shard request, the effective per-shard sort must be index asc
        ? FacetSort.INDEX_ASC.equals(null == prelim_sort ? sort : prelim_sort)
        // for a non-shard request, we can only use streaming if there is no pre-sorting
        : (null == prelim_sort && FacetSort.INDEX_ASC.equals( sort ) ) ) ) {
        
    return new FacetFieldProcessorByEnumTermsStream(fcontext, this, sf);
  }

  // TODO if method=UIF and not single-valued numerics then simply choose that now? TODO add FieldType.getDocValuesType()

  if (!multiToken) {
    if (mincount > 0 && prefix == null && (ntype != null || method == FacetMethod.DVHASH)) {
      // TODO can we auto-pick for strings when term cardinality is much greater than DocSet cardinality?
      //   or if we don't know cardinality but DocSet size is very small
      return new FacetFieldProcessorByHashDV(fcontext, this, sf);
    } else if (ntype == null) {
      // single valued string...
      return new FacetFieldProcessorByArrayDV(fcontext, this, sf);
    } else {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
          "Couldn't pick facet algorithm for field " + sf);
    }
  }

  if (sf.hasDocValues() && sf.getType().isPointField()) {
    return new FacetFieldProcessorByHashDV(fcontext, this, sf);
  }

  // multi-valued after this point

  if (sf.hasDocValues() || method == FacetMethod.DV || !sf.isUninvertible()) {
    // single and multi-valued string docValues
    return new FacetFieldProcessorByArrayDV(fcontext, this, sf);
  }

  // Top-level multi-valued field cache (UIF)
  return new FacetFieldProcessorByArrayUIF(fcontext, this, sf);
}
 
Example 18
Source File: ReverseOrdFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes"})
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
  final int off = readerContext.docBase;
  final LeafReader r;
  Object o = context.get("searcher");
  if (o instanceof SolrIndexSearcher) {
    @SuppressWarnings("resource")  final SolrIndexSearcher is = (SolrIndexSearcher) o;
    SchemaField sf = is.getSchema().getFieldOrNull(field);
    if (sf != null && sf.getType().isPointField()) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "rord() is not supported over Points based field " + field);
    }
    if (sf != null && sf.hasDocValues() == false && sf.multiValued() == false && sf.getType().getNumberType() != null) {
      // it's a single-valued numeric field: we must currently create insanity :(
      List<LeafReaderContext> leaves = is.getIndexReader().leaves();
      LeafReader insaneLeaves[] = new LeafReader[leaves.size()];
      int upto = 0;
      for (LeafReaderContext raw : leaves) {
        insaneLeaves[upto++] = Insanity.wrapInsanity(raw.reader(), field);
      }
      r = SlowCompositeReaderWrapper.wrap(new MultiReader(insaneLeaves));
    } else {
      // reuse ordinalmap
      r = ((SolrIndexSearcher)o).getSlowAtomicReader();
    }
  } else {
    IndexReader topReader = ReaderUtil.getTopLevelContext(readerContext).reader();
    r = SlowCompositeReaderWrapper.wrap(topReader);
  }
  // if it's e.g. tokenized/multivalued, emulate old behavior of single-valued fc
  final SortedDocValues sindex = SortedSetSelector.wrap(DocValues.getSortedSet(r, field), SortedSetSelector.Type.MIN);
  final int end = sindex.getValueCount();

  return new IntDocValues(this) {
    @Override
    public int intVal(int doc) throws IOException {
      if (doc+off > sindex.docID()) {
        sindex.advance(doc+off);
      }
      if (doc+off == sindex.docID()) {
        return (end - sindex.ordValue() - 1);
      } else {
        return end;
      }
    }
  };
}
 
Example 19
Source File: ExpressionFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@link AnalyticsField} out of the given {@link SchemaField}.
 * <p>
 * Currently only fields with doc-values enabled are supported.
 *
 * @param field the field to convert for analytics
 * @return an analytics representation of the field
 * @throws SolrException if the field is not supported by the analytics framework
 */
private AnalyticsField createField(SchemaField field) throws SolrException {
  String fieldName = field.getName();
  if (fields.containsKey(fieldName)) {
    return fields.get(fieldName);
  }
  if (!field.hasDocValues()) {
    throw new SolrException(ErrorCode.BAD_REQUEST,"The field "+fieldName+" does not have docValues enabled.");
  }
  boolean multivalued = field.multiValued();
  FieldType fieldType = field.getType();
  AnalyticsField aField;
  if (fieldType instanceof BoolField) {
    if (multivalued) {
      aField = new BooleanMultiField(fieldName);
    } else {
      aField = new BooleanField(fieldName);
    }
  } else if (fieldType instanceof TrieIntField) {
    if (multivalued) {
      aField = new IntMultiTrieField(fieldName);
    } else {
      aField = new IntField(fieldName);
    }
  } else if (fieldType instanceof IntPointField) {
    if (multivalued) {
      aField = new IntMultiPointField(fieldName);
    } else {
      aField = new IntField(fieldName);
    }
  } else if (fieldType instanceof TrieLongField) {
    if (multivalued) {
      aField = new LongMultiTrieField(fieldName);
    } else {
      aField = new LongField(fieldName);
    }
  } else if (fieldType instanceof LongPointField) {
    if (multivalued) {
      aField = new LongMultiPointField(fieldName);
    } else {
      aField = new LongField(fieldName);
    }
  } else if (fieldType instanceof TrieFloatField) {
    if (multivalued) {
      aField = new FloatMultiTrieField(fieldName);
    } else {
      aField = new FloatField(fieldName);
    }
  } else if (fieldType instanceof FloatPointField) {
    if (multivalued) {
      aField = new FloatMultiPointField(fieldName);
    } else {
      aField = new FloatField(fieldName);
    }
  } else if (fieldType instanceof TrieDoubleField) {
    if (multivalued) {
      aField = new DoubleMultiTrieField(fieldName);
    } else {
      aField = new DoubleField(fieldName);
    }
  } else if (fieldType instanceof DoublePointField) {
    if (multivalued) {
      aField = new DoubleMultiPointField(fieldName);
    } else {
      aField = new DoubleField(fieldName);
    }
  } else if (fieldType instanceof TrieDateField) {
    if (multivalued) {
      aField = new DateMultiTrieField(fieldName);
    } else {
      aField = new DateField(fieldName);
    }
  } else if (fieldType instanceof DatePointField) {
    if (multivalued) {
      aField = new DateMultiPointField(fieldName);
    } else {
      aField = new DateField(fieldName);
    }
  } else if (fieldType instanceof StrField) {
    if (multivalued) {
      aField = new StringMultiField(fieldName);
    } else {
      aField = new StringField(fieldName);
    }
  } else {
    throw new SolrException(ErrorCode.BAD_REQUEST,"FieldType of the following field not supported by analytics: "+fieldName);
  }
  return aField;
}
 
Example 20
Source File: SimpleFacetsTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
static void doEmptyFacetCounts(String field, String[] prefixes) throws Exception {
  SchemaField sf = h.getCore().getLatestSchema().getField(field);

  String response = JQ(req("q", "*:*"));
  @SuppressWarnings({"rawtypes"})
  Map rsp = (Map) fromJSONString(response);
  Long numFound  = (Long)(((Map)rsp.get("response")).get("numFound"));

  ModifiableSolrParams params = params("q","*:*", "facet.mincount","1","rows","0", "facet","true", "facet.field","{!key=myalias}"+field);
  
  String[] methods = {null, "fc","enum","fcs", "uif"};
  if (sf.multiValued() || sf.getType().multiValuedFieldCache()) {
    methods = new String[]{null, "fc","enum", "uif"};
  }

  prefixes = prefixes==null ? new String[]{null} : prefixes;


  for (String method : methods) {
    if (method == null) {
      params.remove("facet.method");
    } else {
      params.set("facet.method", method);
    }
    for (String prefix : prefixes) {
      if (prefix == null) {
        params.remove("facet.prefix");
      } else {
        params.set("facet.prefix", prefix);
      }

      for (String missing : new String[] {null, "true"}) {
        if (missing == null) {
          params.remove("facet.missing");
        } else {
          params.set("facet.missing", missing);
        }
        
        String expected = missing==null ? "[]" : "[null," + numFound + "]";
        
        assertJQ(req(params),
            "/facet_counts/facet_fields/myalias==" + expected);
      }
    }
  }
}