Search in sources :

Example 1 with SearchFilter

use of org.ambraproject.wombat.model.SearchFilter in project wombat by PLOS.

the class SearchFilterService method getSearchFilters.

/**
 * Retrieves a map of search filters to be added to the model. The filters displayed will change
 * depending on the query executed, but the number and type of filters is constant.
 *
 * @param query Execute query to determine the search filter results.
 *              Must be set as faceted with the setFacet() method
 * @param urlParams search URL parameters that have been rebuilt from the ArticleSearchQuery object
 * @param site The site to perform the searches in
 * @return HashMap containing all applicable filters
 * @throws IOException
 */
public Map<String, SearchFilter> getSearchFilters(ArticleSearchQuery query, Multimap<String, String> urlParams, Site site) throws IOException {
    ArticleSearchQuery.Builder journalFacetQuery = ArticleSearchQuery.builder().setFacet(JOURNAL_FACET_FIELD).setQuery(query.getQuery().orElse(null)).setSimple(query.isSimple()).setArticleTypes(query.getArticleTypes()).setSubjects(query.getSubjects()).setAuthors(query.getAuthors()).setDateRange(query.getDateRange().orElse(null)).setSections(query.getSections());
    Map<?, ?> journalFacetResults = solrSearchApi.search(journalFacetQuery.build(), site);
    SearchFilter journalFilter = searchFilterFactory.createSearchFilter(journalFacetResults, JOURNAL, urlParams);
    ArticleSearchQuery.Builder subjectAreaFacetQuery = ArticleSearchQuery.builder().setFacet(SUBJECT_AREA_FACET_FIELD).setQuery(query.getQuery().orElse(null)).setSimple(query.isSimple()).setArticleTypes(query.getArticleTypes()).setAuthors(query.getAuthors()).setDateRange(query.getDateRange().orElse(null)).setJournalKeys(query.getJournalKeys()).setSections(query.getSections()).setSubjects(// pass the previously filtered subjects to narrow the results
    query.getSubjects());
    Map<?, ?> subjectAreaFacetResults = solrSearchApi.search(subjectAreaFacetQuery.build(), site);
    SearchFilter subjectAreaFilter = searchFilterFactory.createSearchFilter(subjectAreaFacetResults, SUBJECT_AREA, urlParams);
    ArticleSearchQuery.Builder authorFacetQuery = ArticleSearchQuery.builder().setFacet(AUTHOR_FACET).setQuery(query.getQuery().orElse(null)).setSimple(query.isSimple()).setJournalKeys(query.getJournalKeys()).setArticleTypes(query.getArticleTypes()).setDateRange(query.getDateRange().orElse(null)).setAuthors(// pass the previously filtered authors to narrow the results
    query.getAuthors()).setSubjects(query.getSubjects()).setSections(query.getSections());
    Map<?, ?> authorFacetResults = solrSearchApi.search(authorFacetQuery.build(), site);
    SearchFilter authorFilter = searchFilterFactory.createSearchFilter(authorFacetResults, AUTHOR, urlParams);
    ArticleSearchQuery.Builder articleTypeFacetQuery = ArticleSearchQuery.builder().setFacet(ARTICLE_TYPE_FACET).setQuery(query.getQuery().orElse(null)).setSimple(query.isSimple()).setDateRange(query.getDateRange().orElse(null)).setJournalKeys(query.getJournalKeys()).setSubjects(query.getSubjects()).setAuthors(query.getAuthors()).setSections(query.getSections());
    Map<?, ?> articleTypeFacetResults = solrSearchApi.search(articleTypeFacetQuery.build(), site);
    SearchFilter articleTypeFilter = searchFilterFactory.createSearchFilter(articleTypeFacetResults, ARTICLE_TYPE, urlParams);
    ArticleSearchQuery.Builder sectionFacetQuery = ArticleSearchQuery.builder().setFacet(SECTION_FACET).setIsPartialSearch(true).setQuery(query.getQuery().orElse(null)).setSimple(query.isSimple()).setDateRange(query.getDateRange().orElse(null)).setJournalKeys(query.getJournalKeys()).setSubjects(query.getSubjects()).setAuthors(query.getAuthors());
    Map<?, ?> sectionFacetResults = solrSearchApi.search(sectionFacetQuery.build(), site);
    SearchFilter sectionFilter = searchFilterFactory.createSearchFilter(sectionFacetResults, SECTION, urlParams);
    // TODO: add other filters here
    Map<String, SearchFilter> filters = new HashMap<>();
    filters.put(JOURNAL, journalFilter);
    filters.put(SUBJECT_AREA, subjectAreaFilter);
    filters.put(AUTHOR, authorFilter);
    filters.put(ARTICLE_TYPE, articleTypeFilter);
    filters.put(SECTION, sectionFilter);
    return filters;
}
Also used : ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) HashMap(java.util.HashMap) SearchFilter(org.ambraproject.wombat.model.SearchFilter)

Example 2 with SearchFilter

use of org.ambraproject.wombat.model.SearchFilter in project wombat by PLOS.

the class SearchFilterService method getSearchFilters.

/**
 * Retrieves a map of search filters to be added to the model. The filters displayed will change
 * depending on the query executed, but the number and type of filters is constant.
 *
 * @param query Execute query to determine the search filter results.
 *              Must be set as faceted with the addFacet() method
 * @param urlParams search URL parameters that have been rebuilt from the ArticleSearchQuery object
 * @param site The site to perform the searches in
 * @return HashMap containing all applicable filters
 * @throws IOException
 */
public Map<String, SearchFilter> getSearchFilters(ArticleSearchQuery query) throws IOException {
    ImmutableList<String> facetFields = ImmutableList.of(JOURNAL_FACET_FIELD, ARTICLE_TYPE_FACET_FIELD, AUTHOR_FACET_FIELD, SUBJECT_AREA_FACET_FIELD);
    /* The journal and article type facets are special because we want
     * to do an OR query with them, not an AND query. For example, a
     * client wants to find articles in One OR in Medicine - an
     * article can't be in both. For the rest of the facets the user
     * wants to find content that matches ALL the conditions (AND) -
     * not articles about neurons OR organisms, but articles about
     * neurons AND organisms. This complicates the facets because we
     * want to exclude the existing certain filter queries (fq) when
     * calculating these facet. See
     * https://lucene.apache.org/solr/guide/7_4/faceting.html#Faceting-TaggingandExcludingFilters
     * for how this mechanism works.
     */
    ArticleSearchQuery.Facet journalFacet = ArticleSearchQuery.Facet.builder().setField(JOURNAL_FACET_FIELD).setExcludeKey(JOURNAL_TAG).build();
    ArticleSearchQuery.Facet articleTypeFacet = ArticleSearchQuery.Facet.builder().setField(ARTICLE_TYPE_FACET_FIELD).setExcludeKey(ARTICLE_TYPE_TAG).build();
    ArticleSearchQuery facetQuery = query.toBuilder().setRows(0).setSortOrder(null).addFacet(journalFacet).addFacet(articleTypeFacet).addFacet(AUTHOR_FACET_FIELD).addFacet(SUBJECT_AREA_FACET_FIELD).build();
    SolrSearchApi.Result results = solrSearchApi.search(facetQuery);
    Map<String, Map<String, Integer>> facets = results.getFacets();
    SearchFilter journalFilter = searchFilterFactory.createSearchFilter(facets.get(JOURNAL_FACET_FIELD), JOURNAL);
    SearchFilter subjectAreaFilter = searchFilterFactory.createSearchFilter(facets.get(SUBJECT_AREA_FACET_FIELD), SUBJECT_AREA);
    SearchFilter authorFilter = searchFilterFactory.createSearchFilter(facets.get(AUTHOR_FACET_FIELD), AUTHOR);
    SearchFilter articleTypeFilter = searchFilterFactory.createSearchFilter(facets.get(ARTICLE_TYPE_FACET_FIELD), ARTICLE_TYPE);
    ArticleSearchQuery sectionFacetQuery = query.toBuilder().addFacet(SECTION_FACET_FIELD).setRows(0).setSortOrder(null).setPartialSearch(true).build();
    Map<String, Integer> sectionFacetResults = solrSearchApi.search(sectionFacetQuery).getFacets().get(SECTION_FACET_FIELD);
    SearchFilter sectionFilter = searchFilterFactory.createSearchFilter(sectionFacetResults, SECTION);
    // TODO: add other filters here
    Map<String, SearchFilter> filters = new HashMap<>();
    filters.put(JOURNAL, journalFilter);
    filters.put(SUBJECT_AREA, subjectAreaFilter);
    filters.put(AUTHOR, authorFilter);
    filters.put(ARTICLE_TYPE, articleTypeFilter);
    filters.put(SECTION, sectionFilter);
    return filters;
}
Also used : ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) HashMap(java.util.HashMap) SearchFilter(org.ambraproject.wombat.model.SearchFilter) SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with SearchFilter

use of org.ambraproject.wombat.model.SearchFilter in project wombat by PLOS.

the class SearchFilterServiceTest method testGetSearchFilters.

@Test
public void testGetSearchFilters() throws IOException {
    SearchFilter mockFilter = mock(SearchFilter.class);
    when(searchFilterFactory.createSearchFilter(any(), any())).thenReturn(mockFilter);
    ArticleSearchQuery query = ArticleSearchQuery.builder().setQuery("blah").setSimple(true).build();
    SolrSearchApi.Result result = mock(SolrSearchApi.Result.class);
    Map<String, Map<String, Integer>> facetMap = mock(Map.class);
    when(result.getFacets()).thenReturn(facetMap);
    when(facetMap.get(any())).thenReturn(ImmutableMap.of());
    when(solrSearchApi.search(any())).thenReturn(result);
    ImmutableMap<String, SearchFilter> expected = ImmutableMap.of("subject_area", mockFilter, "journal", mockFilter, "article_type", mockFilter, "author", mockFilter, "section", mockFilter);
    Map<String, SearchFilter> searchFilters = searchFilterService.getSearchFilters(query);
    assertEquals(expected, searchFilters);
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) SearchFilter(org.ambraproject.wombat.model.SearchFilter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SearchFilter (org.ambraproject.wombat.model.SearchFilter)3 ArticleSearchQuery (org.ambraproject.wombat.service.remote.ArticleSearchQuery)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SolrSearchApi (org.ambraproject.wombat.service.remote.SolrSearchApi)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Test (org.junit.Test)1