Java Code Examples for org.apache.solr.schema.FieldType#getNumberType()

The following examples show how to use org.apache.solr.schema.FieldType#getNumberType() . 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: CommandHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException {
  @SuppressWarnings({"rawtypes"})
  Command firstCommand = commands.get(0);
  String field = firstCommand.getKey();
  SchemaField sf = searcher.getSchema().getField(field);
  FieldType fieldType = sf.getType();
  
  @SuppressWarnings({"rawtypes"})
  final AllGroupHeadsCollector allGroupHeadsCollector;
  if (fieldType.getNumberType() != null) {
    ValueSource vs = fieldType.getValueSource(sf, null);
    allGroupHeadsCollector = AllGroupHeadsCollector.newCollector(new ValueSourceGroupSelector(vs, new HashMap<>()),
        firstCommand.getWithinGroupSort());
  } else {
    allGroupHeadsCollector
        = AllGroupHeadsCollector.newCollector(new TermGroupSelector(firstCommand.getKey()), firstCommand.getWithinGroupSort());
  }
  if (collectors.isEmpty()) {
    searchWithTimeLimiter(query, filter, allGroupHeadsCollector);
  } else {
    collectors.add(allGroupHeadsCollector);
    searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()])));
  }

  return new BitDocSet(allGroupHeadsCollector.retrieveGroupHeads(searcher.maxDoc()));
}
 
Example 2
Source File: TopGroupsFieldCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public List<Collector> create() throws IOException {
  if (firstPhaseGroups.isEmpty()) {
    return Collections.emptyList();
  }

  final List<Collector> collectors = new ArrayList<>(1);
  final FieldType fieldType = field.getType();
  if (fieldType.getNumberType() != null) {
    ValueSource vs = fieldType.getValueSource(field, null);
    Collection<SearchGroup<MutableValue>> v = GroupConverter.toMutable(field, firstPhaseGroups);
    secondPassCollector = new TopGroupsCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()),
        v, groupSort, withinGroupSort, maxDocPerGroup, needMaxScore
    );
  } else {
    secondPassCollector = new TopGroupsCollector<>(new TermGroupSelector(field.getName()),
        firstPhaseGroups, groupSort, withinGroupSort, maxDocPerGroup, needMaxScore
    );
  }
  collectors.add(secondPassCollector);
  return collectors;
}
 
Example 3
Source File: TopGroupsFieldCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void postCollect(IndexSearcher searcher) throws IOException {
  if (firstPhaseGroups.isEmpty()) {
    topGroups = new TopGroups<>(groupSort.getSort(), withinGroupSort.getSort(), 0, 0, new GroupDocs[0], Float.NaN);
    return;
  }

  FieldType fieldType = field.getType();
  if (fieldType.getNumberType() != null) {
    topGroups = GroupConverter.fromMutable(field, secondPassCollector.getTopGroups(0));
  } else {
    topGroups = secondPassCollector.getTopGroups(0);
  }
  if (needScores) {
    for (GroupDocs<?> group : topGroups.groups) {
      TopFieldCollector.populateScores(group.scoreDocs, searcher, query);
    }
  }
}
 
Example 4
Source File: ExpandComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private String numericToString(FieldType fieldType, long val) {
  if (fieldType.getNumberType() != null) {
    switch (fieldType.getNumberType()) {
      case INTEGER:
      case LONG:
        return Long.toString(val);
      case FLOAT:
        return Float.toString(Float.intBitsToFloat((int)val));
      case DOUBLE:
        return Double.toString(Double.longBitsToDouble(val));
      case DATE:
        break;
    }
  }
  throw new IllegalArgumentException("FieldType must be INT,LONG,FLOAT,DOUBLE found " + fieldType);
}
 
Example 5
Source File: MultiFieldWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static LongFunction<Object> bitsToValue(FieldType fieldType) {
  switch (fieldType.getNumberType()) {
    case LONG:
      return (bits)-> bits;
    case DATE:
      return (bits)-> new Date(bits);
    case INTEGER:
      return (bits)-> (int)bits;
    case FLOAT:
      return (bits)-> NumericUtils.sortableIntToFloat((int)bits);
    case DOUBLE:
      return (bits)-> NumericUtils.sortableLongToDouble(bits);
    default:
      throw new AssertionError("Unsupported NumberType: " + fieldType.getNumberType());
  }
}
 
Example 6
Source File: MtasSolrComponentFacet.java    From mtas with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the field type.
 *
 * @param schema the schema
 * @param field the field
 * @return the field type
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String getFieldType(IndexSchema schema, String field)
    throws IOException {
  SchemaField sf = schema.getField(field);
  FieldType ft = sf.getType();
  if (ft != null) {
    if (ft.isPointField() && !sf.hasDocValues()) {
      return ComponentFacet.TYPE_POINTFIELD_WITHOUT_DOCVALUES;
    }
    NumberType nt = ft.getNumberType();
    if (nt != null) {
      return nt.name();
    } else {
      return ComponentFacet.TYPE_STRING;
    }
  } else {
    // best guess
    return ComponentFacet.TYPE_STRING;
  }
}
 
Example 7
Source File: FacetRangeGenerator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static FacetRangeGenerator<? extends Comparable<?>> create(RangeFacet rangeFacet){
  final SchemaField sf = rangeFacet.getField();
  final FieldType ft = sf.getType();
  final FacetRangeGenerator<?> calc;
  if (ft instanceof NumericFieldType) {
    switch (ft.getNumberType()) {
      case FLOAT:
        calc = new FloatFacetRangeGenerator(rangeFacet);
        break;
      case DOUBLE:
        calc = new DoubleFacetRangeGenerator(rangeFacet);
        break;
      case INTEGER:
        calc = new IntegerFacetRangeGenerator(rangeFacet);
        break;
      case LONG:
        calc = new LongFacetRangeGenerator(rangeFacet);
        break;
      case DATE:
        calc = new DateFacetRangeGenerator(rangeFacet, null);
        break;
      default:
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on numeric field of unexpected type: " + sf.getName());
    }
  } else {
    throw new SolrException (SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on non-numeric field: " + sf);
  }
  return calc;
}
 
Example 8
Source File: NumericFacets.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static String bitsToStringValue(FieldType fieldType, long bits) {
  switch (fieldType.getNumberType()) {
    case LONG:
    case INTEGER:
      return String.valueOf(bits);
    case FLOAT:
      return String.valueOf(NumericUtils.sortableIntToFloat((int)bits));
    case DOUBLE:
      return String.valueOf(NumericUtils.sortableLongToDouble(bits));
    case DATE:
      return new Date(bits).toInstant().toString();
    default:
      throw new AssertionError("Unsupported NumberType: " + fieldType.getNumberType());
  }
}
 
Example 9
Source File: SimpleFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * This method will force the appropriate facet method even if the user provided a different one as a request parameter
 *
 * N.B. this method could overwrite what you passed as request parameter. Be Extra careful
 *
 * @param field field we are faceting
 * @param method the facet method passed as a request parameter
 * @param mincount the minimum value a facet should have to be returned
 * @return the FacetMethod to use
 */
 static FacetMethod selectFacetMethod(SchemaField field, FacetMethod method, Integer mincount) {

   FieldType type = field.getType();
   if (type.isPointField()) {
     // Only FCS is supported for PointFields for now
     return FacetMethod.FCS;
   }

   /*The user did not specify any preference*/
   if (method == null) {
     /* Always use filters for booleans if not DocValues only... we know the number of values is very small. */
     if (type instanceof BoolField && (field.indexed() == true || field.hasDocValues() == false)) {
       method = FacetMethod.ENUM;
     } else if (type.getNumberType() != null && !field.multiValued()) {
      /* the per-segment approach is optimal for numeric field types since there
         are no global ords to merge and no need to create an expensive
         top-level reader */
       method = FacetMethod.FCS;
     } else {
       // TODO: default to per-segment or not?
       method = FacetMethod.FC;
     }
   }

   /* FC without docValues does not support single valued numeric facets */
   if (method == FacetMethod.FC
       && type.getNumberType() != null && !field.multiValued()) {
     method = FacetMethod.FCS;
   }

   /* UIF without DocValues can't deal with mincount=0, the reason is because
       we create the buckets based on the values present in the result set.
       So we are not going to see facet values which are not in the result set */
   if (method == FacetMethod.UIF
       && !field.hasDocValues() && mincount == 0) {
     method = field.multiValued() ? FacetMethod.FC : FacetMethod.FCS;
   }

   /* Unless isUninvertible() is true, we prohibit any use of UIF...
      Here we just force FC(S) instead, and trust that the DocValues faceting logic will
      do the right thing either way (with or w/o docvalues) */
   if (FacetMethod.UIF == method && ! field.isUninvertible()) {
     method = field.multiValued() ? FacetMethod.FC : FacetMethod.FCS;
   }
   
   /* ENUM can't deal with trie fields that index several terms per value */
   if (method == FacetMethod.ENUM
       && TrieField.getMainValuePrefix(type) != null) {
     method = field.multiValued() ? FacetMethod.FC : FacetMethod.FCS;
   }

   /* FCS can't deal with multi token fields */
   final boolean multiToken = field.multiValued() || type.multiValuedFieldCache();
   if (method == FacetMethod.FCS
       && multiToken) {
     method = FacetMethod.FC;
   }

   return method;
}
 
Example 10
Source File: GroupConverter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
static Collection<SearchGroup<MutableValue>> toMutable(SchemaField field, Collection<SearchGroup<BytesRef>> values) {
  FieldType fieldType = field.getType();
  List<SearchGroup<MutableValue>> result = new ArrayList<>(values.size());
  for (SearchGroup<BytesRef> original : values) {
    SearchGroup<MutableValue> converted = new SearchGroup<>();
    converted.sortValues = original.sortValues; // ?
    NumberType type = fieldType.getNumberType();
    final MutableValue v;
    switch (type) {
      case INTEGER:
        MutableValueInt mutableInt = new MutableValueInt();
        if (original.groupValue == null) {
          mutableInt.value = 0;
          mutableInt.exists = false;
        } else {
          mutableInt.value = (Integer) fieldType.toObject(field, original.groupValue);
        }
        v = mutableInt;
        break;
      case FLOAT:
        MutableValueFloat mutableFloat = new MutableValueFloat();
        if (original.groupValue == null) {
          mutableFloat.value = 0;
          mutableFloat.exists = false;
        } else {
          mutableFloat.value = (Float) fieldType.toObject(field, original.groupValue);
        }
        v = mutableFloat;
        break;
      case DOUBLE:
        MutableValueDouble mutableDouble = new MutableValueDouble();
        if (original.groupValue == null) {
          mutableDouble.value = 0;
          mutableDouble.exists = false;
        } else {
          mutableDouble.value = (Double) fieldType.toObject(field, original.groupValue);
        }
        v = mutableDouble;
        break;
      case LONG:
        MutableValueLong mutableLong = new MutableValueLong();
        if (original.groupValue == null) {
          mutableLong.value = 0;
          mutableLong.exists = false;
        } else {
          mutableLong.value = (Long) fieldType.toObject(field, original.groupValue);
        }
        v = mutableLong;
        break;
      case DATE:
        MutableValueDate mutableDate = new MutableValueDate();
        if (original.groupValue == null) {
          mutableDate.value = 0;
          mutableDate.exists = false;
        } else {
          mutableDate.value = ((Date)fieldType.toObject(field, original.groupValue)).getTime();
        }
        v = mutableDate;
        break;
      default:
        throw new AssertionError();
    }
    converted.groupValue = v;
    result.add(converted);
  }
  return result;
}
 
Example 11
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 12
Source File: RangeFacetRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the right instance of {@link org.apache.solr.handler.component.RangeFacetRequest.RangeEndpointCalculator}
 * depending on the field type of the schema field
 */
private RangeEndpointCalculator<? extends Comparable<?>> createCalculator() {
  RangeEndpointCalculator<?> calc;
  FieldType ft = schemaField.getType();

  if (ft instanceof TrieField) {
    switch (ft.getNumberType()) {
      case FLOAT:
        calc = new FloatRangeEndpointCalculator(this);
        break;
      case DOUBLE:
        calc = new DoubleRangeEndpointCalculator(this);
        break;
      case INTEGER:
        calc = new IntegerRangeEndpointCalculator(this);
        break;
      case LONG:
        calc = new LongRangeEndpointCalculator(this);
        break;
      case DATE:
        calc = new DateRangeEndpointCalculator(this, null);
        break;
      default:
        throw new SolrException
            (SolrException.ErrorCode.BAD_REQUEST,
                "Unable to range facet on Trie field of unexpected type:" + this.facetOn);
    }
  } else if (ft instanceof DateRangeField) {
    calc = new DateRangeEndpointCalculator(this, null);
  } else if (ft.isPointField()) {
    switch (ft.getNumberType()) {
      case FLOAT:
        calc = new FloatRangeEndpointCalculator(this);
        break;
      case DOUBLE:
        calc = new DoubleRangeEndpointCalculator(this);
        break;
      case INTEGER:
        calc = new IntegerRangeEndpointCalculator(this);
        break;
      case LONG:
        calc = new LongRangeEndpointCalculator(this);
        break;
      case DATE:
        calc = new DateRangeEndpointCalculator(this, null);
        break;
      default:
        throw new SolrException
            (SolrException.ErrorCode.BAD_REQUEST,
                "Unable to range facet on Point field of unexpected type:" + this.facetOn);
    }
  } else if (ft instanceof CurrencyFieldType) {
    calc = new CurrencyRangeEndpointCalculator(this);
  } else {
    throw new SolrException
        (SolrException.ErrorCode.BAD_REQUEST,
            "Unable to range facet on field:" + schemaField);
  }

  return calc;
}