Java Code Examples for org.apache.solr.common.SolrDocument

The following are top voted examples for showing how to use org.apache.solr.common.SolrDocument. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: goobi-viewer-connector   File: SolrSearchIndex.java   View source code 7 votes vote down vote up
/**
 * Returns the latest DATEUPDATED value on the given <code>SolrDocument</code> that is no larger than <code>untilTimestamp</code>. Returns 0 if no
 * such value is found.
 * 
 * @param doc
 * @param untilTimestamp
 * @return Latest DATEUPDATED value is less than or equals untilTimestamp on doc; 0 if none found.
 * @should return correct value
 * @should return 0 if no valid value is found
 */
public static Long getLatestValidDateUpdated(SolrDocument doc, long untilTimestamp) {
    long ret = 0;
    Collection<Object> dateUpdatedValues = doc.getFieldValues(SolrConstants.DATEUPDATED);
    if (dateUpdatedValues != null && !dateUpdatedValues.isEmpty()) {
        // Get latest DATEUPDATED values
        for (Object o : dateUpdatedValues) {
            long dateUpdated = (Long) o;
            if (dateUpdated > ret && dateUpdated <= untilTimestamp) {
                ret = dateUpdated;
            }
        }
    }

    return ret;
}
 
Example 2
Project: solr-autocomplete   File: AutocompleteUpdateRequestProcessor.java   View source code 7 votes vote down vote up
private SolrInputDocument fetchExistingOrCreateNewSolrDoc(String id) throws SolrServerException, IOException {
  Map<String, String> p = new HashMap<String, String>();
  p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\"");
  
  SolrParams params = new MapSolrParams(p);
  
  QueryResponse res = solrAC.query(params);
  
  if (res.getResults().size() == 0) {
    return new SolrInputDocument();
  } else if (res.getResults().size() == 1) {
    SolrDocument doc = res.getResults().get(0);
    SolrInputDocument tmp = new SolrInputDocument();
    
    for (String fieldName : doc.getFieldNames()) {
      tmp.addField(fieldName, doc.getFieldValue(fieldName));
    }
    return tmp;
  } else {
    throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!");
  }
}
 
Example 3
Project: vlo-curation   File: PermalinkServiceImpl.java   View source code 7 votes vote down vote up
@Override
public String getUrlString(Class<? extends Page> pageClass, QueryFacetsSelection selection, SolrDocument document) {
    final PageParameters params = new PageParameters();
    if (selection != null) {
        params.mergeWith(paramsConverter.toParameters(selection));
    }

    if (document != null) {
        params.add(VloWebAppParameters.DOCUMENT_ID, document.getFirstValue(FacetConstants.FIELD_ID));
    }

    final String style = Session.get().getStyle();
    if (style != null) {
        params.add(VloWebAppParameters.THEME, style);
    }

    final CharSequence url = RequestCycle.get().urlFor(pageClass, params);
    final String absoluteUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(Url.parse(url));
    return absoluteUrl;
}
 
Example 4
Project: goobi-viewer-connector   File: SolrSearchIndex.java   View source code 6 votes vote down vote up
/**
 * If the given SolrDocument is an anchor, retrieve the latest DATEUPDATED timestamp value from its volumes.
 * 
 * @param anchorDoc
 * @param untilTimestamp
 * @return
 * @throws SolrServerException
 */
public long getLatestVolumeTimestamp(SolrDocument anchorDoc, long untilTimestamp) throws SolrServerException {
    if (anchorDoc.getFieldValue(SolrConstants.ISANCHOR) != null && (Boolean) anchorDoc.getFieldValue(SolrConstants.ISANCHOR)) {
        SolrDocumentList volumes = search(SolrConstants.ISWORK + ":true AND " + SolrConstants.IDDOC_PARENT + ":" + (String) anchorDoc
                .getFieldValue(SolrConstants.IDDOC));
        if (volumes != null) {
            long latest = 0;
            for (SolrDocument volume : volumes) {
                long volumeTimestamp = getLatestValidDateUpdated(volume, untilTimestamp);
                if (latest < volumeTimestamp) {
                    latest = volumeTimestamp;
                }
            }

            if (latest > 0) {
                return latest;
            }
        }
    }

    return -1;
}
 
Example 5
Project: goobi-viewer-connector   File: XMLGeneration.java   View source code 6 votes vote down vote up
/**
 * for the server request ?verb=ListIdentifiers this method build the xml section
 * 
 * @param handler
 * @param firstRow
 * @param numRows
 * @return
 * @throws SolrServerException
 */
public Element createListIdentifiers(RequestHandler handler, int firstRow, int numRows) throws SolrServerException {
    Map<String, String> datestamp = filterDatestampFromRequest(handler);
    SolrDocumentList listIdentifiers = solr.getListIdentifiers(datestamp, firstRow, numRows, false);
    if (listIdentifiers == null || listIdentifiers.isEmpty()) {
        return new ErrorCode().getNoRecordsMatch();
    }

    Namespace xmlns = DataManager.getInstance().getConfiguration().getStandardNameSpace();
    Element xmlListIdentifiers = new Element("ListIdentifiers", xmlns);
    long totalHits = listIdentifiers.getNumFound();
    for (int i = 0; i < listIdentifiers.size(); i++) {
        SolrDocument doc = listIdentifiers.get(i);
        Element header = getHeader(doc, null, handler);
        xmlListIdentifiers.addContent(header);
    }

    // Create resumption token
    if (totalHits > firstRow + numRows) {
        Element resumption = createResumptionTokenAndElement(totalHits, firstRow + numRows, xmlns, handler);
        xmlListIdentifiers.addContent(resumption);
    }

    return xmlListIdentifiers;
}
 
Example 6
Project: solr-sql   File: SolrTable.java   View source code 6 votes vote down vote up
private boolean readNextPage() {
	if (totalCountOfRows < 0 || startOfCurrentPage < totalCountOfRows) {
		mySolrQuery.set("start", startOfCurrentPage);
		mySolrQuery.set("rows", pageSize);
		startOfCurrentPage += pageSize;
		QueryResponse rsp = null;
		List<Object[]> rows = new ArrayList<>();
		try {
			rsp = solrClientFactory.getClient().query(mySolrQuery);
			SolrDocumentList docs = rsp.getResults();
			totalCountOfRows = docs.getNumFound();
			for (SolrDocument doc : docs) {
				rows.add(doc2Row(doc));
			}
		} catch (Exception e) {
			logger.error(e);
		}
		rowIteratorWithinCurrentPage = rows.iterator();
		return true;
	}
	return false;
}
 
Example 7
Project: vind   File: SolrUtils.java   View source code 6 votes vote down vote up
public static GetResult buildRealTimeGetResult(QueryResponse response, RealTimeGet query, DocumentFactory factory) {
    final String DOC = "doc";

    long nResults = 0;
    List<Document> docResults = new ArrayList<>();

    final SolrDocumentList results = response.getResults();
    if(results != null && results.size() >0){
        docResults = buildResultList(results, null, factory, null);
        nResults = docResults.size();
    } else {
        final SolrDocument solrDoc = (SolrDocument)response.getResponse().get(DOC);
        if(solrDoc != null) {
            final SolrDocumentList solrDocuments = new SolrDocumentList();
            solrDocuments.add(solrDoc);
            docResults = buildResultList(solrDocuments, null, factory, null);
            nResults = 1;
        }
    }

    return new GetResult(nResults,docResults,query,factory);
}
 
Example 8
Project: kowalski   File: WorkerPoolParser.java   View source code 6 votes vote down vote up
private Function<Object, Object> newMapper(String mapper) {
	if (mapper.equals(MAPPER_NEW_URI)) {
		return object -> {
			try {
				return Dependent.newUri((Artifact) object);
			} catch (URISyntaxException exception) {
				throw new RuntimeException(exception);
			}
		};
	}
	if (mapper.equals(MAPPER_NEW_ARTIFACT_WITH_VERSION)) {
		return object -> Match.newArtifactWithVersion((SolrDocument) object);
	}
	if (mapper.equals(MAPPER_NEW_ARTIFACT_WITH_LATEST_VERSION)) {
		return object -> Match.newArtifactWithLatestVersion((SolrDocument) object);
	}
	if (mapper.equals(MAPPER_NEW_SOLR_QUERY_FOR_ALL_VERSIONS)) {
		return object -> Match.newSolrQueryForAllVersions((Artifact) object);
	}
	if (mapper.equals(MAPPER_NEW_SOLR_QUERY_FOR_LATEST_VERSION)) {
		return object -> Match.newSolrQueryForLatestVersion((Artifact) object);
	}
	throw new IllegalArgumentException(
			"Mapper must be one of newUri, newArtifactWithVersion, newArtifactWithLatestVersion or newSolrQuery");
}
 
Example 9
Project: goobi-viewer-indexer   File: LidoIndexer.java   View source code 6 votes vote down vote up
/**
 * Prepares the given record for an update. Creation timestamp is preserved. A new update timestamp is added, child docs are removed.
 * 
 * @param indexObj {@link IndexObject}
 * @throws IOException -
 * @throws SolrServerException
 * @throws FatalIndexerException
 */
private void prepareUpdate(IndexObject indexObj) throws IOException, SolrServerException, FatalIndexerException {
    String pi = indexObj.getPi().trim();
    SolrDocumentList hits = hotfolder.getSolrHelper().search(SolrConstants.PI + ":" + pi, null);
    if (hits != null && hits.getNumFound() > 0) {
        logger.debug("This file has already been indexed, initiating an UPDATE instead...");
        indexObj.setUpdate(true);
        SolrDocument doc = hits.get(0);
        // Set creation timestamp, if exists (should never be updated)
        Object dateCreated = doc.getFieldValue(SolrConstants.DATECREATED);
        if (dateCreated != null) {
            // Set creation timestamp, if exists (should never be updated)
            indexObj.setDateCreated((Long) dateCreated);
        }
        // Set update timestamp
        Collection<Object> dateUpdatedValues = doc.getFieldValues(SolrConstants.DATEUPDATED);
        if (dateUpdatedValues != null) {
            for (Object date : dateUpdatedValues) {
                indexObj.getDateUpdated().add((Long) date);
            }
        }
        // Recursively delete all children
        deleteWithPI(pi, false, hotfolder.getSolrHelper());
    }
}
 
Example 10
Project: goobi-viewer-indexer   File: SolrHelper.java   View source code 6 votes vote down vote up
/**
 * Performs an atomic update of the given solr document. Updates defined in partialUpdates will be applied to the existing document without making
 * any changes to other fields.
 * 
 * @param doc
 * @param partialUpdates Map of update operations (usage: Map<field, Map<operation, value>>)
 * @return
 * @throws FatalIndexerException
 * @should update doc correctly
 * @should add GROUPFIELD if original doc doesn't have it
 */
public boolean updateDoc(SolrDocument doc, Map<String, Map<String, Object>> partialUpdates) throws FatalIndexerException {
    String iddoc = (String) doc.getFieldValue(SolrConstants.IDDOC);
    SolrInputDocument newDoc = new SolrInputDocument();
    newDoc.addField(SolrConstants.IDDOC, iddoc);
    if (!doc.containsKey(SolrConstants.GROUPFIELD)) {
        logger.warn("Document to update {} doesn't contain {} adding now.", iddoc, SolrConstants.GROUPFIELD);
        Map<String, Object> update = new HashMap<>();
        update.put("set", iddoc);
        newDoc.addField(SolrConstants.GROUPFIELD, update);
    }
    for (String field : partialUpdates.keySet()) {
        newDoc.addField(field, partialUpdates.get(field));
    }
    if (writeToIndex(newDoc)) {
        commit(false);
        return true;
    }

    return false;
}
 
Example 11
Project: goobi-viewer-indexer   File: SolrHelper.java   View source code 6 votes vote down vote up
public int removeGrievingAnchors() throws FatalIndexerException {
    String[] fields = { SolrConstants.IDDOC, SolrConstants.PI };
    try {
        List<String> toDelete = new ArrayList<>();
        SolrDocumentList anchors = search(SolrConstants.ISANCHOR + ":true", Arrays.asList(fields));
        for (SolrDocument anchor : anchors) {
            String iddoc = (String) anchor.getFirstValue(SolrConstants.IDDOC);
            String pi = (String) anchor.getFirstValue(SolrConstants.PI);
            if (search(SolrConstants.PI_PARENT + ":" + pi, Collections.singletonList(SolrConstants.IDDOC)).getNumFound() == 0) {
                toDelete.add(iddoc);
                logger.info("{} has no volumes and will be deleted.", pi);
            }
        }
        if (!toDelete.isEmpty()) {
            deleteDocuments(toDelete);
            commit(false);
            return toDelete.size();
        }
    } catch (SolrServerException e) {
        logger.error(e.getMessage(), e);
    }

    return 0;
}
 
Example 12
Project: smarti   File: ConversationMltQueryBuilder.java   View source code 6 votes vote down vote up
@Override
protected ConversationResult toHassoResult(ComponentConfiguration conf, SolrDocument solrDocument, String type) {
    final ConversationResult hassoResult = new ConversationResult(getCreatorName(conf));

    hassoResult.setScore(Double.parseDouble(String.valueOf(solrDocument.getFieldValue("score"))));

    hassoResult.setContent(String.valueOf(solrDocument.getFirstValue(FIELD_MESSAGE)));
    hassoResult.setReplySuggestion(hassoResult.getContent());

    hassoResult.setConversationId(String.valueOf(solrDocument.getFieldValue(FIELD_CONVERSATION_ID)));
    hassoResult.setMessageId(String.valueOf(solrDocument.getFieldValue(FIELD_MESSAGE_ID)));
    hassoResult.setMessageIdx(Integer.parseInt(String.valueOf(solrDocument.getFieldValue(FIELD_MESSAGE_IDX))));

    hassoResult.setVotes(Integer.parseInt(String.valueOf(solrDocument.getFieldValue(FIELD_VOTE))));

    hassoResult.setTimestamp((Date) solrDocument.getFieldValue(FIELD_TIME));
    hassoResult.setUserName((String) solrDocument.getFieldValue(FIELD_USER_NAME));

    return hassoResult;
}
 
Example 13
Project: Camel   File: SolrUpdateTest.java   View source code 6 votes vote down vote up
@Test
public void indexPDFDocumentToExtractingRequestHandler() throws Exception {
    solrEndpoint.setRequestHandler("/update/extract");

    Exchange exchange = createExchangeWithBody(new File("src/test/resources/data/tutorial.pdf"));
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader("SolrParam.literal.id", "tutorial.pdf");

    template.send("direct:start", exchange);
    solrCommit();

    QueryResponse response = executeSolrQuery("*:*");
    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());

    SolrDocument doc = response.getResults().get(0);
    assertEquals("Solr", doc.getFieldValue("subject"));
    assertEquals("tutorial.pdf", doc.getFieldValue("id"));
    assertEquals(Arrays.asList("application/pdf"), doc.getFieldValue("content_type"));
}
 
Example 14
Project: multifield_suggester_code   File: TestSuggestCollectionBuilder.java   View source code 6 votes vote down vote up
private synchronized boolean responseIs( QueryResponse rsp, String field, String ... queries ) {
  System.out.println( field  + " " + rsp.getResults( ).size( ) );
  HashSet<String> expected = new HashSet<String>( );
  for ( int i = 0; i < queries.length; i++) {
    System.out.println( "adding '" + queries[i] + "'" );
    expected.add( queries[i] );
  }
  for (SolrDocument doc : rsp.getResults() ) {
    String val = String.valueOf( doc.getFirstValue( field ) );
    System.out.println( "   '" + val + "'" );
    if (expected.contains( val )) {
      System.out.println( "removing '" + val + "'" );
      expected.remove( val );
    }
  }
  System.out.println( expected.size( ) + " left " );
  return expected.size() == 0;
}
 
Example 15
Project: mycore   File: MCROAISolrSearcher.java   View source code 6 votes vote down vote up
protected MCROAISolrResult solrQuery(Optional<String> cursor) throws SolrServerException, IOException {
    SolrQuery query = getBaseQuery(CommonParams.Q);

    // set support
    if (this.set != null) {
        String setId = this.set.getSetId();
        MCROAISetConfiguration<SolrQuery, SolrDocument, String> setConfig = getSetManager().getConfig(setId);
        setConfig.getHandler().apply(this.set, query);
    }
    // from & until
    if (this.from != null || this.until != null) {
        String fromUntilCondition = buildFromUntilCondition(this.from, this.until);
        query.add(CommonParams.FQ, fromUntilCondition);
    }

    // cursor
    query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursor.orElse(CursorMarkParams.CURSOR_MARK_START));
    query.set(CommonParams.ROWS, String.valueOf(getPartitionSize()));
    query.set(CommonParams.SORT, "id asc");

    // do the query
    SolrClient solrClient = MCRSolrClientFactory.getSolrClient();
    QueryResponse response = solrClient.query(query);
    Collection<MCROAISetResolver<String, SolrDocument>> setResolver = getSetResolver(response.getResults());
    return new MCROAISolrResult(response, d -> toHeader(d, setResolver));
}
 
Example 16
Project: mycore   File: MCROAIClassificationSetResolver.java   View source code 6 votes vote down vote up
@Override
public Collection<Set> getSets(String key) {
    SolrDocument solrDocument = setMap.get(key);
    if (solrDocument == null) {
        throw new MCRException("Unknown key: " + key);
    }
    return Optional.ofNullable(solrDocument.getFieldValues(classField))
        .orElseGet(Collections::emptySet)
        .stream()
        .map(String.class::cast)
        .filter(s -> s.startsWith(classPrefix))
        .map(s -> s.substring(classPrefix.length()))
        .map(s -> getSetId() + ":" + s)
        .map(getSetMap()::get)
        .filter(Objects::nonNull)
        .collect(Collectors.toSet());

}
 
Example 17
Project: Camel   File: SolrUpdateTest.java   View source code 6 votes vote down vote up
@Test
@Ignore("No real advantage has yet been discovered to specifying the file in a header.")
public void indexPDFDocumentSpecifyingFileInParameters() throws Exception {
    solrEndpoint.setRequestHandler("/update/extract");

    Exchange exchange = createExchangeWithBody(null);
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader("SolrParam.stream.file", "src/test/resources/data/tutorial.pdf");
    exchange.getIn().setHeader("SolrParam.literal.id", "tutorial.pdf");

    template.send("direct:start", exchange);
    solrCommit();

    QueryResponse response = executeSolrQuery("*:*");
    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());

    SolrDocument doc = response.getResults().get(0);
    assertEquals("Solr", doc.getFieldValue("subject"));
    assertEquals("tutorial.pdf", doc.getFieldValue("id"));
    assertEquals(Arrays.asList("application/pdf"), doc.getFieldValue("content_type"));
}
 
Example 18
Project: Camel   File: SolrUpdateTest.java   View source code 6 votes vote down vote up
@Test
public void setMultiValuedFieldInHeader() throws Exception {
    String[] categories = {"electronics", "apple"};
    Exchange exchange = createExchangeWithBody("Test body for iPod.");
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader("SolrField.id", "MA147LL/A");
    exchange.getIn().setHeader("SolrField.cat", categories);

    template.send("direct:start", exchange);
    solrCommit();

    // Check things were indexed.
    QueryResponse response = executeSolrQuery("id:MA147LL/A");

    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());

    SolrDocument doc = response.getResults().get(0);
    assertArrayEquals(categories, ((List<?>) doc.getFieldValue("cat")).toArray());
}
 
Example 19
Project: Camel   File: SolrUpdateTest.java   View source code 6 votes vote down vote up
@Test
public void setHeadersAsSolrFields() throws Exception {
    Exchange exchange = createExchangeWithBody("Body is ignored");
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader("SolrField.id", "MA147LL/A");
    exchange.getIn().setHeader("SolrField.name", "Apple 60 GB iPod with Video Playback Black");
    exchange.getIn().setHeader("SolrField.manu", "Apple Computer Inc.");

    template.send("direct:start", exchange);
    solrCommit();

    QueryResponse response = executeSolrQuery("id:MA147LL/A");

    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());

    SolrDocument doc = response.getResults().get(0);
    assertEquals("Apple 60 GB iPod with Video Playback Black", doc.getFieldValue("name"));
    assertEquals("Apple Computer Inc.", doc.getFieldValue("manu"));
}
 
Example 20
Project: tripod   File: SolrDocumentTransformer.java   View source code 6 votes vote down vote up
/**
 * Safely gets a String for the given field.
 *
 * @param solrDocument the docoument to get the field from
 * @param field the field to get
 * @return the String value of the field
 */
default String getString(SolrDocument solrDocument, String field) {
    String returnVal = null;
    final Object object = solrDocument.getFieldValue(field);
    if (object != null) {
        if (object instanceof String) {
            returnVal = (String) object;
        } else if (object instanceof ArrayList) {
            Collection<Object> objects = solrDocument.getFieldValues(field);
            if (objects.size() > 0) {
                returnVal = (String) objects.iterator().next();
            }
        } else {
            returnVal = object.toString();
        }
    }
    return returnVal;
}
 
Example 21
Project: gora-boot   File: SolrStore.java   View source code 6 votes vote down vote up
@Override
public T get(K key, String[] fields) {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set(CommonParams.QT, "/get");
  params.set(CommonParams.FL, toDelimitedString(fields, ","));
  params.set("id", key.toString());
  try {
    QueryResponse rsp = server.query(params);
    Object o = rsp.getResponse().get("doc");
    if (o == null) {
      return null;
    }
    return newInstance((SolrDocument) o, fields);
  } catch (Exception e) {
    LOG.error(e.getMessage(), e);
  }
  return null;
}
 
Example 22
Project: vlo-curation   File: SolrDaoImpl.java   View source code 6 votes vote down vote up
public SolrDocument getSolrDocument(String docId) {
    if (docId == null) {
        throw new NullPointerException("Cannot get SOLR document for null docId");
    }
    SolrDocument result = null;
    SolrQuery query = new SolrQuery();
    query.setQuery("id:" + ClientUtils.escapeQueryChars(docId));
    query.setFields("*");
    SolrDocumentList docs = fireQuery(query).getResults();
    if (docs.getNumFound() > 1) {
        logger.error("Error: found multiple documents for id (will return first one): " + docId + " \nDocuments found: " + docs);
        result = docs.get(0);
    } else if (docs.getNumFound() == 1) {
        result = docs.get(0);
    }
    return result;
}
 
Example 23
Project: beam   File: SolrIOTest.java   View source code 6 votes vote down vote up
@Test
public void testSplit() throws Exception {
  SolrIOTestUtils.insertTestDocuments(SOLR_COLLECTION, NUM_DOCS, solrClient);

  PipelineOptions options = PipelineOptionsFactory.create();
  SolrIO.Read read =
      SolrIO.read().withConnectionConfiguration(connectionConfiguration).from(SOLR_COLLECTION);
  SolrIO.BoundedSolrSource initialSource = new SolrIO.BoundedSolrSource(read, null);
  //desiredBundleSize is ignored for now
  int desiredBundleSizeBytes = 0;
  List<? extends BoundedSource<SolrDocument>> splits =
      initialSource.split(desiredBundleSizeBytes, options);
  SourceTestUtils.assertSourcesEqualReferenceSource(initialSource, splits, options);

  int expectedNumSplits = NUM_SHARDS;
  assertEquals(expectedNumSplits, splits.size());
  int nonEmptySplits = 0;
  for (BoundedSource<SolrDocument> subSource : splits) {
    if (readFromSource(subSource, options).size() > 0) {
      nonEmptySplits += 1;
    }
  }
  // docs are hashed by id to shards, in this test, NUM_DOCS >> NUM_SHARDS
  // therefore, can not exist an empty shard.
  assertEquals("Wrong number of empty splits", expectedNumSplits, nonEmptySplits);
}
 
Example 24
Project: vlo-curation   File: SearchResultItemExpandedPanel.java   View source code 6 votes vote down vote up
public SearchResultItemExpandedPanel(String id, final IModel<SolrDocument> documentModel, final IModel<SearchContext> searchContextModel, Ordering<String> availabilityOrdering) {
    super(id, documentModel);
    this.searchContextModel = searchContextModel;

    // add untruncated description
    final NullFallbackModel descriptionModel = new NullFallbackModel(new SolrFieldStringModel(documentModel, FacetConstants.FIELD_DESCRIPTION), "");
    add(new SmartLinkFieldValueLabel("description", descriptionModel, Model.of(FacetConstants.FIELD_DESCRIPTION)));

    // add link to record
    add(new RecordPageLink("recordLink", documentModel, searchContextModel));

    // table with some basic properties
    add(new FieldsTablePanel("documentProperties", new DocumentFieldsProvider(documentModel, propertiesFilter, fieldOrder)) {

        @Override
        protected boolean isShowFacetSelectLinks() {
            // do not show the value selection links
            return false;
        }

    });

    // add a container for the resources (only visible if there are actual resources)
    add(createResourcesView("resources", searchContextModel));
}
 
Example 25
Project: Camel   File: SolrUpdateTest.java   View source code 6 votes vote down vote up
@Test
public void indexDocumentsToCSVUpdateHandlerWithParameters() throws Exception {
    solrEndpoint.setRequestHandler("/update/csv");

    Exchange exchange = createExchangeWithBody(new File("src/test/resources/data/books.csv"));
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader(SolrConstants.PARAM + UpdateParams.ASSUME_CONTENT_TYPE, "text/csv");
    exchange.getIn().setHeader("SolrParam.fieldnames", "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s");
    exchange.getIn().setHeader("SolrParam.skip", "cat,sequence_i,genre_s");
    exchange.getIn().setHeader("SolrParam.skipLines", 1);

    template.send("direct:start", exchange);
    solrCommit();

    QueryResponse response = executeSolrQuery("*:*");
    assertEquals(0, response.getStatus());
    assertEquals(10, response.getResults().getNumFound());

    SolrDocument doc = response.getResults().get(0);
    assertFalse(doc.getFieldNames().contains("cat"));
}
 
Example 26
Project: vlo-curation   File: ContentSearchFormPanel.java   View source code 6 votes vote down vote up
public ContentSearchFormPanel(String id, final IModel<SolrDocument> documentModel, final IModel<String> endpointModel) {
    super(id);

    // Create a model that returns a JSON representation of the endpoint and document id
    final IModel<String> jsonModel = createJsonModel(documentModel, endpointModel);
    setModel(jsonModel);

    // Populate attributes in form elements...
    // Outer <form> element
    final WebMarkupContainer fcsForm = new WebMarkupContainer("fcsForm");
    // The action of the form should be the aggregator endpoint
    fcsForm.add(new AttributeModifier("action", vloConfig.getFederatedContentSearchUrl()));
    add(fcsForm);

    // Hidden form field for aggregation context
    final WebMarkupContainer aggregationContext = new WebMarkupContainer("aggregationContent");
    // The value should be the JSON object
    aggregationContext.add(new AttributeModifier("value", jsonModel));
    fcsForm.add(aggregationContext);
}
 
Example 27
Project: vlo-curation   File: ContentSearchFormPanel.java   View source code 6 votes vote down vote up
private IModel<String> createJsonModel(final IModel<SolrDocument> model, final IModel<String> endpointModel) {
    // Prepare a JSON object that holds the CQL endpoint and the document self link
    return new AbstractReadOnlyModel<String>() {
        @Override
        public String getObject() {
            final String endPoint = endpointModel.getObject();
            final Object selfLink = model.getObject().getFirstValue(FacetConstants.FIELD_SELF_LINK);
            try {
                final JSONObject json = new JSONObject();
                json.put(endPoint, new JSONArray(new Object[]{selfLink}));
                return json.toString(2);
            } catch (JSONException ex) {
                logger.warn("Could not create JSON for aggregation context with endpoint '{}' and docId '{}'", endPoint, selfLink, ex);
                return null;
            }
        }

        @Override
        public void detach() {
            super.detach();
            endpointModel.detach();
        }

    };
}
 
Example 28
Project: vlo-curation   File: HierarchyPanel.java   View source code 6 votes vote down vote up
public HierarchyPanel(String id, IModel<SolrDocument> documentModel) {
    super(id, documentModel);
    pageDocumentModel = documentModel;

    parentsProvider = new ParentsDataProvider();

    add(createParentLinks("parents"));

    tree = createTree("tree");

    treeContainer = new WebMarkupContainer("treeContainer");
    treeContainer.setOutputMarkupId(true);
    treeContainer.add(tree);
    add(treeContainer);

    setOutputMarkupId(true);
}
 
Example 29
Project: vlo-curation   File: HierarchyPanel.java   View source code 6 votes vote down vote up
@Override
public Iterator<? extends SolrDocument> iterator(long first, long count) {
    final Collection<Object> parentIds = HierarchyPanel.this.getModelObject().getFieldValues(FacetConstants.FIELD_IS_PART_OF);
    if (parentIds == null) {
        // no parents, so provide empty list of documents
        return Collections.emptyIterator();
    } else {
        // parents found, convert ID collection into documents list
        return Iterators.transform(parentIds.iterator(), new Function<Object, SolrDocument>() {

            @Override
            public SolrDocument apply(Object docId) {
                return documentService.getDocument(docId.toString());
            }
        });
    }
}
 
Example 30
Project: vlo-curation   File: SolrDocumentServiceImplTest.java   View source code 6 votes vote down vote up
/**
 * Test of getDocument method, of class SolrDocumentServiceImpl.
 */
@Test
public void testGetDocument() {
    final SolrQuery solrQuery = new SolrQuery("query");
    final SolrDocument resultDocument = new SolrDocument();
    resultDocument.setField("id", "documentId");
    final SolrDocumentList resultList = new SolrDocumentList();
    resultList.add(resultDocument);
    context.checking(new Expectations() {
        {
            oneOf(queryFactory).createDocumentQuery("documentId");
            will(returnValue(solrQuery));
            oneOf(dao).getDocuments(solrQuery);
            will(returnValue(resultList));
        }
    });

    final SolrDocument result = instance.getDocument("documentId");
    assertEquals(resultDocument, result);
    assertEquals("documentId", result.getFieldValue("id"));
}
 
Example 31
Project: vlo-curation   File: SolrDocumentServiceImplTest.java   View source code 6 votes vote down vote up
/**
 * Test of getDocuments method, of class SolrDocumentServiceImpl.
 */
@Test
public void testGetDocuments() {
    final QueryFacetsSelection selection = new QueryFacetsSelection("query", Collections.<String, FacetSelection>emptyMap());
    final int first = 100;
    final int count = 15;

    final SolrQuery solrQuery = new SolrQuery("query");
    final SolrDocumentList resultList = new SolrDocumentList();
    resultList.add(new SolrDocument());
    resultList.add(new SolrDocument());

    context.checking(new Expectations() {
        {
            oneOf(queryFactory).createDocumentQuery(selection, first, count);
            will(returnValue(solrQuery));
            oneOf(dao).getDocuments(solrQuery);
            will(returnValue(resultList));
        }
    });

    final List<SolrDocument> result = instance.getDocuments(selection, first, count);
    assertEquals(resultList, result);
}
 
Example 32
Project: goobi-viewer-connector   File: SruServlet.java   View source code 5 votes vote down vote up
/**
 * @param root
 * @param solrDocuments
 * @param parameter
 */
private static void generateEchoedSearchRetrieveRequest(Element root, List<SolrDocument> solrDocuments, SruRequestParameter parameter) {
    Element echoedSearchRetrieveRequest = new Element("echoedSearchRetrieveRequest", SRU_NAMESPACE);
    root.addContent(echoedSearchRetrieveRequest);

    Element version = new Element("version", SRU_NAMESPACE);
    version.setText(parameter.getVersion());
    echoedSearchRetrieveRequest.addContent(version);

    Element recordSchema = new Element("recordSchema", SRU_NAMESPACE);
    recordSchema.setText(parameter.getRecordSchema().getMetadataPrefix());
    echoedSearchRetrieveRequest.addContent(recordSchema);

    Element query = new Element("query", SRU_NAMESPACE);
    query.setText(parameter.getQuery());
    echoedSearchRetrieveRequest.addContent(query);

    Element recordPacking = new Element("recordPacking", SRU_NAMESPACE);
    recordPacking.setText(parameter.getRecordPacking());
    echoedSearchRetrieveRequest.addContent(recordPacking);

    Element startRecord = new Element("startRecord", SRU_NAMESPACE);
    startRecord.setText(String.valueOf(parameter.getStartRecord()));
    echoedSearchRetrieveRequest.addContent(startRecord);

    Element maximumRecords = new Element("maximumRecords", SRU_NAMESPACE);
    maximumRecords.setText(String.valueOf(parameter.getMaximumRecords()));
    echoedSearchRetrieveRequest.addContent(maximumRecords);

}
 
Example 33
Project: goobi-viewer-connector   File: SruServlet.java   View source code 5 votes vote down vote up
private static String getAnchorTitle(SolrDocument doc, SolrSearchIndex solr) throws SolrServerException {
    String iddocParent = (String) doc.getFieldValue(SolrConstants.IDDOC_PARENT);
    SolrDocumentList hits = solr.search(SolrConstants.IDDOC + ":" + iddocParent);
    if (hits != null && !hits.isEmpty()) {
        return (String) hits.get(0).getFirstValue("MD_TITLE");
    }

    return null;
}
 
Example 34
Project: goobi-viewer-connector   File: SolrSearchIndex.java   View source code 5 votes vote down vote up
public String getEarliestRecordDatestamp() throws SolrServerException {
    try {
        String searchStr = SolrConstants.ISWORK + ":true" + getAllSuffixes();

        SolrQuery solrQuery = new SolrQuery(searchStr);
        solrQuery.setRows(1);
        solrQuery.addField(SolrConstants.DATECREATED);
        solrQuery.addSort(SolrConstants.DATECREATED, ORDER.asc);
        QueryResponse resp = server.query(solrQuery);

        if (resp.getResults().size() > 0) {
            SolrDocument doc = resp.getResults().get(0);
            if (doc.getFieldValue(SolrConstants.DATECREATED) != null) {
                Date d = new Date((Long) doc.getFieldValue(SolrConstants.DATECREATED));
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// ;YYYY-MM-DDThh:mm:ssZ
                SimpleDateFormat hours = new SimpleDateFormat("HH:mm:ss");
                format.setTimeZone(TimeZone.getTimeZone("GMT"));
                hours.setTimeZone(TimeZone.getTimeZone("GMT"));
                String yearMonthDay = format.format(d);
                String hourMinuteSeconde = hours.format(d);

                return yearMonthDay + "T" + hourMinuteSeconde + "Z";
            }
        }
    } catch (NullPointerException e) {
        logger.error(e.getMessage(), e);
    }
    return "";
}
 
Example 35
Project: goobi-viewer-connector   File: XMLGeneration.java   View source code 5 votes vote down vote up
/**
 * 
 * @param doc
 * @return
 */
private String getAnchorTitle(SolrDocument doc) {
    String iddocParent = (String) doc.getFieldValue(SolrConstants.IDDOC_PARENT);
    try {
        SolrDocumentList hits = solr.search(SolrConstants.IDDOC + ":" + iddocParent);
        if (hits != null && !hits.isEmpty()) {
            return (String) hits.get(0).getFirstValue(SolrConstants.TITLE);
        }
    } catch (SolrServerException e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}
 
Example 36
Project: goobi-viewer-connector   File: XMLGeneration.java   View source code 5 votes vote down vote up
/**
 * generates header for epicur format
 * 
 * @param doc
 * @param dateUpdated
 * @return
 */
private static Element generateEpicurHeader(SolrDocument doc, long dateUpdated) {
    Namespace xmlns = DataManager.getInstance().getConfiguration().getStandardNameSpace();
    Element header = new Element("header", xmlns);
    Element identifier = new Element("identifier", xmlns);
    identifier.setText(DataManager.getInstance().getConfiguration().getOaiIdentifier().get("repositoryIdentifier") + (String) doc.getFieldValue(
            "URN"));
    header.addContent(identifier);

    Element datestamp = new Element("datestamp", xmlns);
    datestamp.setText(parseDate(dateUpdated));
    header.addContent(datestamp);
    // setSpec
    List<String> setSpecFields = DataManager.getInstance().getConfiguration().getSetSpecFieldsForMetadataFormat(Metadata.epicur.name());
    if (!setSpecFields.isEmpty()) {
        for (String setSpecField : setSpecFields) {
            if (doc.containsKey(setSpecField)) {
                for (Object fieldValue : doc.getFieldValues(setSpecField)) {
                    // TODO translation
                    Element setSpec = new Element("setSpec", xmlns);
                    setSpec.setText((String) fieldValue);
                    header.addContent(setSpec);
                }
            }
        }
    }
    // status="deleted"
    if (doc.getFieldValues(SolrConstants.DATEDELETED) != null) {
        header.setAttribute("status", "deleted");
    }

    return header;
}
 
Example 37
Project: goobi-viewer-connector   File: XMLGeneration.java   View source code 5 votes vote down vote up
/**
 * 
 * @param doc
 * @param urn
 * @param dateUpdated
 * @return
 */
private static Element generateEpicurPageHeader(SolrDocument doc, String urn, long dateUpdated) {
    Namespace xmlns = DataManager.getInstance().getConfiguration().getStandardNameSpace();
    Element header = new Element("header", xmlns);

    Element identifier = new Element("identifier", xmlns);
    identifier.setText(DataManager.getInstance().getConfiguration().getOaiIdentifier().get("repositoryIdentifier") + urn);
    header.addContent(identifier);

    Element datestamp = new Element("datestamp", xmlns);
    datestamp.setText(parseDate(dateUpdated));
    header.addContent(datestamp);
    // setSpec
    List<String> setSpecFields = DataManager.getInstance().getConfiguration().getSetSpecFieldsForMetadataFormat(Metadata.epicur.name());
    if (!setSpecFields.isEmpty()) {
        for (String setSpecField : setSpecFields) {
            if (doc.containsKey(setSpecField)) {
                for (Object fieldValue : doc.getFieldValues(setSpecField)) {
                    // TODO translation
                    Element setSpec = new Element("setSpec", xmlns);
                    setSpec.setText((String) fieldValue);
                    header.addContent(setSpec);
                }
            }
        }
    }
    // status="deleted"
    if (doc.getFieldValues(SolrConstants.DATEDELETED) != null) {
        header.setAttribute("status", "deleted");
    }

    return header;
}
 
Example 38
Project: goobi-viewer-connector   File: SolrSearchIndexTest.java   View source code 5 votes vote down vote up
/**
 * @see SolrSearchIndex#getLatestValidDateUpdated(SolrDocument,long)
 * @verifies return correct value
 */
@Test
public void getLatestValidDateUpdated_shouldReturnCorrectValue() throws Exception {
    SolrDocument doc = new SolrDocument();
    doc.addField(SolrConstants.DATEUPDATED, 1L);
    doc.addField(SolrConstants.DATEUPDATED, 2L);
    doc.addField(SolrConstants.DATEUPDATED, 3L);
    doc.addField(SolrConstants.DATEUPDATED, 4L);
    Assert.assertEquals(Long.valueOf(3), SolrSearchIndex.getLatestValidDateUpdated(doc, 3));
}
 
Example 39
Project: goobi-viewer-connector   File: SolrSearchIndexTest.java   View source code 5 votes vote down vote up
/**
 * @see SolrSearchIndex#getFirstDoc(String,List)
 * @verifies return correct doc
 */
@Test
public void getFirstDoc_shouldReturnCorrectDoc() throws Exception {
    SolrDocument doc = DataManager.getInstance().getSearchIndex().getFirstDoc(SolrConstants.PI + ":PPN517154005", Collections.singletonList(
            SolrConstants.PI));
    Assert.assertNotNull(doc);
    Assert.assertEquals("PPN517154005", doc.getFieldValue(SolrConstants.PI));
}
 
Example 40
Project: goobi-viewer-connector   File: SolrSearchIndexTest.java   View source code 5 votes vote down vote up
/**
 * @see SolrSearchIndex#search(String,int,int,List,List,Map)
 * @verifies sort results correctly
 */
@Test
public void search_shouldSortResultsCorrectly() throws Exception {
    SolrDocumentList result = DataManager.getInstance().getSearchIndex().search(null, null, null, Metadata.oai_dc.name(), 0, 10, false, null);
    Assert.assertFalse(result.isEmpty());
    long previous = 0;
    for (SolrDocument doc : result) {
        Long dateCreated = (long) doc.getFieldValue(SolrConstants.DATECREATED);
        Assert.assertNotNull(dateCreated);
        Assert.assertTrue(dateCreated > previous);
        previous = dateCreated;
    }
}