diff --git a/dspace-api/src/main/java/org/dspace/browse/BrowseDAO.java b/dspace-api/src/main/java/org/dspace/browse/BrowseDAO.java index 03130e39e78b..26983303a152 100644 --- a/dspace-api/src/main/java/org/dspace/browse/BrowseDAO.java +++ b/dspace-api/src/main/java/org/dspace/browse/BrowseDAO.java @@ -396,4 +396,6 @@ public interface BrowseDAO { public void setStartsWith(String startsWith); public String getStartsWith(); + + public void setDateStartsWith(String dateStartsWith); } diff --git a/dspace-api/src/main/java/org/dspace/browse/BrowseEngine.java b/dspace-api/src/main/java/org/dspace/browse/BrowseEngine.java index 351c36248209..f2bfa142a68c 100644 --- a/dspace-api/src/main/java/org/dspace/browse/BrowseEngine.java +++ b/dspace-api/src/main/java/org/dspace/browse/BrowseEngine.java @@ -203,13 +203,25 @@ private BrowseInfo browseByItem(BrowserScope bs) // get the table name that we are going to be getting our data from dao.setTable(browseIndex.getTableName()); - if (scope.getBrowseIndex() != null && OrderFormat.TITLE.equals(scope.getBrowseIndex().getDataType())) { - // For browsing by title, apply the same normalization applied to indexed titles - dao.setStartsWith(normalizeJumpToValue(scope.getStartsWith())); - } else { - dao.setStartsWith(StringUtils.lowerCase(scope.getStartsWith())); + if (StringUtils.isNotBlank(scope.getStartsWith())) { + boolean isDateBrowse = bs.getSortOption().getType().equals("date"); + if (!isDateBrowse) { + if (scope.getBrowseIndex() != null + && OrderFormat.TITLE.equals(scope.getBrowseIndex().getDataType())) { + // For browsing by title, apply the same normalization applied to indexed titles + dao.setStartsWith(normalizeJumpToValue(scope.getStartsWith())); + } else { + dao.setStartsWith(StringUtils.lowerCase(scope.getStartsWith())); + } + } else { + // For "date" sort browses ({@code webui.itemlist.sort-option.*} config): + // sets a date specific filter where the startsWith query is the start date, + // eg `fq=bi_sort_*_sort:+["1940-02" TO+ ]` + dao.setDateStartsWith(scope.getStartsWith().trim()); + } } + // tell the browse query whether we are ascending or descending on the value dao.setAscending(scope.isAscending()); diff --git a/dspace-api/src/main/java/org/dspace/browse/SolrBrowseDAO.java b/dspace-api/src/main/java/org/dspace/browse/SolrBrowseDAO.java index f99aab852bf5..1a21901b2baf 100644 --- a/dspace-api/src/main/java/org/dspace/browse/SolrBrowseDAO.java +++ b/dspace-api/src/main/java/org/dspace/browse/SolrBrowseDAO.java @@ -96,6 +96,8 @@ public int compare(Object o1, Object o2) { private String startsWith = null; + private String dateStartsWith = null; + /** * field to look for value in */ @@ -211,6 +213,13 @@ private DiscoverResult getSolrResponse() throws BrowseException { query.addFilterQueries( "bi_" + orderField + "_sort:" + ClientUtils.escapeQueryChars(startsWith) + "*"); } + if (StringUtils.isNotBlank(dateStartsWith)) { + if (!ascending) { + query.addFilterQueries("bi_" + orderField + "_sort" + ": [* TO \"" + dateStartsWith + "\"]"); + } else { + query.addFilterQueries("bi_" + orderField + "_sort" + ": [\"" + dateStartsWith + "\" TO *]"); + } + } // filter on item to be sure to don't include any other object // indexed in the Discovery Search core query.addFilterQueries("search.resourcetype:" + IndexableItem.TYPE); @@ -462,6 +471,11 @@ public int getLimit() { return limit; } + @Override + public void setDateStartsWith(String dateStartsWith) { + this.dateStartsWith = dateStartsWith; + } + /* * (non-Javadoc) * diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BrowsesResourceControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BrowsesResourceControllerIT.java index d1791ab872bb..8dd2c0b9d0a0 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/BrowsesResourceControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/BrowsesResourceControllerIT.java @@ -1704,8 +1704,8 @@ public void testBrowseByItemsStartsWith() throws Exception { // ---- BROWSES BY ITEM ---- //** WHEN ** //An anonymous user browses the items in the Browse by date issued endpoint - //with startsWith set to 199 - getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=199") + //with startsWith set to 1990 + getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=1990") .param("size", "2")) //** THEN ** @@ -1714,8 +1714,8 @@ public void testBrowseByItemsStartsWith() throws Exception { //We expect the content type to be "application/hal+json;charset=UTF-8" .andExpect(content().contentType(contentType)) - //We expect the totalElements to be the 2 items present in the repository - .andExpect(jsonPath("$.page.totalElements", is(2))) + //We expect the totalElements to be the 5 items from 1990 til now + .andExpect(jsonPath("$.page.totalElements", is(5))) //We expect to jump to page 1 of the index .andExpect(jsonPath("$.page.number", is(0))) .andExpect(jsonPath("$.page.size", is(2))) @@ -1876,8 +1876,8 @@ public void testBrowseByStartsWithAndPage() throws Exception { //** WHEN ** //An anonymous user browses the items in the Browse by date issued endpoint - //with startsWith set to 199 and Page to 1 - getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=199") + //with startsWith set to 1990 and Page to 1 + getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=1990") .param("size", "1").param("page", "1")) //** THEN ** @@ -1886,17 +1886,47 @@ public void testBrowseByStartsWithAndPage() throws Exception { //We expect the content type to be "application/hal+json;charset=UTF-8" .andExpect(content().contentType(contentType)) - //We expect the totalElements to be the 2 items present in the repository - .andExpect(jsonPath("$.page.totalElements", is(2))) + //We expect the totalElements to be the 5 items present in the repository from 1990 until now + .andExpect(jsonPath("$.page.totalElements", is(5))) //We expect to jump to page 1 of the index .andExpect(jsonPath("$.page.number", is(1))) .andExpect(jsonPath("$.page.size", is(1))) - .andExpect(jsonPath("$._links.self.href", containsString("startsWith=199"))) + .andExpect(jsonPath("$._links.self.href", containsString("startsWith=1990"))) - //Verify that the index jumps to the "Java" item. - .andExpect(jsonPath("$._embedded.items", - contains( - ItemMatcher.matchItemWithTitleAndDateIssued(item3, "Java", "1995-05-23") + //Verify that the returned item is 2nd (page 0 first item, page 1 second item) item from 1990 + // Items: Alan Turing - 1912; Blade Runner - 1982-06-25 || Python - 1990; + // Java - 1995-05-23; Zeta Reticuli - 2018-01-01; Moon - 2018-01-02; T-800 - 2029 + // 2nd since 1990: Java + .andExpect(jsonPath("$._embedded.items", + contains(ItemMatcher.matchItemWithTitleAndDateIssued(item3, + "Java", "1995-05-23") + ))); + + getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=1990") + .param("size", "2").param("page", "1")) + //Verify that the returned item is 3rd&4th item from 1990 + // Items: Alan Turing - 1912; Blade Runner - 1982-06-25 || Python - 1990; + // Java - 1995-05-23; Zeta Reticuli - 2018-01-01; Moon - 2018-01-02; T-800 - 2029 + // => Zeta Reticuli & Moon + .andExpect(jsonPath("$._embedded.items", + contains(ItemMatcher.matchItemWithTitleAndDateIssued(item7, + "Zeta Reticuli", "2018-01-01"), + ItemMatcher.matchItemWithTitleAndDateIssued(item4, + "Moon", "2018-01-02") + ))); + + // Sort descending + getClient().perform(get("/api/discover/browses/dateissued/items?startsWith=1990&sort=default,DESC") + .param("size", "2").param("page", "0")) + //Verify that the returned items are from 1990 and below dates + // Items: Alan Turing - 1912; Blade Runner - 1982-06-25 || Python - 1990; + // Java - 1995-05-23; Zeta Reticuli - 2018-01-01; Moon - 2018-01-02; T-800 - 2029 + // => Python & Blade Runner + .andExpect(jsonPath("$._embedded.items", + contains(ItemMatcher.matchItemWithTitleAndDateIssued(item5, + "Python", "1990"), + ItemMatcher.matchItemWithTitleAndDateIssued(item2, + "Blade Runner", "1982-06-25") ))); }