DiscoverQueryBuilder implements InitializingBean { public DiscoverQuery buildQuery(Context context, IndexableObject scope, DiscoveryConfiguration discoveryConfiguration, String query, List<SearchFilter> searchFilters, String dsoType, Pageable page) throws DSpaceBadRequestException { List<String> dsoTypes = dsoType != null ? singletonList(dsoType) : emptyList(); return buildQuery(context, scope, discoveryConfiguration, query, searchFilters, dsoTypes, page); }  @Override void afterPropertiesSet(); DiscoverQuery buildQuery(Context context, IndexableObject scope,
                                    DiscoveryConfiguration discoveryConfiguration,
                                    String query, List<SearchFilter> searchFilters,
                                    String dsoType, Pageable page); DiscoverQuery buildQuery(Context context, IndexableObject scope,
                                    DiscoveryConfiguration discoveryConfiguration,
                                    String query, List<SearchFilter> searchFilters,
                                    List<String> dsoTypes, Pageable page); DiscoverQuery buildFacetQuery(Context context, IndexableObject scope,
                                         DiscoveryConfiguration discoveryConfiguration,
                                         String prefix, String query, List<SearchFilter> searchFilters,
                                         String dsoType, Pageable page, String facetName); DiscoverQuery buildFacetQuery(Context context, IndexableObject scope,
                                         DiscoveryConfiguration discoveryConfiguration,
                                         String prefix, String query, List<SearchFilter> searchFilters,
                                         List<String> dsoTypes, Pageable page, String facetName); void setIndexableFactories(List<IndexFactory> indexableFactories);  }
@Test public void testBuildQuery() throws Exception { DiscoverQuery discoverQuery = queryBuilder .buildQuery(context, scope, discoveryConfiguration, query, Arrays.asList(searchFilter), "item", page); assertThat(discoverQuery.getFilterQueries(), containsInAnyOrder("archived:true", "subject:\"Java\"")); assertThat(discoverQuery.getQuery(), is(query)); assertThat(discoverQuery.getDSpaceObjectFilters(), contains(IndexableItem.TYPE)); assertThat(discoverQuery.getSortField(), is("dc.title_sort")); assertThat(discoverQuery.getSortOrder(), is(DiscoverQuery.SORT_ORDER.asc)); assertThat(discoverQuery.getMaxResults(), is(10)); assertThat(discoverQuery.getStart(), is(10)); assertThat(discoverQuery.getFacetMinCount(), is(1)); assertThat(discoverQuery.getFacetOffset(), is(0)); assertThat(discoverQuery.getFacetFields(), hasSize(2)); assertThat(discoverQuery.getFacetFields(), containsInAnyOrder( discoverFacetFieldMatcher(new DiscoverFacetField("subject", TYPE_TEXT, 6, COUNT)), discoverFacetFieldMatcher(new DiscoverFacetField("hierarchy", TYPE_HIERARCHICAL, 8, VALUE)) )); assertThat(discoverQuery.getHitHighlightingFields(), hasSize(2)); assertThat(discoverQuery.getHitHighlightingFields(), containsInAnyOrder( discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("dc.title", 0, 3)), discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("fulltext", 0, 3)) )); }
@Test public void testBuildQueryDefaults() throws Exception { DiscoverQuery discoverQuery = queryBuilder.buildQuery(context, null, discoveryConfiguration, null, null, emptyList(), null); assertThat(discoverQuery.getFilterQueries(), containsInAnyOrder("archived:true")); assertThat(discoverQuery.getQuery(), is(emptyOrNullString())); assertThat(discoverQuery.getDSpaceObjectFilters(), is(empty())); assertThat(discoverQuery.getSortField(), is("dc.date.accessioned_sort")); assertThat(discoverQuery.getSortOrder(), is(DiscoverQuery.SORT_ORDER.desc)); assertThat(discoverQuery.getMaxResults(), is(100)); assertThat(discoverQuery.getStart(), is(0)); assertThat(discoverQuery.getFacetMinCount(), is(1)); assertThat(discoverQuery.getFacetOffset(), is(0)); assertThat(discoverQuery.getFacetFields(), hasSize(2)); assertThat(discoverQuery.getFacetFields(), containsInAnyOrder( discoverFacetFieldMatcher(new DiscoverFacetField("subject", TYPE_TEXT, 6, COUNT)), discoverFacetFieldMatcher(new DiscoverFacetField("hierarchy", TYPE_HIERARCHICAL, 8, VALUE)) )); assertThat(discoverQuery.getHitHighlightingFields(), hasSize(2)); assertThat(discoverQuery.getHitHighlightingFields(), containsInAnyOrder( discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("dc.title", 0, 3)), discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("fulltext", 0, 3)) )); }
@Test public void testSortByScore() throws Exception { page = PageRequest.of(2, 10, Sort.Direction.ASC, "SCORE"); DiscoverQuery discoverQuery = queryBuilder.buildQuery(context, null, discoveryConfiguration, null, null, emptyList(), page); assertThat(discoverQuery.getFilterQueries(), containsInAnyOrder("archived:true")); assertThat(discoverQuery.getQuery(), is(emptyOrNullString())); assertThat(discoverQuery.getDSpaceObjectFilters(), is(empty())); assertThat(discoverQuery.getSortField(), is("score_sort")); assertThat(discoverQuery.getSortOrder(), is(DiscoverQuery.SORT_ORDER.asc)); assertThat(discoverQuery.getMaxResults(), is(10)); assertThat(discoverQuery.getStart(), is(20)); assertThat(discoverQuery.getFacetMinCount(), is(1)); assertThat(discoverQuery.getFacetOffset(), is(0)); assertThat(discoverQuery.getFacetFields(), hasSize(2)); assertThat(discoverQuery.getFacetFields(), containsInAnyOrder( discoverFacetFieldMatcher(new DiscoverFacetField("subject", TYPE_TEXT, 6, COUNT)), discoverFacetFieldMatcher(new DiscoverFacetField("hierarchy", TYPE_HIERARCHICAL, 8, VALUE)) )); assertThat(discoverQuery.getHitHighlightingFields(), hasSize(2)); assertThat(discoverQuery.getHitHighlightingFields(), containsInAnyOrder( discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("dc.title", 0, 3)), discoverHitHighlightingFieldMatcher(new DiscoverHitHighlightingField("fulltext", 0, 3)) )); }
@Test(expected = DSpaceBadRequestException.class) public void testInvalidDSOType() throws Exception { queryBuilder .buildQuery(context, scope, discoveryConfiguration, query, Arrays.asList(searchFilter), "TEST", page); }
@Test(expected = DSpaceBadRequestException.class) public void testInvalidSortField() throws Exception { page = PageRequest.of(2, 10, Sort.Direction.ASC, "test"); queryBuilder .buildQuery(context, scope, discoveryConfiguration, query, Arrays.asList(searchFilter), "ITEM", page); }
@Test(expected = DSpaceBadRequestException.class) public void testInvalidSearchFilter1() throws Exception { searchFilter = new SearchFilter("test", "equals", "Smith, Donald"); queryBuilder .buildQuery(context, scope, discoveryConfiguration, query, Arrays.asList(searchFilter), "ITEM", page); }
@Test(expected = DSpaceBadRequestException.class) public void testInvalidSearchFilter2() throws Exception { when(searchService.toFilterQuery(any(Context.class), any(String.class), any(String.class), any(String.class))) .thenThrow(SQLException.class); queryBuilder .buildQuery(context, scope, discoveryConfiguration, query, Arrays.asList(searchFilter), "ITEM", page); }