Commit ba248a95 authored by Matija Obreza's avatar Matija Obreza
Browse files

Enhanced keyword filters for descriptor, descriptor lists, datasets

parent 66081cd9
......@@ -20,6 +20,7 @@ import static org.genesys.catalog.model.dataset.QDataset.dataset;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
......@@ -86,29 +87,26 @@ public class DatasetFilter extends AuditedVersionedModelFilter {
and.and(dataset.language.in(language));
}
if (StringUtils.isNotBlank(_text)) {
/*@formatter:off*/
and.andAnyOf(
dataset.title.containsIgnoreCase(_text),
dataset.description.containsIgnoreCase(_text),
dataset.versionTag.equalsIgnoreCase(_text),
// dataset.crop.equalsIgnoreCase(_text),
// owner
dataset.owner.shortName.equalsIgnoreCase(_text),
dataset.owner.name.containsIgnoreCase(_text),
// descriptor lists
dataset.descriptors.any().title.containsIgnoreCase(_text),
dataset.descriptors.any().crop.equalsIgnoreCase(_text),
// accessions
dataset.accessionIdentifiers.any().genus.equalsIgnoreCase(_text),
dataset.accessionIdentifiers.any().instCode.equalsIgnoreCase(_text),
dataset.accessionIdentifiers.any().acceNumb.equalsIgnoreCase(_text)
ArrayUtils.addAll(
FilterHelpers.equalsAny(_text,
dataset.versionTag,
dataset.owner.shortName,
dataset.descriptors.any().crop,
dataset.accessionIdentifiers.any().genus, dataset.accessionIdentifiers.any().instCode, dataset.accessionIdentifiers.any().acceNumb
),
FilterHelpers.containsAll(_text,
dataset.title, dataset.description,
dataset.owner.name,
dataset.descriptors.any().title
)
)
);
/*@formatter:on*/
}
return and;
}
}
......@@ -19,6 +19,7 @@ import static org.genesys.catalog.model.traits.QDescriptor.descriptor;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.NumberFilter;
......@@ -145,20 +146,18 @@ public class DescriptorFilter extends AuditedVersionedModelFilter {
if (StringUtils.isNotBlank(_text)) {
/*@formatter:off*/
and.andAnyOf(
descriptor.title.containsIgnoreCase(_text),
descriptor.description.containsIgnoreCase(_text),
descriptor.bibliographicCitation.containsIgnoreCase(_text),
descriptor.crop.equalsIgnoreCase(_text),
descriptor.versionTag.equalsIgnoreCase(_text),
descriptor.publisher.equalsIgnoreCase(_text),
// owner
descriptor.owner.shortName.equalsIgnoreCase(_text),
descriptor.owner.name.containsIgnoreCase(_text),
// descriptor lists
descriptor.descriptorLists.any().publisher.equalsIgnoreCase(_text),
descriptor.descriptorLists.any().versionTag.equalsIgnoreCase(_text),
descriptor.descriptorLists.any().crop.equalsIgnoreCase(_text),
descriptor.descriptorLists.any().title.containsIgnoreCase(_text)
ArrayUtils.addAll(
FilterHelpers.equalsAny(_text,
descriptor.crop, descriptor.versionTag, descriptor.publisher,
descriptor.owner.shortName,
descriptor.descriptorLists.any().publisher, descriptor.descriptorLists.any().versionTag, descriptor.descriptorLists.any().crop,
descriptor.descriptorLists.any().title
),
FilterHelpers.containsAll(_text,
descriptor.title, descriptor.description, descriptor.bibliographicCitation,
descriptor.owner.name
)
)
);
/*@formatter:on*/
}
......
......@@ -19,6 +19,7 @@ import static org.genesys.catalog.model.traits.QDescriptorList.descriptorList;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
......@@ -59,7 +60,7 @@ public class DescriptorListFilter extends AuditedVersionedModelFilter {
/** The publisher. */
public Set<String> publisher;
/**
* Builds the query.
*
......@@ -94,19 +95,18 @@ public class DescriptorListFilter extends AuditedVersionedModelFilter {
if (StringUtils.isNotBlank(_text)) {
/*@formatter:off*/
and.andAnyOf(
descriptorList.title.containsIgnoreCase(_text),
descriptorList.description.containsIgnoreCase(_text),
descriptorList.bibliographicCitation.containsIgnoreCase(_text),
descriptorList.crop.equalsIgnoreCase(_text),
descriptorList.versionTag.equalsIgnoreCase(_text),
descriptorList.publisher.equalsIgnoreCase(_text),
// owner
descriptorList.owner.shortName.equalsIgnoreCase(_text),
descriptorList.owner.name.containsIgnoreCase(_text),
// descriptors
descriptorList.descriptors.any().title.containsIgnoreCase(_text),
descriptorList.descriptors.any().crop.equalsIgnoreCase(_text),
descriptorList.descriptors.any().versionTag.equalsIgnoreCase(_text)
ArrayUtils.addAll(
FilterHelpers.equalsAny(_text,
descriptorList.crop, descriptorList.versionTag, descriptorList.publisher,
descriptorList.owner.shortName,
descriptorList.descriptors.any().crop, descriptorList.descriptors.any().versionTag
),
FilterHelpers.containsAll(_text,
descriptorList.title, descriptorList.description, descriptorList.bibliographicCitation,
descriptorList.owner.name,
descriptorList.descriptors.any().title
)
)
);
/*@formatter:on*/
}
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.service.filters;
import java.util.Arrays;
import java.util.regex.Pattern;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.StringPath;
/**
* Filter Helpers.
*/
public abstract class FilterHelpers {
private static final Pattern pattern = Pattern.compile("[^\\w]+", Pattern.UNICODE_CHARACTER_CLASS);
/**
* Create a boolean expression where the string path matches any of the keywords
*
* @param stringPath the string path
* @param keywords the keywords
* @return the boolean expression
*/
public static BooleanExpression equalsAny(StringPath stringPath, String[] keywords) {
BooleanExpression ex = stringPath.equalsIgnoreCase(keywords[0]);
for (int i = 1; i < keywords.length; i++) {
ex = ex.or(stringPath.equalsIgnoreCase(keywords[i]));
}
return ex;
}
/**
* Constructs an array of predicates where each string path matches any of the
* keywords
*
* @param text the text to split into keywords
* @param stringPaths the string paths
* @return the predicate[]
*/
public static Predicate[] equalsAny(String text, StringPath... stringPaths) {
final String[] kw = getKeywords(text);
BooleanExpression[] containsAlls = new BooleanExpression[stringPaths.length];
for (int i = stringPaths.length - 1; i >= 0; i--) {
containsAlls[i] = equalsAny(stringPaths[i], kw);
}
return containsAlls;
}
/**
* Create a boolean expression where the string path contains all keywords
*
* @param stringPath the string path
* @param keywords the keywords
* @return the boolean expression
*/
public static BooleanExpression containsAll(StringPath stringPath, String[] keywords) {
BooleanExpression ex = stringPath.containsIgnoreCase(keywords[0]);
for (int i = 1; i < keywords.length; i++) {
ex = ex.and(stringPath.containsIgnoreCase(keywords[i]));
}
return ex;
}
/**
* Constructs an array of predicates where each string path contains every
* keyword keywords
*
* @param text the text to split into keywords
* @param stringPaths the string paths
* @return the predicate[]
*/
public static Predicate[] containsAll(String _text, StringPath... stringPaths) {
final String[] kw = getKeywords(_text);
BooleanExpression[] containsAlls = new BooleanExpression[stringPaths.length];
for (int i = stringPaths.length - 1; i >= 0; i--) {
containsAlls[i] = containsAll(stringPaths[i], kw);
}
return containsAlls;
}
/**
* Split the input text into keywords
*
* @param text text to split into keywords
* @return array of non-null, non-blank, no-special-chars keywords
*/
protected static String[] getKeywords(String text) {
final String[] kw = Arrays.stream(pattern.split(text)).filter(s -> s.length() > 0).toArray(String[]::new);
// System.err.println("keywords " + text + " -> " + Arrays.toString(kw));
return kw;
}
}
......@@ -92,8 +92,7 @@ public class DescriptorServiceTest extends CatalogServiceTest {
assertThat(result, notNullValue());
}
@Test(expected=DataIntegrityViolationException.class)
@Test(expected = DataIntegrityViolationException.class)
public void testCreateDescriptorWithoutTerms() {
Descriptor input = setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.CODED, VERSION_1_0, false);
descriptorService.createDescriptor(input);
......@@ -429,4 +428,51 @@ public class DescriptorServiceTest extends CatalogServiceTest {
result = descriptorService.updateDescriptor(result);
assertThat(result.getOwner(), not(partner2));
}
@Test
public void testKeywordSearch() {
descriptorService.createDescriptor(setupDescriptor(null, "AAA CCDD", Category.PASSPORT, DataType.BOOLEAN, VERSION_1_0, true));
descriptorService.createDescriptor(setupDescriptor(null, "AAA BBEE1", Category.PASSPORT, DataType.BOOLEAN, VERSION_1_0, true));
Pageable page=new PageRequest(0, 10);
DescriptorFilter descriptorFilter=new DescriptorFilter();
Page<Descriptor> paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(2));
descriptorFilter._text="AAA";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(2));
descriptorFilter._text="BBEE";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(1));
descriptorFilter._text="BB";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(1));
descriptorFilter._text="BB CC";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(0));
descriptorFilter._text="AAA FF";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(0));
descriptorFilter._text="AAA BB";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(1));
descriptorFilter._text="AAA 1";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(1));
descriptorFilter._text="# AAA 1!";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(1));
descriptorFilter._text="NOSUCHTHING";
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(0));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment