Skip to content

Commit

Permalink
feat(instance-search): update Date1FieldProcessor to normalize comput…
Browse files Browse the repository at this point in the history
…ed field for sorting & filtering Date 1 (#657)
  • Loading branch information
SvitlanaKovalova1 authored Sep 6, 2024
1 parent d2fb524 commit dc1e8bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,6 @@ does not produce any values, so the following search options will return an empt
| `oclc` | term | `oclc="1234*"` | Matches instances that have an OCLC identifier with the given value |
| `lccn` | term | `lccn = "LCCN"` | Matches instances with the given lccn |
| `normalizedClassificationNumber` | term | `normalizedClassificationNumber == "LCCN"` | Matches instances with the given classification number (normalizes case, whitespaces, special characters, supports leading and trailing wildcard) |
| `dates.date1` | term | `dates.date1="199*"` | Matches instances with the given Date1 (supports leading, trailing and internal wildcards) |
| `dates.date2` | term | `dates.date2="199*"` | Matches instances with the given Date2 (supports leading, trailing and internal wildcards) |
| `normalizedDate1` | term | `normalizedDate1>=1990` | Matches instances with the given Date1 (normalizes alpha 'u' characters) |

##### Holdings search options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.folio.search.service.setter.instance;

import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.folio.search.domain.dto.Dates;
import org.folio.search.domain.dto.Instance;
Expand All @@ -10,9 +9,9 @@
@Component
public class Date1FieldProcessor implements FieldProcessor<Instance, Short> {

private static final Pattern NUMERIC_REGEX = Pattern.compile("^\\d{1,4}$");
private static final int MAX_LENGTH = 4;
private static final String ZERO = "0";
private static final String ALPHA_U = "u";
private static final String NON_NUMERIC_REGEX = "\\D";

@Override
public Short getFieldValue(Instance instance) {
Expand All @@ -24,10 +23,9 @@ public Short getFieldValue(Instance instance) {
}

public Short normalizeDate1(String value) {
String date1 = value.replace(ALPHA_U, ZERO);
var matcher = NUMERIC_REGEX.matcher(date1);
if (matcher.find()) {
return Short.valueOf(matcher.group());
String date1 = value.replaceAll(NON_NUMERIC_REGEX, ZERO);
if (date1.length() <= MAX_LENGTH) {
return Short.valueOf(date1);
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SearchInstanceFilterIT extends BaseIntegrationTest {
"7e5684a9-c8c1-4c1e-85b9-d047f53eeb6d");

private static final String[] DATES = array(
"2021", "1998", "2020", "1978", "2023", "2022");
"2021", "ddd9", "2020", "d99\\", "2023", "2022", "0", "1000");

@BeforeAll
static void prepare() {
Expand Down Expand Up @@ -206,12 +206,14 @@ private static Stream<Arguments> filteredSearchQueriesProvider() {
arguments(format("(item.effectiveLocationId==%s) sortby title", LOCATIONS[0]),
List.of(IDS[0], IDS[2], IDS[3], IDS[4])),

arguments(format("(normalizedDate1<%s) sortby normalizedDate1", DATES[2]), List.of(IDS[3], IDS[1])),
arguments(format("(normalizedDate1>=%s and normalizedDate1<%s) sortby title", DATES[1], DATES[5]),
List.of(IDS[0], IDS[1], IDS[2])),
arguments(format("(normalizedDate1>=%s and normalizedDate1<%s) sortby normalizedDate1", DATES[1], DATES[5]),
List.of(IDS[1], IDS[2], IDS[0])),
arguments(format("(normalizedDate1<%s) sortby normalizedDate1", DATES[2]), List.of(IDS[1], IDS[3])),
arguments(format("(normalizedDate1>=%s and normalizedDate1<%s) sortby title", DATES[6], DATES[0]),
List.of(IDS[1], IDS[2], IDS[3])),
arguments(format("(normalizedDate1>=%s and normalizedDate1<%s) sortby normalizedDate1", DATES[6], DATES[0]),
List.of(IDS[1], IDS[3], IDS[2])),
arguments(format("(normalizedDate1>=%s) sortby title", DATES[0]), List.of(IDS[0], IDS[4])),
arguments(format("(normalizedDate1>%s and normalizedDate1<%s) sortby normalizedDate1", DATES[6], DATES[7]),
List.of(IDS[1], IDS[3])),

arguments("(item.status.name==Available) sortby title", List.of(IDS[0], IDS[1], IDS[4])),
arguments("(item.status.name==Missing) sortby title", List.of(IDS[2], IDS[3])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ private static Stream<Arguments> date1DataProvider() {
arguments(instance("199"), Short.valueOf("199")),
arguments(instance("1u9"), Short.valueOf("109")),
arguments(instance("1"), Short.valueOf("1")),
arguments(instance("19d5"), Short.valueOf("1905")),
arguments(instance("d9d5"), Short.valueOf("905")),
arguments(instance("19 5"), Short.valueOf("1905")),
arguments(instance("195."), Short.valueOf("1950")),
arguments(instance("19\\5"), Short.valueOf("1905")),
arguments(instance("199\\"), Short.valueOf("1990")),
arguments(instance("\\95\\"), Short.valueOf("950")),
arguments(instance("1\\\\"), Short.valueOf("100")),
arguments(instance("1\\\\\\"), Short.valueOf("1000")),
arguments(instance("19999"), Short.valueOf("0")),
arguments(instance("19k5"), Short.valueOf("0")),
arguments(instance("195.."), Short.valueOf("0")),
arguments(instance("19\\5\\"), Short.valueOf("0")),
arguments(instance("\\\\\\\\"), Short.valueOf("0")),
arguments(new Instance(), Short.valueOf("0"))
);
}
Expand Down

0 comments on commit dc1e8bd

Please sign in to comment.