Example usage for org.apache.solr.common SolrInputDocument remove

List of usage examples for org.apache.solr.common SolrInputDocument remove

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument remove.

Prototype

@Override
    public SolrInputField remove(Object key) 

Source Link

Usage

From source file:com.francelabs.datafari.updateprocessor.DatafariUpdateProcessor.java

License:Apache License

@Override
public void processAdd(final AddUpdateCommand cmd) throws IOException {
    final SolrInputDocument doc = cmd.getSolrInputDocument();

    // Sometimes Tika put several ids so we keep the first one which is
    // always the right one
    if (doc.getFieldValues("id").size() > 1) {
        final Object id = doc.getFieldValue("id");
        doc.remove("id");
        doc.addField("id", id);
    }//from w ww. j a v a  2s  . com

    // Try to retrieve at the ignored_filelastmodified field to set it's
    // value in the last_modified field
    if (doc.getFieldValue("ignored_filelastmodified") != null) {
        final Object last_modified = doc.getFieldValue("ignored_filelastmodified");
        doc.remove("last_modified");
        doc.addField("last_modified", last_modified);
    }

    // Sometimes Tika put several last_modified dates, so we keep the first
    // one which is always the right one
    if ((doc.getFieldValues("last_modified") != null) && (doc.getFieldValues("last_modified").size() > 1)) {
        final Object last_modified = doc.getFieldValue("last_modified");
        doc.remove("last_modified");
        doc.addField("last_modified", last_modified);
    }

    final String url = (String) doc.getFieldValue("id");

    // Create path hierarchy for facet
    final List<String> urlHierarchy = new ArrayList<>();

    /*
     * // Create path hierarchy for facet
     *
     * final List<String> urlHierarchy = new ArrayList<String>();
     *
     * final String path = url.replace("file:", ""); int previousIndex = 1; int
     * depth = 0; // Tokenize the path and add the depth as first character for
     * each token // (like: 0/home, 1/home/project ...) for (int i = 0; i <
     * path.split("/").length - 2; i++) { int endIndex = path.indexOf('/',
     * previousIndex); if (endIndex == -1) { endIndex = path.length() - 1; }
     * urlHierarchy.add(depth + path.substring(0, endIndex)); depth++;
     * previousIndex = endIndex + 1; }
     *
     * // Add the tokens to the urlHierarchy field doc.addField("urlHierarchy",
     * urlHierarchy);
     */

    doc.addField("url", url);

    String filename = "";
    final SolrInputField streamNameField = doc.get("ignored_stream_name");
    if (streamNameField != null) {
        filename = (String) streamNameField.getFirstValue();
    } else {
        final Pattern pattern = Pattern.compile("[^/]*$");
        final Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            filename = matcher.group();
        }
    }

    if (url.startsWith("http")) {
        if (doc.get("title") == null) {
            doc.addField("title", filename);
        }
        doc.addField("source", "web");
    }

    if (url.startsWith("file")) {
        doc.removeField("title");
        doc.addField("title", filename);
        doc.addField("source", "file");
    }

    String extension = "";
    URL urlObject = new URL(url);
    String path = urlObject.getPath();
    final SolrInputField mimeTypeField = doc.get("ignored_content_type");

    String nameExtension = FilenameUtils.getExtension(path);
    String tikaExtension = mimeTypeField == null ? "" : extensionFromMimeTypeField(mimeTypeField);

    if (extensionFromName) {
        extension = nameExtension.length() > 1 && nameExtension.length() < 5 ? nameExtension : tikaExtension;
    } else {
        extension = tikaExtension.length() > 1 && tikaExtension.length() < 5 ? tikaExtension : nameExtension;
    }
    /*
    if (extensionFromName || mimeTypeField == null) {
       if (path.contains(".")){
         extension = FilenameUtils.getExtension(path);
          if (extension.length() > 4 || extension.length() < 1) {
    // If length is too long, try extracting from tika information if available
    String tryExtension = mimeTypeField==null ? null : extensionFromMimeTypeField(mimeTypeField);
    if (tryExtension != null) {
      extension = tryExtension;
    } else {
      // Else default to bin for anything else
      extension = "bin";
    }
          }
       }
       else if (urlObject.getProtocol().equals("http") || urlObject.getProtocol().equals("https")) {
         extension = null;
         if (mimeTypeField != null) {
           extension = extensionFromMimeTypeField(mimeTypeField);
         } 
         if (extension == null) {
           extension = "html";
         }
       }
    } else {
      extension = extensionFromMimeTypeField(mimeTypeField);
      if (extension == null) {
        extension = FilenameUtils.getExtension(path);
      }
    }
    */
    doc.addField("extension", extension.toLowerCase());

    super.processAdd(cmd);
}

From source file:ec.edu.ucuenca.dcc.sld.SolrConnection.java

public void update(String uri, String[] var, String[] val) throws SolrServerException, IOException {

    assert var.length == val.length;
    NamedList params = new NamedList();
    String qry = "uri:\"" + uri + "\"";
    params.add("q", qry);
    params.add("fl", "*");
    SolrParams toSolrParams = SolrParams.toSolrParams(params);
    QueryResponse query = Solr.query(toSolrParams, SolrRequest.METHOD.POST);
    SolrDocumentList results = query.getResults();
    assert results.getNumFound() != 0;
    SolrDocument get = results.get(0);//from  w w w.  ja  v a 2  s . c o m

    SolrInputDocument document = new SolrInputDocument();
    Set<Map.Entry<String, Object>> entrySet = get.entrySet();
    for (Map.Entry<String, Object> en : entrySet) {
        document.setField(en.getKey(), en.getValue());
    }
    for (int i = 0; i < var.length; i++) {
        document.remove(var[i]);
        document.setField(var[i], val[i]);
    }

    Solr.deleteByQuery(qry);
    Solr.commit();
    UpdateResponse add = Solr.add(document);
    Solr.commit();

}

From source file:org.dspace.util.SolrUpgradePre6xStatistics.java

License:BSD License

private int updateRecords(String query) throws SolrServerException, SQLException, IOException {
    int initNumProcessed = numProcessed;
    SolrQuery sQ = new SolrQuery();
    sQ.setQuery(query);/*from  w w  w.  j a  va2s .c om*/
    sQ.setRows(batchSize);

    // Ensure that items are grouped by id
    // Sort by id fails due to presense of id and string fields. The ord function
    // seems to help
    sQ.addSort("type", SolrQuery.ORDER.desc);
    sQ.addSort("scopeType", SolrQuery.ORDER.desc);
    sQ.addSort("ord(owningItem)", SolrQuery.ORDER.desc);
    sQ.addSort("id", SolrQuery.ORDER.asc);
    sQ.addSort("scopeId", SolrQuery.ORDER.asc);

    QueryResponse sr = server.query(sQ);
    SolrDocumentList sdl = sr.getResults();

    for (int i = 0; i < sdl.size() && (numProcessed < numRec); i++) {
        SolrDocument sd = sdl.get(i);
        SolrInputDocument input = ClientUtils.toSolrInputDocument(sd);
        input.remove("_version_");
        for (FIELD col : FIELD.values()) {
            mapField(input, col);
        }

        docs.add(input);
        ++numProcessed;
    }
    return numProcessed - initNumProcessed;
}

From source file:org.roda.core.index.utils.SolrUtils.java

private static SolrInputDocument validateDescriptiveMetadataFields(SolrInputDocument doc) {
    if (doc.get(RodaConstants.AIP_DATE_INITIAL) != null) {
        Object value = doc.get(RodaConstants.AIP_DATE_INITIAL).getValue();
        if (value instanceof String) {
            try {
                Date d = DateUtil.parseDate((String) value);
                doc.setField(RodaConstants.AIP_DATE_INITIAL, d);
            } catch (ParseException pe) {
                doc.remove(RodaConstants.AIP_DATE_INITIAL);
                doc.setField(RodaConstants.AIP_DATE_INITIAL + "_txt", value);
            }/*from   ww  w .  ja v  a 2 s .c o  m*/
        }
    }

    if (doc.get(RodaConstants.AIP_DATE_FINAL) != null) {
        Object value = doc.get(RodaConstants.AIP_DATE_FINAL).getValue();
        if (value instanceof String) {
            try {
                Date d = DateUtil.parseDate((String) value);
                doc.setField(RodaConstants.AIP_DATE_FINAL, d);
            } catch (ParseException pe) {
                doc.remove(RodaConstants.AIP_DATE_FINAL);
                doc.setField(RodaConstants.AIP_DATE_FINAL + "_txt", value);
            }
        }
    }

    return doc;
}