Example usage for org.apache.solr.common SolrDocument getFieldNames

List of usage examples for org.apache.solr.common SolrDocument getFieldNames

Introduction

In this page you can find the example usage for org.apache.solr.common SolrDocument getFieldNames.

Prototype

@Override
public Collection<String> getFieldNames() 

Source Link

Usage

From source file:co.diji.solr.XMLWriter.java

License:Apache License

/**
 * @since solr 1.3/* ww  w  . j  a  v  a2s . com*/
 */
final void writeDoc(String name, SolrDocument doc, Set<String> returnFields, boolean includeScore)
        throws IOException {
    startTag("doc", name, false);

    if (includeScore && returnFields != null) {
        returnFields.add("score");
    }

    for (String fname : doc.getFieldNames()) {
        if (returnFields != null && !returnFields.contains(fname)) {
            continue;
        }
        Object val = doc.getFieldValue(fname);

        writeVal(fname, val);
    }

    writer.write("</doc>");
}

From source file:com.digitalpebble.stormcrawler.solr.persistence.SolrSpout.java

License:Apache License

private void populateBuffer() {
    // TODO Sames as the ElasticSearchSpout?
    // TODO Use the cursor feature?
    // https://cwiki.apache.org/confluence/display/solr/Pagination+of+Results
    SolrQuery query = new SolrQuery();

    query.setQuery("*:*").addFilterQuery("nextFetchDate:[* TO NOW]").setStart(lastStartOffset)
            .setRows(this.bufferSize);

    if (StringUtils.isNotBlank(diversityField)) {
        query.addFilterQuery(String.format("{!collapse field=%s}", diversityField));
        query.set("expand", "true").set("expand.rows", diversityBucketSize);
    }//from w  ww  . ja va2 s.co  m

    try {
        QueryResponse response = connection.getClient().query(query);
        SolrDocumentList docs = new SolrDocumentList();

        if (StringUtils.isNotBlank(diversityField)) {
            // Add the main documents collapsed by the CollapsingQParser
            // plugin
            docs.addAll(response.getResults());

            Map<String, SolrDocumentList> expandedResults = response.getExpandedResults();

            for (String key : expandedResults.keySet()) {
                docs.addAll(expandedResults.get(key));
            }

        } else {
            docs = response.getResults();
        }

        int numhits = response.getResults().size();

        // no more results?
        if (numhits == 0)
            lastStartOffset = 0;
        else
            lastStartOffset += numhits;

        String prefix = mdPrefix.concat(".");

        for (SolrDocument doc : docs) {
            String url = (String) doc.get("url");

            // is already being processed - skip it!
            if (beingProcessed.containsKey(url))
                continue;

            Metadata metadata = new Metadata();

            Iterator<String> keyIterators = doc.getFieldNames().iterator();
            while (keyIterators.hasNext()) {
                String key = keyIterators.next();

                if (key.startsWith(prefix)) {
                    Collection<Object> values = doc.getFieldValues(key);

                    key = StringUtils.replace(key, prefix, "", 1);
                    Iterator<Object> valueIterator = values.iterator();
                    while (valueIterator.hasNext()) {
                        String value = (String) valueIterator.next();

                        metadata.addValue(key, value);
                    }
                }
            }

            buffer.add(new Values(url, metadata));
        }

    } catch (Exception e) {
        LOG.error("Can't query Solr: {}", e);
    }
}

From source file:com.doculibre.constellio.utils.NamedListUtils.java

License:Open Source License

private static Element newSolrDocument(SolrDocument doc) {
    Element nlElement = new Element("lst");
    // Unit test prefer when field are sorted
    List<String> keys = new ArrayList<String>();
    keys.addAll(doc.getFieldNames());
    Collections.sort(keys);/*  w w  w  . j  ava2s.c  o m*/
    for (String key : keys) {
        Collection<Object> c = doc.getFieldValues(key);
        Object value = c.size() > 1 ? c : c.iterator().next();
        if (value != null) {
            Element child = toElement(value);
            if (child != null) {
                nameElement(child, key);
                nlElement.addContent(child);
            }
        }
    }
    return nlElement;
}

From source file:com.gisgraphy.domain.valueobject.SolrResponseDto.java

License:Open Source License

private Map<String, List<String>> getFieldsToMap(SolrDocument solrDocument, String fieldNamePrefix) {
    Map<String, List<String>> result = new HashMap<String, List<String>>();
    for (String fieldName : solrDocument.getFieldNames()) {
        if (fieldName.startsWith(fieldNamePrefix)) {
            for (Object fieldValue : solrDocument.getFieldValues(fieldName)) {
                String fieldValueString = (String) fieldValue;
                String languageCode = fieldName.substring(fieldName.lastIndexOf("_") + 1);
                List<String> languageList = result.get(languageCode);
                if (languageList == null) {
                    languageList = new ArrayList<String>();
                    result.put(languageCode, languageList);
                }//from  w  w w  .  j av  a  2s  .  co  m
                languageList.add(fieldValueString);
            }
        }
    }
    return result;
}

From source file:com.github.fengtan.sophie.tables.DocumentsTable.java

License:Open Source License

/**
 * Upload local changes to the Solr server.
 * // w w  w  . ja  v a2s .  c o  m
 * @throws SophieException
 *             If the local changes could not be uploaded to the Solr
 *             server.
 */
public void upload() throws SophieException {
    // Upload local updates.
    for (SolrDocument document : documentsUpdated) {
        SolrInputDocument input = new SolrInputDocument();
        for (String name : document.getFieldNames()) {
            input.addField(name, document.getFieldValue(name), 1.0f);
        }
        try {
            // Returned object seems to have no relevant information.
            Sophie.client.add(input);
        } catch (SolrServerException | IOException | SolrException e) {
            throw new SophieException("Unable to update document in index: " + input.toString(), e);
        }
    }
    // Upload local deletions.
    for (SolrDocument document : documentsDeleted) {
        String id = Objects.toString(document.getFieldValue(uniqueField), StringUtils.EMPTY);
        try {
            Sophie.client.deleteById(id);
        } catch (SolrServerException | IOException | SolrException e) {
            throw new SophieException("Unable to delete document from index: " + id, e);
        }
    }
    // Upload local additions.
    for (SolrDocument document : documentsAdded) {
        SolrInputDocument input = new SolrInputDocument();
        for (String name : document.getFieldNames()) {
            input.addField(name, document.getFieldValue(name), BOOST);
        }
        try {
            // Returned object seems to have no relevant information.
            Sophie.client.add(input);
        } catch (SolrServerException | IOException | SolrException e) {
            throw new SophieException("Unable to add document to index: " + input.toString(), e);
        }
    }
    // Commit the index.
    try {
        Sophie.client.commit();
    } catch (SolrServerException | IOException | SolrException e) {
        throw new SophieException("Unable to commit index", e);
    }

    // Refresh so user can see the new state of the server.
    refresh();
}

From source file:com.gu.solr.MergeUtils.java

License:Apache License

private static SolrInputDocument copy(SolrDocument document) {
    SolrInputDocument copy = new SolrInputDocument();

    for (String name : document.getFieldNames()) {
        copy.addField(name, document.getFieldValue(name));
    }/*from  www  .j a v a2  s  .co  m*/

    return copy;
}

From source file:com.hurence.logisland.service.solr.api.SolrRecordConverter.java

License:Apache License

public SolrInputDocument toSolrInputDocument(SolrDocument document) {
    SolrInputDocument inputDocument = createNewSolrInputDocument();

    for (String name : document.getFieldNames()) {
        inputDocument.addField(name, document.getFieldValue(name));
    }// w w  w . j a  v a 2 s  .  co  m

    return inputDocument;
}

From source file:com.liferay.portal.search.solr.internal.SolrIndexSearcher.java

License:Open Source License

protected Document processSolrDocument(SolrDocument solrDocument, QueryConfig queryConfig) {

    Document document = new DocumentImpl();

    Collection<String> fieldNames = solrDocument.getFieldNames();

    for (String fieldName : fieldNames) {
        Collection<Object> fieldValues = solrDocument.getFieldValues(fieldName);

        Field field = new Field(fieldName,
                ArrayUtil.toStringArray(fieldValues.toArray(new Object[fieldValues.size()])));

        document.add(field);/*w w w  .  ja v a2  s  .  co  m*/
    }

    populateUID(document, queryConfig);

    return document;
}

From source file:com.liferay.portal.search.solr.SolrIndexSearcher.java

License:Open Source License

protected Document processSolrDocument(SolrDocument solrDocument) {
    Document document = new DocumentImpl();

    Collection<String> fieldNames = solrDocument.getFieldNames();

    for (String fieldName : fieldNames) {
        Collection<Object> fieldValues = solrDocument.getFieldValues(fieldName);

        Field field = new Field(fieldName,
                ArrayUtil.toStringArray(fieldValues.toArray(new Object[fieldValues.size()])));

        document.add(field);/*from ww  w .ja va  2 s . c o m*/
    }

    return document;
}

From source file:com.liferay.portal.search.solr.SolrIndexSearcherImpl.java

License:Open Source License

protected Hits subset(SolrQuery solrQuery, Query query, QueryConfig queryConfig, QueryResponse queryResponse,
        boolean allResults) throws Exception {

    long startTime = System.currentTimeMillis();

    Hits hits = new HitsImpl();

    SolrDocumentList solrDocumentList = queryResponse.getResults();

    long total = solrDocumentList.getNumFound();

    if (allResults && (total > 0)) {
        solrQuery.setRows((int) total);

        queryResponse = _solrServer.query(solrQuery);

        return subset(solrQuery, query, queryConfig, queryResponse, false);
    }/*from www  . j  a  v  a2  s .  com*/

    float maxScore = 1;

    if (queryConfig.isScoreEnabled()) {
        maxScore = solrDocumentList.getMaxScore();
    }

    int subsetTotal = solrDocumentList.size();

    Document[] documents = new DocumentImpl[subsetTotal];
    String[] snippets = new String[subsetTotal];
    float[] scores = new float[subsetTotal];

    int j = 0;

    Set<String> queryTerms = new HashSet<String>();

    Map<String, Map<String, List<String>>> highlights = queryResponse.getHighlighting();

    for (SolrDocument solrDocument : solrDocumentList) {
        Document document = new DocumentImpl();

        Collection<String> names = solrDocument.getFieldNames();

        for (String name : names) {
            Collection<Object> fieldValues = solrDocument.getFieldValues(name);

            Field field = new Field(name,
                    ArrayUtil.toStringArray(fieldValues.toArray(new Object[fieldValues.size()])));

            document.add(field);
        }

        float score = 1;

        if (queryConfig.isScoreEnabled()) {
            score = GetterUtil.getFloat(String.valueOf(solrDocument.getFieldValue("score")));
        }

        documents[j] = document;

        if (queryConfig.isHighlightEnabled()) {
            snippets[j] = getSnippet(solrDocument, queryTerms, highlights);
        } else {
            snippets[j] = StringPool.BLANK;
        }

        scores[j] = score / maxScore;

        j++;
    }

    float searchTime = (float) (System.currentTimeMillis() - startTime) / Time.SECOND;

    hits.setDocs(documents);
    hits.setLength((int) total);
    hits.setQuery(query);
    hits.setQueryTerms(queryTerms.toArray(new String[queryTerms.size()]));
    hits.setScores(scores);
    hits.setSearchTime(searchTime);
    hits.setSnippets(snippets);
    hits.setStart(startTime);

    return hits;
}