Skip to content

Commit f95c1a0

Browse files
authored
Merge pull request #10598 from Recherche-Data-Gouv/8941-adding-fileCount-in-solr
8941 adding file count in solr (v2)
2 parents b8e4758 + aa3f855 commit f95c1a0

File tree

8 files changed

+42
-14
lines changed

8 files changed

+42
-14
lines changed

conf/solr/schema.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
<field name="datasetValid" type="boolean" stored="true" indexed="true" multiValued="false"/>
235235

236236
<field name="license" type="string" stored="true" indexed="true" multiValued="false"/>
237+
<field name="fileCount" type="plong" stored="true" indexed="true" multiValued="false"/>
237238

238239
<!--
239240
METADATA SCHEMA FIELDS
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Release Highlights
2+
3+
### Adding fileCount as SOLR field
4+
5+
A new search field called `fileCount` can be searched to discover the number of files per dataset. (#10598)
6+
7+
## Upgrade Instructions
8+
9+
1. Update your Solr `schema.xml` to include the new field.
10+
For details, please see https://guides.dataverse.org/en/latest/admin/metadatacustomization.html#updating-the-solr-schema
11+
12+
2. Reindex Solr.
13+
Once the schema.xml is updated, Solr must be restarted and a reindex initiated.
14+
For details, see https://guides.dataverse.org/en/latest/admin/solr-search-index.html but here is the reindex command:
15+
`curl http://localhost:8080/api/admin/index`

src/main/java/edu/harvard/iq/dataverse/api/Search.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public Response search(
175175
JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
176176
List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
177177
for (SolrSearchResult solrSearchResult : solrSearchResults) {
178-
itemsArrayBuilder.add(solrSearchResult.json(showRelevance, showEntityIds, showApiUrls, metadataFields, getDatasetFileCount(solrSearchResult)));
178+
itemsArrayBuilder.add(solrSearchResult.json(showRelevance, showEntityIds, showApiUrls, metadataFields));
179179
}
180180

181181
JsonObjectBuilder spelling_alternatives = Json.createObjectBuilder();
@@ -229,15 +229,6 @@ public Response search(
229229
}
230230
}
231231

232-
private Long getDatasetFileCount(SolrSearchResult solrSearchResult) {
233-
DvObject dvObject = solrSearchResult.getEntity();
234-
if (dvObject.isInstanceofDataset()) {
235-
DatasetVersion datasetVersion = ((Dataset) dvObject).getVersionFromId(solrSearchResult.getDatasetVersionId());
236-
return datasetVersionFilesServiceBean.getFileMetadataCount(datasetVersion);
237-
}
238-
return null;
239-
}
240-
241232
private User getUser(ContainerRequestContext crc) throws WrappedResponse {
242233
User userToExecuteSearchAs = GuestUser.get();
243234
try {

src/main/java/edu/harvard/iq/dataverse/search/IndexServiceBean.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public class IndexServiceBean {
135135
@EJB
136136
DatasetFieldServiceBean datasetFieldService;
137137

138+
@Inject
139+
DatasetVersionFilesServiceBean datasetVersionFilesServiceBean;
140+
138141
public static final String solrDocIdentifierDataverse = "dataverse_";
139142
public static final String solrDocIdentifierFile = "datafile_";
140143
public static final String solrDocIdentifierDataset = "dataset_";
@@ -1018,6 +1021,8 @@ public SolrInputDocuments toSolrDocs(IndexableDataset indexableDataset, Set<Long
10181021
solrInputDocument.addField(SearchFields.DATASET_CITATION, datasetVersion.getCitation(false));
10191022
solrInputDocument.addField(SearchFields.DATASET_CITATION_HTML, datasetVersion.getCitation(true));
10201023

1024+
solrInputDocument.addField(SearchFields.FILE_COUNT, datasetVersionFilesServiceBean.getFileMetadataCount(datasetVersion));
1025+
10211026
if (datasetVersion.isInReview()) {
10221027
solrInputDocument.addField(SearchFields.PUBLICATION_STATUS, IN_REVIEW_STRING);
10231028
}

src/main/java/edu/harvard/iq/dataverse/search/SearchFields.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,5 +291,6 @@ more targeted results for just datasets. The format is YYYY (i.e.
291291
public static final String DATASET_VALID = "datasetValid";
292292

293293
public static final String DATASET_LICENSE = "license";
294+
public static final String FILE_COUNT = "fileCount";
294295

295296
}

src/main/java/edu/harvard/iq/dataverse/search/SearchServiceBean.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ public SolrQueryResponse search(
497497
Long retentionEndDate = (Long) solrDocument.getFieldValue(SearchFields.RETENTION_END_DATE);
498498
//
499499
Boolean datasetValid = (Boolean) solrDocument.getFieldValue(SearchFields.DATASET_VALID);
500-
500+
Long fileCount = (Long) solrDocument.getFieldValue(SearchFields.FILE_COUNT);
501+
501502
List<String> matchedFields = new ArrayList<>();
502503

503504
SolrSearchResult solrSearchResult = new SolrSearchResult(query, name);
@@ -570,6 +571,7 @@ public SolrQueryResponse search(
570571
solrSearchResult.setDeaccessionReason(deaccessionReason);
571572
solrSearchResult.setDvTree(dvTree);
572573
solrSearchResult.setDatasetValid(datasetValid);
574+
solrSearchResult.setFileCount(fileCount);
573575

574576
if (Boolean.TRUE.equals((Boolean) solrDocument.getFieldValue(SearchFields.IS_HARVESTED))) {
575577
solrSearchResult.setHarvested(true);

src/main/java/edu/harvard/iq/dataverse/search/SolrSearchResult.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public class SolrSearchResult {
7878
private String citation;
7979
private String citationHtml;
8080
private String datasetType;
81+
/**
82+
* Only Dataset can have a file count
83+
*/
84+
private Long fileCount;
8185
/**
8286
* Files and datasets might have a UNF. Dataverses don't.
8387
*/
@@ -456,10 +460,10 @@ public JsonObjectBuilder getJsonForMyData(boolean isValid) {
456460
} // getJsonForMydata
457461

458462
public JsonObjectBuilder json(boolean showRelevance, boolean showEntityIds, boolean showApiUrls) {
459-
return json(showRelevance, showEntityIds, showApiUrls, null, null);
463+
return json(showRelevance, showEntityIds, showApiUrls, null);
460464
}
461465

462-
public JsonObjectBuilder json(boolean showRelevance, boolean showEntityIds, boolean showApiUrls, List<String> metadataFields, Long datasetFileCount) {
466+
public JsonObjectBuilder json(boolean showRelevance, boolean showEntityIds, boolean showApiUrls, List<String> metadataFields) {
463467
if (this.type == null) {
464468
return jsonObjectBuilder();
465469
}
@@ -597,7 +601,7 @@ public JsonObjectBuilder json(boolean showRelevance, boolean showEntityIds, bool
597601
subjects.add(subject);
598602
}
599603
nullSafeJsonBuilder.add("subjects", subjects);
600-
nullSafeJsonBuilder.add("fileCount", datasetFileCount);
604+
nullSafeJsonBuilder.add("fileCount", this.fileCount);
601605
nullSafeJsonBuilder.add("versionId", dv.getId());
602606
nullSafeJsonBuilder.add("versionState", dv.getVersionState().toString());
603607
if (this.isPublishedState()) {
@@ -1348,4 +1352,12 @@ public boolean isValid(Predicate<SolrSearchResult> canUpdateDataset) {
13481352
}
13491353
return !canUpdateDataset.test(this);
13501354
}
1355+
1356+
public Long getFileCount() {
1357+
return fileCount;
1358+
}
1359+
1360+
public void setFileCount(Long fileCount) {
1361+
this.fileCount = fileCount;
1362+
}
13511363
}

src/test/java/edu/harvard/iq/dataverse/search/IndexServiceBeanTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void setUp() {
5353
indexService.dataverseService = Mockito.mock(DataverseServiceBean.class);
5454
indexService.datasetFieldService = Mockito.mock(DatasetFieldServiceBean.class);
5555
indexService.datasetVersionService = Mockito.mock(DatasetVersionServiceBean.class);
56+
indexService.datasetVersionFilesServiceBean = Mockito.mock(DatasetVersionFilesServiceBean.class);
5657
BrandingUtil.injectServices(indexService.dataverseService, indexService.settingsService);
5758

5859
Mockito.when(indexService.dataverseService.findRootDataverse()).thenReturn(dataverse);

0 commit comments

Comments
 (0)