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

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

Introduction

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

Prototype

public void addField(String name, Object value) 

Source Link

Document

Add a field value to any existing values that may or may not exist.

Usage

From source file:WikipediaModel.java

License:Open Source License

public SolrInputDocument getDocument() {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", this.id);
    doc.addField("title", this.title);
    doc.addField("title_annotation", this.titleAnnotation);
    doc.addField("text", this.text);
    doc.addField("text_count", this.text.length());
    doc.addField("last_modified", this.lastModified);
    return doc;/*from  w w  w.  j av a2  s. c o m*/
}

From source file:at.newmedialab.lmf.search.services.indexing.SolrIndexingServiceImpl.java

License:Apache License

@Override
public void indexResource(Resource resource, SolrCoreRuntime runtime) {
    Program<Value> program = runtime.getConfiguration().getProgram();
    if (program == null) {
        try {/*from w w w  . jav  a 2 s. c om*/
            program = solrProgramService
                    .parseProgram(new StringReader(runtime.getConfiguration().getProgramString()));
            runtime.getConfiguration().setProgram(program);
        } catch (LDPathParseException e) {
            log.error("error parsing path program for engine {}", runtime.getConfiguration().getName(), e);
            return;
        }
    }

    if (resource == null)
        return;
    final String coreName = runtime.getConfiguration().getName();
    final String rID = getResourceId(resource);

    try {
        final RepositoryConnection connection = sesameService.getConnection();
        try {
            connection.begin();

            //if (resource instanceof KiWiResource && ((KiWiResource) resource).isDeleted()) {
            //    runtime.queueDeletion(rID);
            //}
            //FIXME: find a proper way to do this with the new api
            boolean deleted = true;
            RepositoryResult<Statement> statements = connection.getStatements(resource, null, null, false);
            while (statements.hasNext()) {
                if (!ResourceUtils.isDeleted(connection, statements.next())) {
                    deleted = false;
                    break;
                }
            }
            if (deleted) {
                runtime.queueDeletion(rID);
            }

            final Resource[] contexts;
            if (program.getGraphs().isEmpty()) {
                contexts = new Resource[0];
            } else {
                contexts = Collections2.transform(program.getGraphs(), new Function<java.net.URI, URI>() {
                    @Override
                    public URI apply(java.net.URI in) {
                        return connection.getValueFactory().createURI(in.toString());
                    }
                }).toArray(new Resource[0]);
            }

            final SesameConnectionBackend backend = ContextAwareSesameConnectionBackend
                    .withConnection(connection, contexts);
            if (program.getFilter() != null
                    && !program.getFilter().apply(backend, resource, Collections.singleton((Value) resource))) {
                if (log.isDebugEnabled()) {
                    log.debug("({}) <{}> does not match filter '{}', ignoring", coreName, resource,
                            program.getFilter().getPathExpression(backend));
                }
                // Some resources might be still in the index, so delete it.
                runtime.queueDeletion(rID);
                connection.commit();
                return;
            } else if (log.isTraceEnabled() && program.getFilter() != null) {
                log.trace("({}) <{}> matches filter '{}', indexing...", coreName, resource,
                        program.getFilter().getPathExpression(backend));
            }

            SolrInputDocument doc = new SolrInputDocument();

            doc.addField("id", rID);
            doc.addField("lmf.indexed", new Date());
            if (resource instanceof KiWiUriResource) {
                doc.addField("lmf.created", ((KiWiUriResource) resource).getCreated());
            }

            if (resource instanceof URI) {
                URI r = (URI) resource;

                doc.addField("lmf.uri", r.stringValue());
            } else if (resource instanceof BNode) {
                BNode r = (BNode) resource;
                doc.addField("lmf.anon_id", r.getID());
            } else {
                // This should not happen, but never the less...
                log.warn("Tried to index a Resource that is neither a URI nor BNode: {}", resource);
                runtime.queueDeletion(rID);
                connection.rollback();
                return;
            }

            for (Resource type : getTypes(connection, resource)) {
                if (type instanceof KiWiUriResource) {
                    doc.addField("lmf.type", type.stringValue());
                }
            }

            // Set the document boost
            if (program.getBooster() != null) {
                final Collection<Float> boostValues = program.getBooster().getValues(backend, resource);
                if (boostValues.size() > 0) {
                    final Float docBoost = boostValues.iterator().next();
                    if (boostValues.size() > 1) {
                        log.warn("found more than one boostFactor for <{}>, using {}", resource, docBoost);
                    }
                    doc.setDocumentBoost(docBoost);
                }
            }

            // set shortcut fields
            Set<Value> dependencies = new HashSet<Value>();
            for (FieldMapping<?, Value> rule : program.getFields()) {
                //                    Map<Value, List<Value>> paths = new HashMap<Value, List<Value>>();
                //                    Collection<?> values = rule.getValues(backend, resource, paths);
                //FIXME: Temporary fixing due LDPath reverse properties selector bug
                Map<Value, List<Value>> paths = null;
                Collection<?> values = null;
                if (runtime.getConfiguration().isUpdateDependencies()) {
                    paths = new HashMap<Value, List<Value>>();
                    values = rule.getValues(backend, resource, paths);
                } else {
                    values = rule.getValues(backend, resource);
                }
                //
                try {
                    final boolean isSinge = !isMultiValuedField(rule);
                    for (Object value : values) {
                        if (value != null) {
                            doc.addField(rule.getFieldName(), value);
                            if (isSinge) {
                                break;
                            }
                        }
                    }
                    if (rule.getFieldConfig() != null) {
                        final String b = rule.getFieldConfig().get("boost");
                        try {
                            if (b != null) {
                                doc.getField(rule.getFieldName()).setBoost(Float.parseFloat(b));
                            }
                        } catch (NumberFormatException e) {
                            throw new NumberFormatException("could not parse boost value: '" + b + "'");
                        }
                    }
                } catch (Exception ex) {
                    log.error("({}) exception while building path indexes for <{}>, field {}: {}", coreName,
                            resource, rule.getFieldName(), ex.getMessage());
                    log.debug("(" + coreName + ") stacktrace", ex);
                }
                if (runtime.getConfiguration().isUpdateDependencies()) {
                    for (List<Value> path : paths.values()) {
                        dependencies.addAll(path);
                    }
                }
            }

            if (runtime.getConfiguration().isUpdateDependencies()) {
                for (Value dependency : dependencies) {
                    if (dependency instanceof URI && !dependency.equals(resource)) {
                        doc.addField("lmf.dependencies", dependency.stringValue());
                    }
                }
            }

            runtime.queueInputDocument(doc);

            connection.commit();
        } finally {
            connection.close();
        }
    } catch (RepositoryException e) {
        log.warn("Could not build index document for " + resource.stringValue(), e);
    } catch (Throwable t) {
        log.error("unknown error while indexing document", t);
    }
}

From source file:au.edu.aekos.shared.solr.index.SubmissionSolrDocumentBuilder.java

public static SolrInputDocument initialiseSHaREDDocument(String id, String doi, String title,
        String datasetNameForCitation, String datasetAbstract, Date firstVisit, Date lastVisit) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", id);
    doc.addField("doi", doi);
    doc.addField("title", title);
    doc.addField("datasetFormalName_t", title);
    doc.addField("datasetNameForCitation_t", datasetNameForCitation);
    doc.addField("description", datasetAbstract);
    doc.addField("datasetAbstract_t", datasetAbstract);
    doc.addField("firstStudyAreaVisitDate_dt", firstVisit);
    doc.addField("lastStudyAreaVisitDate_dt", firstVisit);
    return doc;/*from ww w . j  a  v  a  2s.  c  om*/
}

From source file:au.edu.aekos.shared.solr.index.SubmissionSolrDocumentBuilder.java

public static void addFloraSpecies(List<String> speciesNames, SolrInputDocument doc) {
    for (String speciesName : speciesNames) {
        doc.addField("studySpeciesFlora_txt", speciesName);
        doc.addField("species_txt", speciesName);
    }// ww w  .  j  a v  a 2 s. co  m
}

From source file:au.edu.aekos.shared.solr.index.SubmissionSolrDocumentBuilder.java

public static void addFaunaSpecies(List<String> speciesNames, SolrInputDocument doc) {
    for (String speciesName : speciesNames) {
        doc.addField("studySpeciesFauna_txt", speciesName);
        doc.addField("species_txt", speciesName);
    }/*  ww  w. ja v  a  2  s  .  com*/
}

From source file:au.edu.aekos.shared.solr.index.SubmissionSolrDocumentBuilder.java

public static void addSpatialFeature(String wktFeature, SolrInputDocument doc) {
    doc.addField("geo_mv", wktFeature);
}

From source file:au.org.ala.bhl.service.IndexingService.java

License:Open Source License

private void addItemMetadata(SolrInputDocument doc, JsonNode metadata) {
    String year = metadata.get("Year").getTextValue();
    if (!StringUtils.isEmpty(year)) {
        YearRange range = parseYearRange(year);
        if (range != null) {
            doc.addField("startYear", range.startYear);
            doc.addField("endYear", range.endYear);
        }//  w w w  . j a v a2 s.  c o m
    }

    addField(metadata, "Volume", "volume", doc);
    addField(metadata, "Contributor", "contributor", doc);
    addField(metadata, "Source", "source", doc);

    int titleId = metadata.get("PrimaryTitleID").asInt();
    doc.addField("titleId", titleId);

}

From source file:au.org.ala.bhl.service.IndexingService.java

License:Open Source License

private void addTitleMetadata(SolrInputDocument doc, JsonNode titleData) {
    // Full title
    addField(titleData, "FullTitle", "fullTitle", doc);
    // Publisher Name
    addField(titleData, "PublisherName", "publisherName", doc);
    // Publisher Place
    addField(titleData, "PublisherPlace", "publisherPlace", doc);

    // Author(s)/*  w  w w  .j ava  2s.c om*/
    for (String author : selectField(titleData.get("Authors"), "Name")) {
        doc.addField("author", author);
    }

    // Author ID
    for (String authorId : selectField(titleData.get("Authors"), "CreatorID")) {
        doc.addField("authorId", authorId);
    }

    // Subject(s)
    for (String subject : selectField(titleData.get("Subjects"), "SubjectText")) {
        doc.addField("subject", subject);
    }

    // Publication Dates
    String date = titleData.get("PublicationDate").getTextValue();
    YearRange range = parseYearRange(date);
    if (range != null) {
        doc.addField("publicationStartYear", range.startYear);
        doc.addField("publicationEndYear", range.startYear);
    }

}

From source file:au.org.ala.bhl.service.IndexingService.java

License:Open Source License

private void addField(JsonNode obj, String jsonfield, String indexField, SolrInputDocument doc) {
    String value = obj.get(jsonfield).getTextValue();
    if (!StringUtils.isEmpty(value)) {
        doc.addField(indexField, value);
    }/*  ww w. ja va  2s . c  o  m*/
}

From source file:au.org.intersect.dms.catalogue.db.DatasetIndexer.java

License:Open Source License

public void indexDatasets(Collection<DbDataset> datasets) {
    List<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();

    dataset: for (DbDataset dataset : datasets) {
        LOGGER.debug("Indexing dataset with ID {}", dataset.getId());
        SolrInputDocument sid = new SolrInputDocument();
        addBasicFields(dataset, sid);/*from   w ww  .  ja va 2s . c  o m*/
        StringBuilder summary = new StringBuilder();
        summary.append(dataset.getCreationDate()).append(SPACE).append(dataset.getId()).append(SPACE)
                .append(dataset.getOwner()).append(SPACE).append(dataset.getUrl());

        String metadata;
        try {
            metadata = indexMetadata(dataset, sid);
        } catch (IOException e) {
            LOGGER.error("Exception during indexing. Skipping this dataset.", e);
            continue dataset;
        } catch (TransformerException e) {
            LOGGER.error("Exception during indexing. Skipping this dataset.", e);
            continue dataset;
        }

        if (metadata.length() > 0) {
            sid.addField("dataset.metadata_t", metadata.toString());
            summary.append(SPACE).append(metadata);
        }

        // Add summary field to allow searching documents for objects of this type
        sid.addField("dataset_solrsummary_t", summary);

        documents.add(sid);
    }

    try {
        SolrServer solrServer = DbDataset.solrServer();
        solrServer.add(documents);
        solrServer.commit();
    } catch (SolrServerException e) {
        LOGGER.error(INDEX_EXCEPTION_MESSAGE, e);
    } catch (IOException e) {
        LOGGER.error(INDEX_EXCEPTION_MESSAGE, e);
    }
}