Commit eb402dba authored by Matija Obreza's avatar Matija Obreza

Merge branch 'server-side-for-ui-335' into 'master'

Full-text query supporting

See merge request genesys-pgr/genesys-server!405
parents 392d6f8c a2ac4497
......@@ -109,6 +109,7 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "dataset_descriptor", joinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "descriptorId", referencedColumnName = "id"))
@OrderColumn(name = "position")
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<Descriptor> descriptors;
......
......@@ -75,6 +75,7 @@ import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.impl.QSubsetAccessionRef;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.worker.AccessionRefMatcher;
import org.genesys2.util.JPAUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -172,6 +173,9 @@ public class DatasetServiceImpl implements DatasetService {
@Autowired
private VersionManager versionManager;
@Autowired
private ElasticsearchService elasticsearchService;
/**
* {@inheritDoc}
*/
......@@ -528,11 +532,13 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public Page<Dataset> listDatasets(final DatasetFilter filter, final Pageable page) {
final Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
final BooleanBuilder published = new BooleanBuilder();
published.and(QDataset.dataset.state.eq(PublishState.PUBLISHED).and(QDataset.dataset.current.isTrue()));
published.and(filter.buildPredicate());
final Page<Dataset> res = datasetRepository.findAll(published, markdownSortPageRequest);
if (filter.isFulltextQuery()) {
return elasticsearchService.findAll(Dataset.class, filter, published, page);
}
final Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
final Page<Dataset> res = datasetRepository.findAll(published.and(filter.buildPredicate()), markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements());
}
......
......@@ -43,6 +43,7 @@ import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.util.JPAUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -92,6 +93,9 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Autowired
private DownloadService downloadService;
@Autowired
private ElasticsearchService elasticsearchService;
/* (non-Javadoc)
* @see org.genesys.catalog.service.DescriptorListService#countDescriptorLists(org.genesys.catalog.model.filters.DescriptorListFilter)
*/
......@@ -326,8 +330,13 @@ public class DescriptorListServiceImpl implements DescriptorListService {
@Override
public Page<DescriptorList> listDescriptorLists(final DescriptorListFilter filters, final Pageable page) {
final BooleanBuilder published = new BooleanBuilder();
published.and(descriptorList.state.in(PublishState.PUBLISHED));
if (filters.isFulltextQuery()) {
return elasticsearchService.findAll(DescriptorList.class, filters, published, page);
}
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page<DescriptorList> res = descriptorListRepository.findAll(new BooleanBuilder().and(filters.buildPredicate()).and(descriptorList.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
Page<DescriptorList> res = descriptorListRepository.findAll(published.and(filters.buildPredicate()), markdownSortPageRequest);
return new PageImpl<DescriptorList>(res.getContent(), page, res.getTotalElements());
}
......
......@@ -307,7 +307,7 @@ public class DescriptorServiceImpl implements DescriptorService {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page<Descriptor> res;
if (descriptorFilter.isFulltextQuery()) {
res = elasticsearchService.findAll(Descriptor.class, descriptorFilter, descriptorFilter._text, markdownSortPageRequest);
res = elasticsearchService.findAll(Descriptor.class, descriptorFilter, page);
} else {
res = descriptorRepository.findAll(new BooleanBuilder().and(descriptorFilter.buildPredicate()).and(QDescriptor.descriptor.state.in(PublishState.PUBLISHED)), markdownSortPageRequest);
}
......
......@@ -43,6 +43,7 @@ import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
......@@ -52,6 +53,7 @@ import org.genesys.catalog.model.Partner;
import org.genesys2.server.model.PublishState;
import org.genesys2.util.MCPDUtil;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
......@@ -64,6 +66,7 @@ import com.fasterxml.jackson.annotation.JsonView;
@Entity
@Table(name = "subset", uniqueConstraints = @UniqueConstraint(name = "UQ_current_subs_version", columnNames={"versionsId", "current"}))
@Audited
@Document(indexName = "subset")
public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
/** The Constant serialVersionUID. */
......@@ -109,11 +112,14 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "institute_id", updatable = false)
@JsonView({ JsonViews.Public.class })
@JsonIgnoreProperties({"owner"})
@Field(type = FieldType.Object)
private FaoInstitute institute;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "owner_id", updatable = false)
@JsonView({ JsonViews.Public.class })
@Field(type = FieldType.Object)
@NotNull
private Partner owner;
......
......@@ -204,7 +204,9 @@ public interface ElasticsearchService {
<T extends BasicModel> List<T> find(Class<T> clazz, BasicModelFilter<?, ?> filter);
<T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, String text, Pageable page);
<T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, Pageable page);
<T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, Predicate predicate, Pageable page);
/**
* Wrapper for search results
......
......@@ -35,7 +35,10 @@ import com.querydsl.core.types.Predicate;
*
* @author Maxym Borodenko
*/
public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> {
public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> implements IFullTextFilter {
/** Any text. */
public String _text;
/** The owner. */
public PartnerFilter owner;
......@@ -105,4 +108,9 @@ public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> {
}
return this;
}
@Override
public String get_text() {
return _text;
}
}
......@@ -371,7 +371,7 @@ public class AccessionServiceImpl implements AccessionService {
// @Cacheable(value = "apiResponses.accessionApi1.list", unless = "#result == null", keyGenerator = "shortFilterKeyGenerator")
public Page<Accession> list(AccessionFilter filter, Pageable page) {
if (filter.isFulltextQuery()) {
return elasticsearchService.findAll(Accession.class, filter, filter._text, page);
return elasticsearchService.findAll(Accession.class, filter, page);
}
List<Accession> content = accessionRepository.findAll(filter, page);
......
......@@ -37,6 +37,7 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.collections4.MapUtils;
......@@ -911,14 +912,23 @@ public class ElasticsearchServiceImpl implements ElasticsearchService, Initializ
}
@Override
public <T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, String text, Pageable page) {
public <T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, Pageable page) {
return findAll(clazz, toEsQuery(clazz, filter), page);
}
@Override
public <T extends BasicModel> Page<T> findAll(Class<T> clazz, BasicModelFilter<?, ?> filter, Predicate predicate, Pageable page) {
return findAll(clazz, toEsQuery(clazz, filter, predicate), page);
}
private <T extends BasicModel> Page<T> findAll(Class<T> clazz, QueryBuilder query, Pageable page) {
String indexName = toIndexName(clazz) + INDEX_READ;
if (page.getOffset() > 100000) {
LOG.warn("Large offset={} for ES query", page.getOffset());
}
SearchRequestBuilder esQuery = client.prepareSearch(indexName).setTypes(COMMON_TYPE_NAME).setQuery(toEsQuery(clazz, filter))
SearchRequestBuilder esQuery = client.prepareSearch(indexName).setTypes(COMMON_TYPE_NAME).setQuery(query)
.setFrom(page.getOffset()).setSize(page.getPageSize());
// for (Sort.Order order : page.getSort()) {
......@@ -1047,10 +1057,18 @@ public class ElasticsearchServiceImpl implements ElasticsearchService, Initializ
}
private QueryBuilder toEsQuery(Class<?> clazz, BasicModelFilter<?, ?> filters) {
return toEsQuery(clazz, filters, null);
}
private QueryBuilder toEsQuery(Class<?> clazz, BasicModelFilter<?, ?> filters, Predicate predicate) {
ElasticQueryBuilder esQb = new ElasticQueryBuilder();
if (filters != null) {
filters.buildPredicate().accept(esQb, null);
BooleanBuilder builder = new BooleanBuilder();
if (predicate != null) {
builder.and(predicate);
}
builder.and(filters.buildPredicate()).accept(esQb, null);
}
QueryBuilder esQuery = esQb.getQuery();
......
......@@ -57,6 +57,7 @@ import org.genesys2.server.persistence.SubsetCreatorRepository;
import org.genesys2.server.persistence.SubsetRepository;
import org.genesys2.server.persistence.SubsetVersionsRepository;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
import org.genesys2.server.service.worker.AccessionRefMatcher;
......@@ -141,6 +142,9 @@ public class SubsetServiceImpl implements SubsetService {
@Autowired
private AccessionRefMatcher accessionRefMatcher;
@Autowired
private ElasticsearchService elasticsearchService;
/**
* {@inheritDoc}
......@@ -149,6 +153,9 @@ public class SubsetServiceImpl implements SubsetService {
public Page<Subset> list(final SubsetFilter filter, final Pageable page) {
final BooleanBuilder published = new BooleanBuilder();
published.and(QSubset.subset.state.eq(PublishState.PUBLISHED).and(QSubset.subset.current.isTrue()));
if (filter.isFulltextQuery()) {
return elasticsearchService.findAll(Subset.class, filter, published, page);
}
published.and(filter.buildPredicate());
return subsetRepository.findAll(published, page);
}
......
......@@ -40,6 +40,7 @@ import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.impl.ElasticsearchServiceImpl;
import org.slf4j.Logger;
......@@ -149,6 +150,7 @@ public class ElasticsearchConfig {
es.indexEntity(Descriptor.class);
es.indexEntity(DescriptorList.class);
es.indexEntity(Dataset.class);
es.indexEntity(Subset.class);
es.indexEntity(Accession.class);
es.indexEntity(FaoInstitute.class);
......
Markdown is supported
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