use of edu.harvard.iq.dataverse.search.SolrSearchResult in project dataverse by IQSS.
the class RoleTagRetriever method loadInfoFromSolrResponseDocs.
/**
* Iterate through the Solr Cards and collect
* - DvObject Id + Parent ID
* - Dtype for object and parent
* - Whether a "grandparent id" is needed for a file object
*
* @param solrQueryResponse
*/
private void loadInfoFromSolrResponseDocs(SolrQueryResponse solrQueryResponse) {
if (solrQueryResponse == null) {
throw new NullPointerException("RoleTagRetriever.constructor. solrQueryResponse cannot be null");
}
// ----------------------------------
// Load initial data
// ----------------------------------
msgt("load initial data");
//
for (SolrSearchResult doc : solrQueryResponse.getSolrSearchResults()) {
// -------------------------------------------------
// (a) retrieve Card Id and DvObject type
// -------------------------------------------------
finalCardIds.add(doc.getEntityId());
String dtype = doc.getType();
Long entityId = doc.getEntityId();
if (dtype == null) {
throw new NullPointerException("The dvobject type cannot be null for SolrSearchResult");
}
logger.fine("\nid: " + doc.getEntityId() + " dtype: " + dtype);
// -------------------------------------------------
// (b) Populate dict of { dvObject id : dtype }
// e.g. { 3 : 'Dataverse' }
// -------------------------------------------------
this.idToDvObjectType.put(entityId, dtype);
// -------------------------------------------------
// (c) initialize dict of { dvObject id : [ (empty list for role ids) ] }
// -------------------------------------------------
addIdNeedingRoleRetrieval(entityId);
Long parentId = doc.getParentIdAsLong();
// -------------------------------------------------
if (!(dtype.equals(SearchConstants.SOLR_DATAVERSES))) {
// -------------------------------------------------
if (parentId == null) {
throw new NullPointerException("A dataset or file parent cannot be null for SolrSearchResult");
}
logger.fine("\nparentId: " + parentId);
this.childToParentIdHash.put(doc.getEntityId(), parentId);
// -------------------------------------------------
// (e) For the parent, add to dict of
// { dvObject id : [ (empty list for role ids) ] }
// - similar to (c) above
// -------------------------------------------------
addIdNeedingRoleRetrieval(parentId);
// -------------------------------------------------
if (doc.getType().equals(SearchConstants.SOLR_FILES)) {
logger.fine("It's a file");
// -------------------------------------------------
// (f1) This is a file, we know the parent is a Dataset
// -------------------------------------------------
this.idToDvObjectType.put(parentId, SearchConstants.SOLR_DATASETS);
// -------------------------------------------------
// (g) For files, we'll need to get roles from the grandparent--e.g., the dataverse
// -------------------------------------------------
this.datasetIdsNeedingParentIds.add(parentId);
}
if (dtype.equals(SearchConstants.SOLR_DATASETS)) {
logger.fine("It's a dataset");
// -------------------------------------------------
// (f2) This is a Dataset, we know the parent is a Dataverse
// -------------------------------------------------
this.idToDvObjectType.put(parentId, SearchConstants.SOLR_DATAVERSES);
}
}
// -------------------------------------------------
// initialize final hash of dvObject id and empty list of role names
// { dvObject id : [ (empty list for role nams) ] }
// -------------------------------------------------
this.finalIdToRolesHash.put(doc.getEntityId(), new ArrayList<>());
}
}
use of edu.harvard.iq.dataverse.search.SolrSearchResult in project dataverse by IQSS.
the class Index method searchDebug.
/**
* This method is for integration tests of search.
*/
@GET
@Path("test")
public Response searchDebug(@QueryParam("key") String apiToken, @QueryParam("q") String query, @QueryParam("fq") final List<String> filterQueries) {
User user = findUserByApiToken(apiToken);
if (user == null) {
return error(Response.Status.UNAUTHORIZED, "Invalid apikey '" + apiToken + "'");
}
Dataverse subtreeScope = dataverseService.findRootDataverse();
String sortField = SearchFields.ID;
String sortOrder = SortBy.ASCENDING;
int paginationStart = 0;
boolean dataRelatedToMe = false;
int numResultsPerPage = Integer.MAX_VALUE;
SolrQueryResponse solrQueryResponse;
try {
solrQueryResponse = searchService.search(createDataverseRequest(user), subtreeScope, query, filterQueries, sortField, sortOrder, paginationStart, dataRelatedToMe, numResultsPerPage);
} catch (SearchException ex) {
return error(Response.Status.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage() + ": " + ex.getCause().getLocalizedMessage());
}
JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
for (SolrSearchResult solrSearchResult : solrSearchResults) {
itemsArrayBuilder.add(solrSearchResult.getType() + ":" + solrSearchResult.getNameSort());
}
return ok(itemsArrayBuilder);
}
use of edu.harvard.iq.dataverse.search.SolrSearchResult in project dataverse by IQSS.
the class Index method filesearch.
@GET
@Path("filesearch")
public Response filesearch(@QueryParam("persistentId") String persistentId, @QueryParam("semanticVersion") String semanticVersion, @QueryParam("q") String userSuppliedQuery) {
Dataset dataset = datasetService.findByGlobalId(persistentId);
if (dataset == null) {
return error(Status.BAD_REQUEST, "Could not find dataset with persistent id " + persistentId);
}
User user = GuestUser.get();
try {
AuthenticatedUser authenticatedUser = findAuthenticatedUserOrDie();
if (authenticatedUser != null) {
user = authenticatedUser;
}
} catch (WrappedResponse ex) {
}
RetrieveDatasetVersionResponse datasetVersionResponse = datasetVersionService.retrieveDatasetVersionByPersistentId(persistentId, semanticVersion);
if (datasetVersionResponse == null) {
return error(Status.BAD_REQUEST, "Problem searching for files. Could not find dataset version based on " + persistentId + " and " + semanticVersion);
}
DatasetVersion datasetVersion = datasetVersionResponse.getDatasetVersion();
FileView fileView = searchFilesService.getFileView(datasetVersion, user, userSuppliedQuery);
if (fileView == null) {
return error(Status.BAD_REQUEST, "Problem searching for files. Null returned from getFileView.");
}
JsonArrayBuilder filesFound = Json.createArrayBuilder();
JsonArrayBuilder cards = Json.createArrayBuilder();
JsonArrayBuilder fileIds = Json.createArrayBuilder();
for (SolrSearchResult result : fileView.getSolrSearchResults()) {
cards.add(result.getNameSort());
fileIds.add(result.getEntityId());
JsonObjectBuilder fileFound = Json.createObjectBuilder();
fileFound.add("name", result.getNameSort());
fileFound.add("entityId", result.getEntityId().toString());
fileFound.add("datasetVersionId", result.getDatasetVersionId());
fileFound.add("datasetId", result.getParent().get(SearchFields.ID));
filesFound.add(fileFound);
}
JsonArrayBuilder facets = Json.createArrayBuilder();
for (FacetCategory facetCategory : fileView.getFacetCategoryList()) {
facets.add(facetCategory.getFriendlyName());
}
JsonArrayBuilder filterQueries = Json.createArrayBuilder();
for (String filterQuery : fileView.getFilterQueries()) {
filterQueries.add(filterQuery);
}
JsonArrayBuilder allDatasetVersionIds = Json.createArrayBuilder();
for (DatasetVersion dsVersion : dataset.getVersions()) {
allDatasetVersionIds.add(dsVersion.getId());
}
JsonObjectBuilder data = Json.createObjectBuilder();
data.add("filesFound", filesFound);
data.add("cards", cards);
data.add("fileIds", fileIds);
data.add("facets", facets);
data.add("user", user.getIdentifier());
data.add("persistentID", persistentId);
data.add("query", fileView.getQuery());
data.add("filterQueries", filterQueries);
data.add("allDataverVersionIds", allDatasetVersionIds);
data.add("semanticVersion", datasetVersion.getSemanticVersion());
return ok(data);
}
use of edu.harvard.iq.dataverse.search.SolrSearchResult in project dataverse by IQSS.
the class Search method search.
@GET
public Response search(@QueryParam("q") String query, @QueryParam("type") final List<String> types, @QueryParam("subtree") String subtreeRequested, @QueryParam("sort") String sortField, @QueryParam("order") String sortOrder, @QueryParam("per_page") final int numResultsPerPageRequested, @QueryParam("start") final int paginationStart, @QueryParam("show_relevance") boolean showRelevance, @QueryParam("show_facets") boolean showFacets, @QueryParam("fq") final List<String> filterQueries, @QueryParam("show_entity_ids") boolean showEntityIds, @QueryParam("show_api_urls") boolean showApiUrls, @QueryParam("show_my_data") boolean showMyData, @Context HttpServletResponse response) {
User user;
try {
user = getUser();
} catch (WrappedResponse ex) {
return ex.getResponse();
}
if (query != null) {
// sanity checking on user-supplied arguments
SortBy sortBy;
int numResultsPerPage;
Dataverse subtree;
try {
if (!types.isEmpty()) {
filterQueries.add(getFilterQueryFromTypes(types));
}
sortBy = SearchUtil.getSortBy(sortField, sortOrder);
numResultsPerPage = getNumberOfResultsPerPage(numResultsPerPageRequested);
subtree = getSubtree(subtreeRequested);
if (!subtree.equals(dataverseService.findRootDataverse())) {
String dataversePath = dataverseService.determineDataversePath(subtree);
String filterDownToSubtree = SearchFields.SUBTREE + ":\"" + dataversePath + "\"";
/**
* @todo Should filterDownToSubtree logic be centralized in
* SearchServiceBean?
*/
filterQueries.add(filterDownToSubtree);
}
} catch (Exception ex) {
return error(Response.Status.BAD_REQUEST, ex.getLocalizedMessage());
}
// users can't change these (yet anyway)
// getDataRelatedToMe();
boolean dataRelatedToMe = showMyData;
SolrQueryResponse solrQueryResponse;
try {
solrQueryResponse = searchService.search(createDataverseRequest(user), subtree, query, filterQueries, sortBy.getField(), sortBy.getOrder(), paginationStart, dataRelatedToMe, numResultsPerPage);
} catch (SearchException ex) {
Throwable cause = ex;
StringBuilder sb = new StringBuilder();
sb.append(cause + " ");
while (cause.getCause() != null) {
cause = cause.getCause();
sb.append(cause.getClass().getCanonicalName() + " ");
sb.append(cause + " ");
// if you search for a colon you see RemoteSolrException: org.apache.solr.search.SyntaxError: Cannot parse ':'
}
String message = "Exception running search for [" + query + "] with filterQueries " + filterQueries + " and paginationStart [" + paginationStart + "]: " + sb.toString();
logger.info(message);
return error(Response.Status.INTERNAL_SERVER_ERROR, message);
}
JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
for (SolrSearchResult solrSearchResult : solrSearchResults) {
itemsArrayBuilder.add(solrSearchResult.toJsonObject(showRelevance, showEntityIds, showApiUrls));
}
JsonObjectBuilder spelling_alternatives = Json.createObjectBuilder();
for (Map.Entry<String, List<String>> entry : solrQueryResponse.getSpellingSuggestionsByToken().entrySet()) {
spelling_alternatives.add(entry.getKey(), entry.getValue().toString());
}
JsonArrayBuilder facets = Json.createArrayBuilder();
JsonObjectBuilder facetCategoryBuilder = Json.createObjectBuilder();
for (FacetCategory facetCategory : solrQueryResponse.getFacetCategoryList()) {
JsonObjectBuilder facetCategoryBuilderFriendlyPlusData = Json.createObjectBuilder();
JsonArrayBuilder facetLabelBuilderData = Json.createArrayBuilder();
for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
JsonObjectBuilder countBuilder = Json.createObjectBuilder();
countBuilder.add(facetLabel.getName(), facetLabel.getCount());
facetLabelBuilderData.add(countBuilder);
}
facetCategoryBuilderFriendlyPlusData.add("friendly", facetCategory.getFriendlyName());
facetCategoryBuilderFriendlyPlusData.add("labels", facetLabelBuilderData);
facetCategoryBuilder.add(facetCategory.getName(), facetCategoryBuilderFriendlyPlusData);
}
facets.add(facetCategoryBuilder);
JsonObjectBuilder value = Json.createObjectBuilder().add("q", query).add("total_count", solrQueryResponse.getNumResultsFound()).add("start", solrQueryResponse.getResultsStart()).add("spelling_alternatives", spelling_alternatives).add("items", itemsArrayBuilder.build());
if (showFacets) {
value.add("facets", facets);
}
value.add("count_in_response", solrSearchResults.size());
// value.add("fq_provided", filterQueries.toString());
if (solrQueryResponse.getError() != null) {
/**
* @todo You get here if you pass only ":" as a query, for
* example. Should we return more or better information?
*/
return error(Response.Status.BAD_REQUEST, solrQueryResponse.getError());
}
response.setHeader("Access-Control-Allow-Origin", "*");
return allowCors(ok(value));
} else {
return allowCors(error(Response.Status.BAD_REQUEST, "q parameter is missing"));
}
}
use of edu.harvard.iq.dataverse.search.SolrSearchResult in project dataverse by IQSS.
the class DataRetrieverAPI method formatSolrDocs.
/**
* Using RoleTagRetriever to find role names for each card
* Trying to minimize extra queries
*
* @param solrResponse
* @param roleTagRetriever
* @return
*/
private JsonArrayBuilder formatSolrDocs(SolrQueryResponse solrResponse, RoleTagRetriever roleTagRetriever) {
if (solrResponse == null) {
throw new NullPointerException("DataRetrieverAPI.formatSolrDocs: solrResponse should not be null");
}
if (roleTagRetriever == null) {
throw new NullPointerException("DataRetrieverAPI.formatSolrDocs: roleTagRetriever should not be null");
}
JsonArrayBuilder jsonSolrDocsArrayBuilder = Json.createArrayBuilder();
JsonObjectBuilder myDataCardInfo;
JsonArrayBuilder rolesForCard;
for (SolrSearchResult doc : solrQueryResponse.getSolrSearchResults()) {
// -------------------------------------------
// (a) Get core card data from solr
// -------------------------------------------
myDataCardInfo = doc.getJsonForMyData();
if (!doc.getEntity().isInstanceofDataFile()) {
String parentAlias = dataverseService.getParentAliasString(doc);
System.out.print("parentAlias: " + parentAlias);
myDataCardInfo.add("parent_alias", parentAlias);
}
// -------------------------------------------
// (b) Add role info
// -------------------------------------------
rolesForCard = roleTagRetriever.getRolesForCardAsJSON(doc.getEntityId());
if (rolesForCard != null) {
myDataCardInfo.add("user_roles", rolesForCard);
}
// -------------------------------------------
// (c) Add final MyData JSON to array
// -------------------------------------------
jsonSolrDocsArrayBuilder.add(myDataCardInfo);
}
return jsonSolrDocsArrayBuilder;
}
Aggregations