Search in sources :

Example 6 with ArticleSearchQuery

use of org.ambraproject.wombat.service.remote.ArticleSearchQuery 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 7 with ArticleSearchQuery

use of org.ambraproject.wombat.service.remote.ArticleSearchQuery in project wombat by PLOS.

the class HomeController method getRssFeedView.

/**
 * Serves recent journal articles as XML to be read by an RSS reader
 *
 * @param site site the request originates from
 * @return RSS view of recent articles for the specified site
 * @throws IOException
 */
@RequestMapping(name = "homepageFeed", value = "/feed/{feedType:atom|rss}", method = RequestMethod.GET)
public ModelAndView getRssFeedView(@SiteParam Site site, @PathVariable String feedType) throws IOException {
    ArticleSearchQuery query = ArticleSearchQuery.builder().setStart(0).setRows(getFeedLength(site)).setSortOrder(ArticleSearchQuery.SolrSortOrder.DATE_NEWEST_FIRST).setJournalKeys(ImmutableList.of(site.getJournalKey())).setDateRange(ArticleSearchQuery.SolrEnumeratedDateRange.ALL_TIME).setFields(ArticleSearchQuery.RSS_FIELDS).build();
    SolrSearchApi.Result recentArticles = solrSearchApi.search(query);
    ModelAndView mav = new ModelAndView();
    FeedMetadataField.SITE.putInto(mav, site);
    FeedMetadataField.FEED_INPUT.putInto(mav, recentArticles.getDocs());
    mav.setView(FeedType.getView(articleFeedView, feedType));
    return mav;
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with ArticleSearchQuery

use of org.ambraproject.wombat.service.remote.ArticleSearchQuery in project wombat by PLOS.

the class SearchController method getSearchRssFeedView.

/**
 * Performs a simple search and serves the result as XML to be read by an RSS reader
 *
 * @param request HttpServletRequest
 * @param model   model that will be passed to the template
 * @param site    site the request originates from
 * @param params  search parameters identical to the {@code search} method
 * @return RSS view of articles returned by the search
 * @throws IOException
 */
@RequestMapping(name = "searchFeed", value = "/search/feed/{feedType:atom|rss}", params = { "q" }, method = RequestMethod.GET)
public ModelAndView getSearchRssFeedView(HttpServletRequest request, Model model, @SiteParam Site site, @PathVariable String feedType, @RequestParam MultiValueMap<String, String> params) throws IOException {
    CommonParams commonParams = modelCommonParams(request, model, site, params);
    String queryString = params.getFirst("q");
    ArticleSearchQuery query = commonParams.makeArticleSearchQueryBuilder().setQuery(queryString).setSimple(commonParams.isSimpleSearch(queryString)).setFields(ArticleSearchQuery.RSS_FIELDS).build();
    SolrSearchApi.Result searchResults = solrSearchApi.search(query);
    String feedTitle = new UrlParamBuilder().addAll(params).build().toString();
    ;
    ModelAndView mav = new ModelAndView();
    FeedMetadataField.SITE.putInto(mav, site);
    FeedMetadataField.FEED_INPUT.putInto(mav, searchResults.getDocs());
    FeedMetadataField.TITLE.putInto(mav, feedTitle);
    mav.setView(FeedType.getView(articleFeedView, feedType));
    return mav;
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) UrlParamBuilder(org.ambraproject.wombat.util.UrlParamBuilder) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with ArticleSearchQuery

use of org.ambraproject.wombat.service.remote.ArticleSearchQuery in project wombat by PLOS.

the class SearchController method performValidSearch.

private boolean performValidSearch(HttpServletRequest request, Model model, @SiteParam Site site, @RequestParam MultiValueMap<String, String> params) throws IOException {
    CommonParams commonParams = modelCommonParams(request, model, site, params);
    String queryString = params.getFirst("q");
    ArticleSearchQuery query = commonParams.makeArticleSearchQueryBuilder().setQuery(queryString).setSimple(commonParams.isSimpleSearch(queryString)).build();
    SolrSearchApi.Result searchResults;
    try {
        searchResults = solrSearchApi.search(query);
    } catch (ServiceRequestException sre) {
        model.addAttribute(isInvalidSolrRequest(queryString, sre) ? CANNOT_PARSE_QUERY_ERROR : UNKNOWN_QUERY_ERROR, true);
        // not a valid search - report errors
        return false;
    }
    addFiltersToModel(request, model, site, commonParams, query, searchResults);
    model.addAttribute("searchResults", addArticleLinks(searchResults, request, site, siteSet));
    model.addAttribute("alertQuery", alertService.convertParamsToJson(params));
    // valid search - proceed to return results
    return true;
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) ServiceRequestException(org.ambraproject.wombat.service.remote.ServiceRequestException)

Example 10 with ArticleSearchQuery

use of org.ambraproject.wombat.service.remote.ArticleSearchQuery in project wombat by PLOS.

the class ArticleArchiveServiceImpl method getDatesForJournal.

@Override
public Range<Date> getDatesForJournal(Site site) throws IOException, ParseException {
    ArticleSearchQuery query = ArticleSearchQuery.builder().setRows(0).setStatsField("publication_date").setSortOrder(SolrSortOrder.RELEVANCE).setDateRange(SolrEnumeratedDateRange.ALL_TIME).setJournalKeys(ImmutableList.of(site.getJournalKey())).build();
    SolrSearchApi.Result result = solrSearchApi.search(query);
    Date minDate = result.getPublicationDateStats().getMin();
    Date maxDate = result.getPublicationDateStats().getMax();
    return Range.between(minDate, maxDate);
}
Also used : SolrSearchApi(org.ambraproject.wombat.service.remote.SolrSearchApi) ArticleSearchQuery(org.ambraproject.wombat.service.remote.ArticleSearchQuery) Date(java.util.Date)

Aggregations

ArticleSearchQuery (org.ambraproject.wombat.service.remote.ArticleSearchQuery)17 SolrSearchApi (org.ambraproject.wombat.service.remote.SolrSearchApi)9 HashMap (java.util.HashMap)3 Map (java.util.Map)3 SearchFilter (org.ambraproject.wombat.model.SearchFilter)3 ServiceRequestException (org.ambraproject.wombat.service.remote.ServiceRequestException)2 CacheKey (org.ambraproject.wombat.util.CacheKey)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Site (org.ambraproject.wombat.config.site.Site)1