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

The following examples show how to use org.apache.solr.schema.IndexSchema#getFieldOrNull() . 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: AlfrescoLukeRequestHandler.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * This is a destructive call... the queue is empty at the end
 */
public NamedList<Integer> toNamedList(IndexSchema schema) {
	// reverse the list..
	List<TermInfo> aslist = new LinkedList<>();
	while (size() > 0) {
		aslist.add(0, (TermInfo) pop());
	}

	NamedList<Integer> list = new NamedList<>();
	for (TermInfo i : aslist) {
		String txt = i.term.text();
		SchemaField ft = schema.getFieldOrNull(i.term.field());
		if (ft != null) {
			txt = ft.getType().indexedToReadable(txt);
		}
		list.add(txt, i.docFreq);
	}
	return list;
}
 
Example 2
Source File: AlfrescoSearchHandler.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
public final SolrDocument toSolrDocument(Document doc, IndexSchema schema) {
	SolrDocument out = new SolrDocument();
	for (IndexableField f : doc) {
		// Make sure multivalued fields are represented as lists
		Object existing = out.get(f.name());
		if (existing == null) {
			SchemaField sf = schema.getFieldOrNull(f.name());
			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 3
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 4
Source File: TopLevelJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private SortedSetDocValues validateAndFetchDocValues(SolrIndexSearcher solrSearcher, String fieldName, String querySide) throws IOException {
  final IndexSchema schema = solrSearcher.getSchema();
  final SchemaField field = schema.getFieldOrNull(fieldName);
  if (field == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, querySide + " field '" + fieldName + "' does not exist");
  }

  if (!field.hasDocValues()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "'top-level' join queries require both 'from' and 'to' fields to have docValues, but " + querySide +
            " field [" + fieldName +  "] does not.");
  }

  final LeafReader leafReader = solrSearcher.getSlowAtomicReader();
  if (field.multiValued()) {
    return DocValues.getSortedSet(leafReader, fieldName);
  }
  return DocValues.singleton(DocValues.getSorted(leafReader, fieldName));
}
 
Example 5
Source File: LukeRequestHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * This is a destructive call... the queue is empty at the end
 */
public NamedList<Integer> toNamedList( IndexSchema schema )
{
  // reverse the list..
  List<TermInfo> aslist = new LinkedList<>();
  while( size() > 0 ) {
    aslist.add( 0, (TermInfo)pop() );
  }

  NamedList<Integer> list = new NamedList<>();
  for (TermInfo i : aslist) {
    String txt = i.term.text();
    SchemaField ft = schema.getFieldOrNull( i.term.field() );
    if( ft != null ) {
      txt = ft.getType().indexedToReadable( txt );
    }
    list.add( txt, i.docFreq );
  }
  return list;
}
 
Example 6
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static SolrInputDocument toSolrInputDocument(Document doc, IndexSchema schema) {
  SolrInputDocument out = new SolrInputDocument();
  for( IndexableField f : doc.getFields() ) {
    String fname = f.name();
    SchemaField sf = schema.getFieldOrNull(f.name());
    Object val = null;
    if (sf != null) {
      if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue;
      val = sf.getType().toObject(f);   // object or external string?
    } else {
      val = f.stringValue();
      if (val == null) val = f.numericValue();
      if (val == null) val = f.binaryValue();
      if (val == null) val = f;
    }

    // todo: how to handle targets of copy fields (including polyfield sub-fields)?
    out.addField(fname, val);
  }
  return out;
}
 
Example 7
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 8
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 9
Source File: TopGroupsResultTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected ScoreDoc[] transformToNativeShardDoc(List<NamedList<Object>> documents, Sort groupSort, String shard,
                                               IndexSchema schema) {
  ScoreDoc[] scoreDocs = new ScoreDoc[documents.size()];
  int j = 0;
  for (NamedList<Object> document : documents) {
    Object docId = document.get(ID);
    if (docId != null) {
      docId = docId.toString();
    } else {
      log.error("doc {} has null 'id'", document);
    }
    Float score = (Float) document.get("score");
    if (score == null) {
      score = Float.NaN;
    }
    Object[] sortValues = null;
    Object sortValuesVal = document.get("sortValues");
    if (sortValuesVal != null) {
      sortValues = ((List) sortValuesVal).toArray();
      for (int k = 0; k < sortValues.length; k++) {
        SchemaField field = groupSort.getSort()[k].getField() != null
            ? schema.getFieldOrNull(groupSort.getSort()[k].getField()) : null;
        sortValues[k] = ShardResultTransformerUtils.unmarshalSortValue(sortValues[k], field);
      }
    } else {
      log.debug("doc {} has null 'sortValues'", document);
    }
    scoreDocs[j++] = new ShardDoc(score, sortValues, docId, shard);
  }
  return scoreDocs;
}
 
Example 10
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static SolrInputDocument toSolrInputDocument(SolrDocument doc, IndexSchema schema) {
  SolrInputDocument out = new SolrInputDocument();
  for( String fname : doc.getFieldNames() ) {
    boolean fieldArrayListCreated = false;
    SchemaField sf = schema.getFieldOrNull(fname);
    if (sf != null) {
      if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue;
    }
    for (Object val: doc.getFieldValues(fname)) {
      if (val instanceof Field) {
        Field f = (Field) val;
        if (sf != null) {
          val = sf.getType().toObject(f);   // object or external string?
        } else {
          val = f.stringValue();
          if (val == null) val = f.numericValue();
          if (val == null) val = f.binaryValue();
          if (val == null) val = f;
        }
      } else if(val instanceof SolrDocument) {
        val = toSolrInputDocument((SolrDocument) val, schema);
        if(!fieldArrayListCreated && doc.getFieldValue(fname) instanceof Collection) {
          // previous value was array so we must return as an array even if was a single value array
          out.setField(fname, Lists.newArrayList(val));
          fieldArrayListCreated = true;
          continue;
        }
      }
      out.addField(fname, val);
    }
  }
  return out;
}
 
Example 11
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 5 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<>();
        if (f.fieldType().docValuesType() == DocValuesType.SORTED_NUMERIC) {
          // SORTED_NUMERICS store sortable bits version of the value, need to retrieve the original
          vals.add(sf.getType().toObject(f)); // (will materialize by side-effect)
        } else {
          vals.add( materialize(f) );
        }
      out.setField(f.name(), vals);
    } else {
        out.setField( f.name(), materialize(f) );
      }
    }
    else {
      out.addField( f.name(), materialize(f) );
    }
  }
  return out;
}
 
Example 12
Source File: AlfrescoLukeRequestHandler.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static SimpleOrderedMap<Object> getIndexedFieldsInfo(
		SolrQueryRequest req) throws Exception {

	SolrIndexSearcher searcher = req.getSearcher();
	SolrParams params = req.getParams();

	Set<String> fields = null;
	String fl = params.get(CommonParams.FL);
	if (fl != null) {
		fields = new TreeSet<>(Arrays.asList(fl.split("[,\\s]+")));
	}

	LeafReader reader = searcher.getSlowAtomicReader();
	IndexSchema schema = searcher.getSchema();

	// Don't be tempted to put this in the loop below, the whole point here
	// is to alphabetize the fields!
	Set<String> fieldNames = new TreeSet<>();
	for (FieldInfo fieldInfo : reader.getFieldInfos()) {
		fieldNames.add(fieldInfo.name);
	}

	// Walk the term enum and keep a priority queue for each map in our set
	SimpleOrderedMap<Object> vInfo = new SimpleOrderedMap<>();
	SimpleOrderedMap<Object> aInfo = new SimpleOrderedMap<>();

	for (String fieldName : fieldNames) {
		if (fields != null && !fields.contains(fieldName)
				&& !fields.contains("*")) {
			continue; // we're not interested in this field Still an issue
						// here
		}

		SimpleOrderedMap<Object> fieldMap = new SimpleOrderedMap<>();

		SchemaField sfield = schema.getFieldOrNull(fieldName);
		FieldType ftype = (sfield == null) ? null : sfield.getType();

		fieldMap.add("type", (ftype == null) ? null : ftype.getTypeName());
		fieldMap.add("schema", getFieldFlags(sfield));
		if (sfield != null && schema.isDynamicField(sfield.getName())
				&& schema.getDynamicPattern(sfield.getName()) != null) {
			fieldMap.add("dynamicBase",
					schema.getDynamicPattern(sfield.getName()));
		}
		Terms terms = reader.fields().terms(fieldName);
		if (terms == null) { // Not indexed, so we need to report what we
								// can (it made it through the fl param if
								// specified)
			vInfo.add(AlfrescoSolrDataModel.getInstance()
					.getAlfrescoPropertyFromSchemaField(fieldName),
					fieldMap);
			aInfo.add(fieldName, fieldMap);
			continue;
		}

		if (sfield != null && sfield.indexed()) {
			if (params.getBool(INCLUDE_INDEX_FIELD_FLAGS, true)) {
				Document doc = getFirstLiveDoc(terms, reader);

				if (doc != null) {
					// Found a document with this field
					try {
						IndexableField fld = doc.getField(fieldName);
						if (fld != null) {
							fieldMap.add("index", getFieldFlags(fld));
						} else {
							// it is a non-stored field...
							fieldMap.add("index", "(unstored field)");
						}
					} catch (Exception ex) {
						log.warn("error reading field: " + fieldName);
					}
				}
			}
			fieldMap.add("docs", terms.getDocCount());

		}
		if (fields != null
				&& (fields.contains(fieldName) || fields.contains("*"))) {
			getDetailedFieldInfo(req, fieldName, fieldMap);
		}
		// Add the field
		vInfo.add(fieldName, fieldMap);
		aInfo.add(AlfrescoSolrDataModel.getInstance()
				.getAlfrescoPropertyFromSchemaField(fieldName), fieldMap);
	}

	SimpleOrderedMap<Object> finfo = new SimpleOrderedMap<>();
	finfo.addAll(vInfo);
	// finfo.add("mimetype()", finfo.get("cm:content.mimetype"));
	// finfo.add("contentSize()", finfo.get("cm:content.size"));
	finfo.addAll(aInfo);
	return finfo;
}
 
Example 13
Source File: LukeRequestHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static SimpleOrderedMap<Object> getIndexedFieldsInfo(SolrQueryRequest req)
    throws Exception {

  SolrIndexSearcher searcher = req.getSearcher();
  SolrParams params = req.getParams();

  Set<String> fields = null;
  String fl = params.get(CommonParams.FL);
  if (fl != null) {
    fields = new TreeSet<>(Arrays.asList(fl.split( "[,\\s]+" )));
  }

  LeafReader reader = searcher.getSlowAtomicReader();
  IndexSchema schema = searcher.getSchema();

  // Don't be tempted to put this in the loop below, the whole point here is to alphabetize the fields!
  Set<String> fieldNames = new TreeSet<>();
  for(FieldInfo fieldInfo : reader.getFieldInfos()) {
    fieldNames.add(fieldInfo.name);
  }

  // Walk the term enum and keep a priority queue for each map in our set
  SimpleOrderedMap<Object> finfo = new SimpleOrderedMap<>();

  for (String fieldName : fieldNames) {
    if (fields != null && ! fields.contains(fieldName) && ! fields.contains("*")) {
      continue; //we're not interested in this field Still an issue here
    }

    SimpleOrderedMap<Object> fieldMap = new SimpleOrderedMap<>();

    SchemaField sfield = schema.getFieldOrNull( fieldName );
    FieldType ftype = (sfield==null)?null:sfield.getType();

    fieldMap.add( "type", (ftype==null)?null:ftype.getTypeName() );
    fieldMap.add("schema", getFieldFlags(sfield));
    if (sfield != null && schema.isDynamicField(sfield.getName()) && schema.getDynamicPattern(sfield.getName()) != null) {
      fieldMap.add("dynamicBase", schema.getDynamicPattern(sfield.getName()));
    }
    Terms terms = reader.terms(fieldName);
    if (terms == null) { // Not indexed, so we need to report what we can (it made it through the fl param if specified)
      finfo.add( fieldName, fieldMap );
      continue;
    }

    if(sfield != null && sfield.indexed() ) {
      if (params.getBool(INCLUDE_INDEX_FIELD_FLAGS,true)) {
        Document doc = getFirstLiveDoc(terms, reader);

        if (doc != null) {
          // Found a document with this field
          try {
            IndexableField fld = doc.getField(fieldName);
            if (fld != null) {
              fieldMap.add("index", getFieldFlags(fld));
            } else {
              // it is a non-stored field...
              fieldMap.add("index", "(unstored field)");
            }
          } catch (Exception ex) {
            log.warn("error reading field: {}", fieldName);
          }
        }
      }
      fieldMap.add("docs", terms.getDocCount());
    }
    if (fields != null && (fields.contains(fieldName) || fields.contains("*"))) {
      getDetailedFieldInfo(req, fieldName, fieldMap);
    }
    // Add the field
    finfo.add( fieldName, fieldMap );
  }
  return finfo;
}
 
Example 14
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a SolrInputDocument to SolrDocument, using an IndexSchema instance.
 *
 * @param sdoc The input document.
 * @param schema The index schema.
 * @param forInPlaceUpdate Whether the document is being used for an in place update,
 *                         see {@link DocumentBuilder#toDocument(SolrInputDocument, IndexSchema, boolean, boolean)}
 */
public static SolrDocument toSolrDoc(SolrInputDocument sdoc, IndexSchema schema, boolean forInPlaceUpdate) {
  // TODO what about child / nested docs?
  // TODO: do something more performant than this double conversion
  Document doc = DocumentBuilder.toDocument(sdoc, schema, forInPlaceUpdate, true);

  // copy the stored fields only
  Document out = new Document();
  for (IndexableField f : doc.getFields()) {
    if (f.fieldType().stored()) {
      out.add(f);
    } else if (f.fieldType().docValuesType() != DocValuesType.NONE) {
      SchemaField schemaField = schema.getFieldOrNull(f.name());
      if (schemaField != null && !schemaField.stored() && schemaField.useDocValuesAsStored()) {
        out.add(f);
      }
    } else {
      log.debug("Don't know how to handle field {}", f);
    }
  }

  SolrDocument solrDoc = toSolrDoc(out, schema);

  // add child docs
  for(SolrInputField solrInputField: sdoc) {
    if(solrInputField.getFirstValue() instanceof SolrInputDocument) {
      // is child doc
      Object val = solrInputField.getValue();
      Iterator<SolrDocument> childDocs = solrInputField.getValues().stream()
          .map(x -> toSolrDoc((SolrInputDocument) x, schema)).iterator();
      if(val instanceof Collection) {
        // add as collection even if single element collection
        solrDoc.setField(solrInputField.getName(), Lists.newArrayList(childDocs));
      } else {
        // single child doc
        solrDoc.setField(solrInputField.getName(), childDocs.next());
      }
    }
  }

  return solrDoc;
}