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

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

Introduction

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

Prototype

@Override
    public Set<Entry<String, Object>> entrySet() 

Source Link

Usage

From source file:au.edu.aekos.shared.service.index.PublishSubmissionToSolrTestIT.java

License:Creative Commons License

private void assertThatDocumentCanBeQueried(Submission s) throws SolrServerException {
    SolrServer server = objectUnderTest.getSolrServer();
    QueryResponse rsp = server.query(new SolrQuery("id:" + s.getId().toString()));
    assertEquals(1, rsp.getResults().size());
    SolrDocument doc = rsp.getResults().get(0);
    logger.info("Solr document contents:");
    for (Entry<String, Object> entry : doc.entrySet()) {
        logger.info(entry.getKey() + "=" + entry.getValue());
    }//w  w w  .j a  va2 s.  c  o  m
}

From source file:com.att.archive.restful.util.SolrDocumentToContentConverter.java

@Override
public ArchiveDocument convert(SolrDocument source) {
    final DefaultConversionService conversionService;
    Entry<String, Object> entry;
    Iterator<Entry<String, Object>> itr = source.entrySet().iterator();
    SearchableContent content = new ArchiveDocument();
    while (itr.hasNext()) {
        entry = itr.next();/*from w  w  w  . jav a 2s. c  o m*/
        content.put(entry.getKey(), entry.getValue());
    }
    return (ArchiveDocument) content;
}

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

License:Apache License

public Map<String, Map<String, String>> toMap(SolrDocument document, String uniqueKey) {
    Map<String, Map<String, String>> map = new HashMap<>();
    Map<String, String> retrievedFields = new HashMap<>();
    String uniqueKeyValue = null;
    for (Map.Entry<String, Object> entry : document.entrySet()) {
        String name = entry.getKey();
        String value = entry.getValue().toString();

        if (isSolrDocumentFieldIgnored(name)) {
            continue;
        }//from  w  w w.  j a v  a  2s .  c om

        if (name.equals(uniqueKey)) {
            uniqueKeyValue = value;
            continue;
        }

        retrievedFields.put(name, value);
    }

    if (uniqueKeyValue != null) {
        map.put(uniqueKeyValue, retrievedFields);
    }

    return map;
}

From source file:com.ngdata.hbaseindexer.mr.HBaseMapReduceIndexerToolGoLiveTest.java

License:Apache License

private void verifySolrContents() throws Exception {

    // verify query
    assertEquals(RECORD_COUNT, executeSolrQuery("*:*").getNumFound());
    assertEquals(1, executeSolrQuery("firstname_s:John0001").getNumFound());
    int i = 0;//from   w ww  . j  a  v a2s . c o  m
    for (SolrDocument doc : executeSolrQuery("*:*")) {
        assertEquals(String.format("row%04d", i), doc.getFirstValue("id"));
        assertEquals(String.format("John%04d", i), doc.getFirstValue("firstname_s"));
        assertEquals(String.format("Doe%04d", i), doc.getFirstValue("lastname_s"));

        // perform update
        doc.removeFields("_version_");
        SolrInputDocument update = new SolrInputDocument();
        for (Map.Entry<String, Object> entry : doc.entrySet()) {
            update.setField(entry.getKey(), entry.getValue());
        }
        update.setField("firstname_s", String.format("Nadja%04d", i));
        COLLECTION1.add(update);
        i++;
    }
    assertEquals(RECORD_COUNT, i);
    COLLECTION1.commit();

    // verify updates
    assertEquals(RECORD_COUNT, executeSolrQuery("*:*").getNumFound());
    i = 0;
    for (SolrDocument doc : executeSolrQuery("*:*")) {
        assertEquals(String.format("row%04d", i), doc.getFirstValue("id"));
        assertEquals(String.format("Nadja%04d", i), doc.getFirstValue("firstname_s"));
        assertEquals(String.format("Doe%04d", i), doc.getFirstValue("lastname_s"));

        // perform delete
        COLLECTION1.deleteById((String) doc.getFirstValue("id"));
        i++;
    }
    assertEquals(RECORD_COUNT, i);
    COLLECTION1.commit();

    // verify deletes
    assertEquals(0, executeSolrQuery("*:*").size());
}

From source file:com.nridge.ds.solr.SolrResponseBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
private void populateDocument(Document aDocument, SolrDocumentList aSolrDocumentList) {
    Date entryDate;// ww w .j  a  va  2s .c o m
    DataBag resultBag;
    Object entryObject;
    DataField dataField;
    String cellName, cellValue;
    ArrayList<Object> objectArrayList;
    ArrayList<String> stringArrayList;
    Logger appLogger = mAppMgr.getLogger(this, "populateDocument");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    if ((aDocument != null) && (aSolrDocumentList != null)) {
        DataTable resultTable = aDocument.getTable();

        if (mIsSchemaStatic)
            resultTable.emptyRows();
        else {
            resultTable.empty();
            resultBag = resultTable.getColumnBag();
            resultBag.setTitle("Document Table");
            for (SolrDocument solrDocument : aSolrDocumentList) {
                for (Map.Entry<String, Object> entry : solrDocument.entrySet()) {
                    cellName = entry.getKey();
                    dataField = resultBag.getFieldByName(cellName);
                    if (dataField == null) {
                        entryObject = entry.getValue();
                        dataField = new DataField(Field.getTypeField(entryObject), cellName,
                                Field.nameToTitle(cellName));
                        if (Solr.isSolrReservedFieldName(cellName))
                            dataField.addFeature(Field.FEATURE_IS_VISIBLE, StrUtl.STRING_FALSE);
                        resultBag.add(dataField);
                    }
                }
            }
        }
        resultBag = resultTable.getColumnBag();
        for (SolrDocument solrDocument : aSolrDocumentList) {
            resultTable.newRow();
            for (Map.Entry<String, Object> entry : solrDocument.entrySet()) {
                cellName = entry.getKey();
                entryObject = entry.getValue();
                if (entryObject instanceof ArrayList) {
                    stringArrayList = new ArrayList<String>();
                    objectArrayList = (ArrayList<Object>) entryObject;
                    for (Object alo : objectArrayList) {
                        cellValue = alo.toString();
                        stringArrayList.add(cellValue);
                    }
                    resultTable.setValuesByName(cellName, stringArrayList);
                } else {
                    cellValue = entryObject.toString();
                    if (StringUtils.isNotEmpty(cellValue)) {
                        dataField = resultBag.getFieldByName(cellName);
                        if ((dataField != null) && (dataField.isTypeDateOrTime())) {
                            entryDate = (Date) entryObject;
                            resultTable.setValueByName(cellName, entryDate);
                        } else
                            resultTable.setValueByName(cellName, cellValue);
                    }
                }
            }
            resultTable.addRow();
        }
    }

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

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);

    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());
    }/* ww w .ja  v a 2 s  .c om*/
    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:eu.europeana.uim.gui.cp.server.RetrievalServiceImpl.java

License:Open Source License

private EdmFieldRecordDTO createSolrFields(QueryResponse response) {
    EdmFieldRecordDTO solrFields = new EdmFieldRecordDTO();
    SolrDocument doc = response.getResults().get(0);
    List<FieldValueDTO> fieldValues = new ArrayList<FieldValueDTO>();
    for (Entry<String, Object> entry : doc.entrySet()) {
        FieldValueDTO fieldValue = new FieldValueDTO();
        List<String> values = new ArrayList<String>();
        fieldValue.setFieldName(entry.getKey());
        if (entry.getValue() instanceof ArrayList) {
            for (Object obj : (ArrayList<Object>) entry.getValue()) {
                values.add(obj.toString());
            }/*from   w  w  w  . j a  v  a 2s  .  c o  m*/
        }
        if (entry.getValue() instanceof String) {
            values.add((String) entry.getValue());
        }
        if (entry.getValue() instanceof Float) {
            values.add(Float.toString((Float) entry.getValue()));
        }
        if (entry.getValue() instanceof String[]) {
            for (String str : (String[]) entry.getValue()) {
                values.add(str);
            }
        }
        if (entry.getValue() instanceof Date) {
            values.add(((Date) entry.getValue()).toString());
        }
        fieldValue.setFieldValue(values);
        fieldValues.add(fieldValue);
    }
    solrFields.setFieldValue(fieldValues);
    return solrFields;
}

From source file:it.damore.solr.importexport.App.java

License:Open Source License

/**
 * @param client//  w  w w.  j  ava  2 s.com
 * @param outputFile
 * @throws SolrServerException
 * @throws IOException
 */
private static void readAllDocuments(HttpSolrClient client, File outputFile)
        throws SolrServerException, IOException {

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*:*");
    if (config.getFilterQuery() != null) {
        solrQuery.addFilterQuery(config.getFilterQuery());
    }
    solrQuery.setRows(0);

    solrQuery.addSort(config.getUniqueKey(), ORDER.asc); // Pay attention to this line

    String cursorMark = CursorMarkParams.CURSOR_MARK_START;

    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

    // objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
    DateFormat df = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:sss'Z'");
    objectMapper.setDateFormat(df);
    objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

    QueryResponse r = client.query(solrQuery);

    long nDocuments = r.getResults().getNumFound();
    logger.info("Found " + nDocuments + " documents");

    if (!config.getDryRun()) {
        logger.info("Creating " + config.getFileName());

        Set<SkipField> skipFieldsEquals = config.getSkipFieldsSet().stream()
                .filter(s -> s.getMatch() == MatchType.EQUAL).collect(Collectors.toSet());
        Set<SkipField> skipFieldsStartWith = config.getSkipFieldsSet().stream()
                .filter(s -> s.getMatch() == MatchType.STARTS_WITH).collect(Collectors.toSet());
        Set<SkipField> skipFieldsEndWith = config.getSkipFieldsSet().stream()
                .filter(s -> s.getMatch() == MatchType.ENDS_WITH).collect(Collectors.toSet());

        try (PrintWriter pw = new PrintWriter(outputFile)) {
            solrQuery.setRows(200);
            boolean done = false;
            while (!done) {
                solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
                QueryResponse rsp = client.query(solrQuery);
                String nextCursorMark = rsp.getNextCursorMark();

                for (SolrDocument d : rsp.getResults()) {
                    skipFieldsEquals.forEach(f -> d.removeFields(f.getText()));
                    if (skipFieldsStartWith.size() > 0 || skipFieldsEndWith.size() > 0) {
                        Map<String, Object> collect = d.entrySet().stream()
                                .filter(e -> !skipFieldsStartWith.stream()
                                        .filter(f -> e.getKey().startsWith(f.getText())).findFirst()
                                        .isPresent())
                                .filter(e -> !skipFieldsEndWith.stream()
                                        .filter(f -> e.getKey().endsWith(f.getText())).findFirst().isPresent())
                                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
                        pw.write(objectMapper.writeValueAsString(collect));
                    } else {
                        pw.write(objectMapper.writeValueAsString(d));
                    }
                    pw.write("\n");
                }
                if (cursorMark.equals(nextCursorMark)) {
                    done = true;
                }

                cursorMark = nextCursorMark;
            }

        }
    }

}

From source file:net.hasor.search.server.rsf.service.AbstractSearchService.java

License:Apache License

protected SearchDocument convetTo(SolrDocument solrDocument) {
    Set<Map.Entry<String, Object>> docDataEntrySet = solrDocument.entrySet();
    SearchDocument document = new SearchDocument();
    for (Map.Entry<String, Object> entry : docDataEntrySet) {
        document.setField(entry.getKey(), entry.getValue());
    }/*www.j  a  va2  s.c  om*/
    List<SolrDocument> solrList = solrDocument.getChildDocuments();
    if (solrList != null) {
        for (SolrDocument solrDoc : solrList) {
            document.addChildDocument(convetTo(solrDoc));
        }
    }
    return document;
}

From source file:nl.knaw.dans.common.solr.converter.SolrQueryResponseConverter.java

License:Apache License

public static SimpleSearchResult<Document> convert(QueryResponse queryResponse, Index index) {
    SimpleSearchResult<Document> result = new SimpleSearchResult<Document>();

    Map<String, Map<String, List<String>>> hl = queryResponse.getHighlighting();
    SolrDocumentList sdl = queryResponse.getResults();

    // paging info
    result.setNumFound((int) sdl.getNumFound());

    // Relevance scores in Solr are calculated from the base
    // of 1.0f. If any document is scored any different then 
    // the maximum relevance score is not 1.0f anymore. The
    // chances of a maximum relevance score of 1.0f with actual
    // meaning is pretty slim. This therefore assumes that if
    // a maximum relevance score of 1.0f is returned that it
    // is then better for the receiver of the search results
    // to ignore the relevancy score completely.
    result.setUseRelevanceScore(sdl.getMaxScore() != 1.0f);

    // add the documents
    List<SearchHit<Document>> hits = new ArrayList<SearchHit<Document>>(sdl.size());
    String primaryKeyValue = null;
    for (SolrDocument solrDoc : sdl) {
        // Don't change class type! SimpleSearchHit is assumed in SolrSearchEngine!
        SimpleDocument resultDoc = new SimpleDocument();
        float score = 0;
        List<SnippetField> snippetFields = null;

        // copy all fields
        for (Entry<String, Object> fieldEntry : solrDoc.entrySet()) {
            if (index != null) {
                if (fieldEntry.getKey().equals(index.getPrimaryKey())) {
                    primaryKeyValue = fieldEntry.getValue().toString();
                }/*  w w  w .ja  v  a  2 s.com*/
            }

            if (fieldEntry.getKey().equals("score")) {
                score = ((Float) fieldEntry.getValue()).floatValue() / sdl.getMaxScore();
            } else {
                SimpleField<Object> field = new SimpleField<Object>(fieldEntry.getKey(), fieldEntry.getValue());
                resultDoc.addField(field);
            }
        }

        // add highlight info to SearchHit
        if (hl != null && primaryKeyValue != null) {
            Map<String, List<String>> hlMap = hl.get(primaryKeyValue);
            if (hlMap != null && hlMap.size() > 0) {
                snippetFields = new ArrayList<SnippetField>(hlMap.size());
                for (Entry<String, List<String>> hlEntry : hlMap.entrySet()) {
                    SimpleSnippetField snippetField = new SimpleSnippetField(hlEntry.getKey(),
                            hlEntry.getValue());
                    snippetFields.add(snippetField);
                }
            }
        }

        SimpleSearchHit<Document> hit = new SimpleSearchHit<Document>(resultDoc);
        hit.setRelevanceScore(score);
        if (snippetFields != null)
            hit.setSnippets(snippetFields);
        hits.add(hit);
    }
    result.setHits(hits);

    // add facet fields to response
    List<org.apache.solr.client.solrj.response.FacetField> solrFacets = queryResponse.getFacetFields();
    if (solrFacets != null) {
        List<FacetField> facetFields = new ArrayList<FacetField>(solrFacets.size());
        for (org.apache.solr.client.solrj.response.FacetField solrFacet : solrFacets) {
            List<Count> solrFacetValues = solrFacet.getValues();
            if (solrFacetValues == null)
                continue;
            List<FacetValue<?>> facetValues = new ArrayList<FacetValue<?>>(solrFacetValues.size());
            for (Count solrFacetValue : solrFacetValues) {
                SimpleFacetValue<String> facetValue = new SimpleFacetValue<String>();
                facetValue.setCount((int) solrFacetValue.getCount());
                facetValue.setValue(solrFacetValue.getName());
                facetValues.add(facetValue);
            }

            facetFields.add(new SimpleFacetField(solrFacet.getName(), facetValues));
        }
        result.setFacets(facetFields);
    }

    return result;
}