Example usage for org.apache.solr.request SolrQueryRequest updateSchemaToLatest

List of usage examples for org.apache.solr.request SolrQueryRequest updateSchemaToLatest

Introduction

In this page you can find the example usage for org.apache.solr.request SolrQueryRequest updateSchemaToLatest.

Prototype

public void updateSchemaToLatest();

Source Link

Document

Replaces the current schema snapshot with the latest from the core.

Usage

From source file:com.sindicetech.siren.solr.handler.JsonLoader.java

License:Open Source License

@Override
public void load(final SolrQueryRequest req, final SolrQueryResponse rsp, final ContentStream stream,
        final UpdateRequestProcessor processor) throws Exception {
    Reader reader = null;/*from w w  w  .  j  av  a2s .  c o  m*/
    try {
        reader = stream.getReader();
        // keep a copy of the body for the source entry
        // TODO: instead of reading the stream to make a copy, try to create a copy of the json
        // while parsing it in the JsonReader
        String body = IOUtils.toString(reader);

        FieldMappersHandler mappersHandler = new FieldMappersHandler(fieldMappers, req.getCore());
        DocumentBuilder docBuilder = new DocumentBuilder();

        // Add the source field entry
        FieldEntry source = new FieldEntry(SOURCE_FIELDNAME, body);
        docBuilder.add(mappersHandler.map(source));

        // Add the id field initialised with a UUID. It will be overwritten if an id field exist in the JSON document.
        FieldEntry id = new FieldEntry(IdFieldMapper.INPUT_FIELD,
                UUID.randomUUID().toString().toLowerCase(Locale.ROOT));
        docBuilder.add(mappersHandler.map(id));

        JsonParser parser = mapper.getJsonFactory().createJsonParser(new StringReader(body));
        JsonReader jreader = new JsonReader(parser);

        FieldEntry entry;
        while ((entry = jreader.next()) != null) {
            docBuilder.add(mappersHandler.map(entry));
        }

        // the index schema might have changed
        req.updateSchemaToLatest();

        // check that we have seen all the required field mappers
        Set<String> missingRequiredMappers = mappersHandler.getMissingRequiredMappers();
        if (!missingRequiredMappers.isEmpty()) {
            throw new SolrException(BAD_REQUEST,
                    "Document is missing the following required fields: " + missingRequiredMappers);
        }

        // Create and process the Add command
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docBuilder.getSolrInputDocument();
        processor.processAdd(cmd);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}