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

The following examples show how to use org.apache.solr.schema.SchemaField#getName() . 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: UnifiedSolrHighlighter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the unique keys for the topdocs to key the results
 */
protected String[] getUniqueKeys(SolrIndexSearcher searcher, int[] docIDs) throws IOException {
  IndexSchema schema = searcher.getSchema();
  SchemaField keyField = schema.getUniqueKeyField();
  if (keyField != null) {
    SolrReturnFields returnFields = new SolrReturnFields(keyField.getName(), null);
    String[] uniqueKeys = new String[docIDs.length];
    for (int i = 0; i < docIDs.length; i++) {
      int docid = docIDs[i];
      SolrDocument solrDoc = searcher.getDocFetcher().solrDoc(docid, returnFields);
      uniqueKeys[i] = schema.printableUniqueKey(solrDoc);
    }
    return uniqueKeys;
  } else {
    return new String[docIDs.length];
  }
}
 
Example 2
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Returns the indexed ID for this document.  The returned BytesRef is retained across multiple calls, and should not be modified. */
public BytesRef getIndexedId() {
  if (indexedId == null) {
    IndexSchema schema = req.getSchema();
    SchemaField sf = schema.getUniqueKeyField();
    if (sf != null) {
      if (solrDoc != null) {
        SolrInputField field = solrDoc.getField(sf.getName());

        int count = field==null ? 0 : field.getValueCount();
        if (count == 0) {
          if (overwrite) {
            throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document is missing mandatory uniqueKey field: " + sf.getName());
          }
        } else if (count  > 1) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document contains multiple values for uniqueKey field: " + field);
        } else {
          BytesRefBuilder b = new BytesRefBuilder();
          sf.getType().readableToIndexed(field.getFirstValue().toString(), b);
          indexedId = b.get();
        }
      }
    }
  }
  return indexedId;
}
 
Example 3
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * @return String id to hash
 */
public String getHashableId() {
  IndexSchema schema = req.getSchema();
  SchemaField sf = schema.getUniqueKeyField();
  if (sf != null) {
    if (solrDoc != null) {
      SolrInputField field = solrDoc.getField(sf.getName());

      int count = field == null ? 0 : field.getValueCount();
      if (count == 0) {
        if (overwrite) {
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
              "Document is missing mandatory uniqueKey field: "
                  + sf.getName());
        }
      } else if (count > 1) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Document contains multiple values for uniqueKey field: " + field);
      } else {
        return field.getFirstValue().toString();
      }
    }
  }
  return null;
}
 
Example 4
Source File: UUIDUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public UpdateRequestProcessor getInstance(SolrQueryRequest req,
                                          SolrQueryResponse rsp,
                                          UpdateRequestProcessor next ) {
  String fieldName = this.fieldName;

  String fname = req.getParams().get(PREFIX_PARAM+FIELD_PARAM);
  if (!StringUtils.isEmpty(fname)) {
    fieldName = fname;
  }

  if (StringUtils.isEmpty(fieldName)) {
    SchemaField schemaField = req.getSchema().getUniqueKeyField();
    fieldName = schemaField.getName();
  }

  return new AbstractDefaultValueUpdateProcessorFactory.DefaultValueUpdateProcessor(fieldName, next) {
    @Override
    public Object getDefaultValue() {
      return UUID.randomUUID().toString().toLowerCase(Locale.ROOT);
    }
  };
}
 
Example 5
Source File: DefaultSolrHighlighter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Highlights and returns the highlight object for this field -- a String[] by default.  Null if none. */
@SuppressWarnings("unchecked")
protected Object doHighlightingByFastVectorHighlighter(SolrDocument doc, int docId,
                                                       SchemaField schemaField, FvhContainer fvhContainer,
                                                       IndexReader reader, SolrQueryRequest req) throws IOException {
  SolrParams params = req.getParams();
  String fieldName = schemaField.getName();
  SolrFragmentsBuilder solrFb = getSolrFragmentsBuilder(fieldName, params);

  String[] snippets = fvhContainer.fvh.getBestFragments(fvhContainer.fieldQuery, reader, docId, fieldName,
          params.getFieldInt(fieldName, HighlightParams.FRAGSIZE, 100),
          params.getFieldInt(fieldName, HighlightParams.SNIPPETS, 1),
          getFragListBuilder(fieldName, params),
          getFragmentsBuilder(fieldName, params),
          solrFb.getPreTags(params, fieldName),
          solrFb.getPostTags(params, fieldName),
          getEncoder(fieldName, params));
  if (snippets != null && snippets.length > 0)
    return snippets;
  return null;
}
 
Example 6
Source File: AlfrescoFieldType.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive)
{
    Analyzer multiAnalyzer = constructMultiTermAnalyzer(getQueryAnalyzer());
    BytesRef lower = analyzeMultiTerm(field.getName(), part1, multiAnalyzer);
    BytesRef upper = analyzeMultiTerm(field.getName(), part2, multiAnalyzer);
    return new TermRangeQuery(field.getName(), lower, upper, minInclusive, maxInclusive);
}
 
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: BinaryDocValuesField.java    From liresolr with GNU General Public License v2.0 5 votes vote down vote up
@Override
    public IndexableField createField(SchemaField field, Object val /*, float boost*/) {
        if (val == null) return null;
        if (!field.stored()) {
            return null;
        }
        byte[] buf = null;
        int offset = 0, len = 0;
        if (val instanceof byte[]) {
            buf = (byte[]) val;
            len = buf.length;
        } else if (val instanceof ByteBuffer && ((ByteBuffer)val).hasArray()) {
            ByteBuffer byteBuf = (ByteBuffer) val;
            buf = byteBuf.array();
            offset = byteBuf.position();
            len = byteBuf.limit() - byteBuf.position();
        } else {
            String strVal = val.toString();
            //the string has to be a base64 encoded string
            buf = Base64.base64ToByteArray(strVal);
            offset = 0;
            len = buf.length;
        }

        Field f = new org.apache.lucene.document.BinaryDocValuesField(field.getName(), new BytesRef(buf, offset, len));
//        Field f = new org.apache.lucene.document.StoredField(field.getName(), buf, offset, len);
        //f.setBoost(boost);
        return f;
    }
 
Example 9
Source File: RoutedAliasUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SolrCmdDistributor.Node routeDocToSlice(String collection, SolrInputDocument doc) {
  SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
  // schema might not have key field...
  String idFieldName = uniqueKeyField == null ? null : uniqueKeyField.getName();
  String idValue = uniqueKeyField == null ? null : doc.getFieldValue(idFieldName).toString();
  DocCollection coll = zkController.getClusterState().getCollection(collection);
  Slice slice = coll.getRouter().getTargetSlice(idValue, doc, null, req.getParams(), coll);
  return getLeaderNode(collection, slice);
}
 
Example 10
Source File: GraphEdgeCollector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Query getResultQuery(SchemaField matchField, boolean useAutomaton) {
  if (collectorTerms == null || collectorTerms.size() == 0) {
    // return null if there are no terms (edges) to traverse.
    return null;
  } else {
    // Create a query
    Query q = null;

    // TODO: see if we should dynamically select this based on the frontier size.
    if (useAutomaton) {
      // build an automaton based query for the frontier.
      Automaton autn = buildAutomaton(collectorTerms);
      AutomatonQuery autnQuery = new AutomatonQuery(new Term(matchField.getName()), autn);
      q = autnQuery;
    } else {
      List<BytesRef> termList = new ArrayList<>(collectorTerms.size());
      for (int i = 0; i < collectorTerms.size(); i++) {
        BytesRef ref = new BytesRef();
        collectorTerms.get(i, ref);
        termList.add(ref);
      }
      q = (matchField.hasDocValues() && !matchField.indexed())
              ? new DocValuesTermsQuery(matchField.getName(), termList)
              : new TermInSetQuery(matchField.getName(), termList);
    }

    return q;
  }
}
 
Example 11
Source File: FieldFacetStats.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public FieldFacetStats(SolrIndexSearcher searcher, SchemaField facet_sf, StatsField statsField) {
  this.statsField = statsField;
  this.facet_sf = facet_sf;
  this.name = facet_sf.getName();

  topLevelReader = searcher.getSlowAtomicReader();
  valueSource = facet_sf.getType().getValueSource(facet_sf, null);

  facetStatsValues = new HashMap<>();
  facetStatsTerms = new ArrayList<>();
  missingStats = new HashMap<>();
}
 
Example 12
Source File: ForgivingInteger.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(SchemaField field, boolean reverse) {
    field.checkSortability();
    return new SortField(field.getName(), SortField.Type.INT, reverse);
}
 
Example 13
Source File: AlfrescoCollatableTextFieldType.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public SortField getSortField(SchemaField field, boolean reverse)
{
    return new SortField(field.getName(), new TextSortFieldComparatorSource(), reverse);
}
 
Example 14
Source File: MergeException.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
public MergeException(SchemaField field, String message) {
  super(message + ": " + field.getName());
  this.field = field;
}
 
Example 15
Source File: ElevatedMarkerFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) {
  SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
  String idfield = uniqueKeyField.getName();
  return new MarkTransformer(field,idfield, uniqueKeyField.getType());
}
 
Example 16
Source File: ExcludedMarkerFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public DocTransformer create(String field, SolrParams params, SolrQueryRequest req) {
  SchemaField uniqueKeyField = req.getSchema().getUniqueKeyField();
  String idfield = uniqueKeyField.getName();
  return new ExcludedTransformer(field,idfield, uniqueKeyField.getType());
}
 
Example 17
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 18
Source File: AlfrescoCollatableMLTextFieldType.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public SortField getSortField(SchemaField field, boolean reverse)
{
    return new SortField(field.getName(), new MLTextSortFieldComparatorSource(), reverse);
}