Java Code Examples for org.apache.solr.schema.IndexSchema#getUniqueKeyField()

The following examples show how to use org.apache.solr.schema.IndexSchema#getUniqueKeyField() . 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: 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 2
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 3
Source File: ResponseLogComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void process(ResponseBuilder rb) throws IOException {
  SolrParams params = rb.req.getParams();
  if (!params.getBool(COMPONENT_NAME, false)) return;
  
  SolrIndexSearcher searcher = rb.req.getSearcher();
  IndexSchema schema = searcher.getSchema();
  if (schema.getUniqueKeyField() == null) return;

  ResultContext rc = (ResultContext) rb.rsp.getResponse();

  DocList docs = rc.getDocList();
  if (docs.hasScores()) {
    processScores(rb, docs, schema, searcher);
  } else {
    processIds(rb, docs, schema, searcher);
  }
}
 
Example 4
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 5
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public String getPrintableId() {
 if (req != null) {
   IndexSchema schema = req.getSchema();
   SchemaField sf = schema.getUniqueKeyField();
   if (solrDoc != null && sf != null) {
     SolrInputField field = solrDoc.getField(sf.getName());
     if (field != null) {
       return field.getFirstValue().toString();
     }
   }
 }
  return "(null)";
}
 
Example 6
Source File: DeleteUpdateCommand.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Returns the indexed ID for this delete.  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 && id != null) {
      BytesRefBuilder b = new BytesRefBuilder();
      sf.getType().readableToIndexed(id, b);
      indexedId = b.get();
    }
  }
  return indexedId;
}
 
Example 7
Source File: DeleteUpdateCommand.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public String getId() {
  if (id == null && indexedId != null) {
    IndexSchema schema = req.getSchema();
    SchemaField sf = schema.getUniqueKeyField();
    if (sf != null) {
      CharsRefBuilder ref = new CharsRefBuilder();
      sf.getType().indexedToReadable(indexedId, ref);
      id = ref.toString();
    }
  }
  return id;
}
 
Example 8
Source File: DocumentBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static String getID( SolrInputDocument doc, IndexSchema schema )
{
  String id = "";
  SchemaField sf = schema.getUniqueKeyField();
  if( sf != null ) {
    id = "[doc="+doc.getFieldValue( sf.getName() )+"] ";
  }
  return id;
}
 
Example 9
Source File: SolrLocator.java    From kite with Apache License 2.0 5 votes vote down vote up
private void validateSchema(IndexSchema schema) {
  if (schema.getUniqueKeyField() == null) {
    throw new MorphlineCompilationException("Solr schema.xml is missing unique key field", config);
  }
  if (!schema.getUniqueKeyField().isRequired()) {
    throw new MorphlineCompilationException("Solr schema.xml must contain a required unique key field", config);
  }
}
 
Example 10
Source File: CursorMark.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Generates an empty CursorMark bound for use with the 
 * specified schema and {@link SortSpec}.
 *
 * @param schema used for basic validation
 * @param sortSpec bound to this totem (un)marshalling serialized values
 */
public CursorMark(IndexSchema schema, SortSpec sortSpec) {

  final SchemaField uniqueKey = schema.getUniqueKeyField();
  if (null == uniqueKey) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
                            "Cursor functionality is not available unless the IndexSchema defines a uniqueKey field");
  }

  final Sort sort = sortSpec.getSort();
  if (null == sort) {
    // pure score, by definition we don't include the mandatyr uniqueKey tie breaker
    throw new SolrException(ErrorCode.BAD_REQUEST,
                            "Cursor functionality requires a sort containing a uniqueKey field tie breaker");
  }
  
  if (!sortSpec.getSchemaFields().contains(uniqueKey)) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
                            "Cursor functionality requires a sort containing a uniqueKey field tie breaker");
  }

  if (0 != sortSpec.getOffset()) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
                            "Cursor functionality requires start=0");
  }

  for (SortField sf : sort.getSort()) {
    if (sf.getType().equals(SortField.Type.DOC)) {
      throw new SolrException(ErrorCode.BAD_REQUEST,
                              "Cursor functionality can not be used with internal doc ordering sort: _docid_");
    }
  }

  if (sort.getSort().length != sortSpec.getSchemaFields().size()) {
      throw new SolrException(ErrorCode.SERVER_ERROR,
                              "Cursor SortSpec failure: sort length != SchemaFields: " 
                              + sort.getSort().length + " != " + 
                              sortSpec.getSchemaFields().size());
  }

  this.sortSpec = sortSpec;
  this.values = null;
}