List of usage examples for org.apache.solr.client.solrj SolrQuery addFacetField
public SolrQuery addFacetField(String... fields)
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
License:Open Source License
/** * Helper method to create SolrQuery facets for dataset counts * * @return solrQuery the SolrQuery//from www . j a v a 2 s . c o m */ protected SolrQuery initCountsQuery(String facetField) { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQueryType("standard"); solrQuery.setFacet(true); solrQuery.addFacetField(facetField); solrQuery.setFacetMinCount(0); solrQuery.setFacetLimit(10000); solrQuery.setRows(1); solrQuery.setStart(0); return solrQuery; }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
License:Open Source License
public Collection getRankingFacetByUserIdAndGuid(String userId, String guid) throws Exception { String key = null;/*from www. j ava 2s. c om*/ String sortField = null; try { String[] fq = new String[] {}; SolrQuery solrQuery = new SolrQuery(); solrQuery.setQueryType("standard"); solrQuery.setRows(0); solrQuery.setFacet(true); solrQuery.setFacetMinCount(1); solrQuery.setFacetLimit(-1); // unlimited = -1 if (guid == null || guid.length() < 1 || "*".equals(guid)) { key = "*"; solrQuery.addFacetField("guid"); sortField = "guid"; } else { key = ClientUtils.escapeQueryChars(guid); sortField = "superColumnName"; } solrQuery.addFacetField("superColumnName"); solrQuery.setQuery("idxtype:" + IndexedTypes.RANKING + " AND userId:" + userId + " AND guid:" + key); SearchResultsDTO qr = doSolrQuery(solrQuery, fq, 100, 0, sortField, "asc"); if (qr == null || qr.getFacetResults() == null) { return new ArrayList(); } return qr.getFacetResults(); } catch (SolrServerException ex) { logger.error("Problem communicating with SOLR server. " + ex.getMessage(), ex); return new ArrayList(); } }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
License:Open Source License
public Collection getUserIdFacetByGuid(String guid) throws Exception { String key = null;// w w w . ja v a 2s.com try { String[] fq = new String[] {}; SolrQuery solrQuery = new SolrQuery(); solrQuery.setQueryType("standard"); solrQuery.setRows(0); solrQuery.setFacet(true); solrQuery.setFacetMinCount(1); solrQuery.setFacetLimit(-1); // unlimited = -1 if (guid == null || guid.length() < 1 || "*".equals(guid)) { logger.info("Invalid guid: " + guid); return new ArrayList(); } else { key = ClientUtils.escapeQueryChars(guid); } solrQuery.addFacetField("userId"); solrQuery.setQuery("idxtype:" + IndexedTypes.RANKING + " AND guid:" + key); SearchResultsDTO qr = doSolrQuery(solrQuery, fq, 100, 0, "userId", "asc"); if (qr == null || qr.getFacetResults() == null) { return new ArrayList(); } return qr.getFacetResults(); } catch (SolrServerException ex) { logger.error("Problem communicating with SOLR server. " + ex.getMessage(), ex); return new ArrayList(); } }
From source file:org.ambraproject.article.service.BrowseServiceImpl.java
License:Apache License
private Years loadArticleDates(String journalKey) { Years dates = new Years(); SolrQuery query = createCommonQuery(journalKey); query.setFacet(true);//www . j a va 2 s . c o m query.addFacetField("publication_date"); query.setFacetLimit(-1); query.setFacetMinCount(1); query.setRows(0); try { QueryResponse response = this.serverFactory.getServer().query(query); FacetField ff = response.getFacetField("publication_date"); List<FacetField.Count> counts = ff.getValues(); for (FacetField.Count count : counts) { String publicationDate = count.getName(); Integer y = getYear(publicationDate); Integer m = getMonth(publicationDate); Integer d = getDay(publicationDate); dates.getMonths(y).getDays(m).add(d); } } catch (SolrServerException e) { log.error("Unable to execute a query on the Solr Server.", e); } return dates; }
From source file:org.ambraproject.article.service.BrowseServiceImpl.java
License:Apache License
/** * Get a list of article counts for each category * * @param journalKey the current journal * * @return category info// www.ja v a2 s . c o m */ private SortedMap<String, Long> getSubjectsForJournalViaSolr(String journalKey) { SortedMap<String, Long> categories = new TreeMap<String, Long>(); SolrQuery query = createCommonQuery(journalKey); query.setFacet(true); query.addFacetField("subject_level_1"); query.setFacetLimit(-1); query.setFacetMinCount(1); query.setRows(0); try { QueryResponse response = this.serverFactory.getServer().query(query); FacetField ff = response.getFacetField("subject_level_1"); List<FacetField.Count> counts = ff.getValues(); if (counts != null) { for (FacetField.Count count : counts) { categories.put(count.getName(), count.getCount()); } } } catch (SolrServerException e) { log.error("Unable to execute a query on the Solr Server.", e); } return categories; }
From source file:org.ambraproject.article.service.BrowseServiceImpl.java
License:Apache License
/** * Returns a list of articles for a given category * @param params a collection filters / parameters to browse by * @return articles/*from ww w . j a v a 2 s .com*/ */ private BrowseResult getArticlesBySubjectViaSolr(BrowseParameters params) { BrowseResult result = new BrowseResult(); ArrayList<SearchHit> articles = new ArrayList<SearchHit>(); long total = 0; SolrQuery query = createCommonQuery(params.getJournalKey()); query.addField("title_display"); query.addField("author_display"); query.addField("article_type"); query.addField("publication_date"); query.addField("id"); query.addField("abstract_primary_display"); query.addField("eissn"); if (params.getSubjects() != null && params.getSubjects().length > 0) { StringBuffer subjectQuery = new StringBuffer(); for (String subject : params.getSubjects()) { subjectQuery.append("\"").append(subject).append("\"").append(" AND "); } // remove the last " AND " query.setQuery("subject_level_1:(" + subjectQuery.substring(0, subjectQuery.length() - 5) + ")"); } // we use subject_level_1 field instead of subject_facet field because // we are only interested in the top level subjects query.setFacet(true); query.addFacetField("subject_level_1"); query.setFacetMinCount(1); query.setFacetSort("index"); setSort(query, params); query.setStart(params.getPageNum() * params.getPageSize()); query.setRows(params.getPageSize()); try { QueryResponse response = this.serverFactory.getServer().query(query); SolrDocumentList documentList = response.getResults(); total = documentList.getNumFound(); for (SolrDocument document : documentList) { SearchHit sh = createArticleBrowseDisplay(document, query.toString()); articles.add(sh); } result.setSubjectFacet(facetCountsToHashMap(response.getFacetField("subject_level_1"))); } catch (SolrServerException e) { log.error("Unable to execute a query on the Solr Server.", e); } result.setTotal(total); result.setArticles(articles); return result; }
From source file:org.ambraproject.search.service.SolrSearchService.java
License:Apache License
/** * Populate facets of the search object. * * If no search results and hence facets are found remove defined filters and try * the search again. Journals and ArticleType facets will always be the complete list. * /*from ww w . jav a 2 s.co m*/ * * @param searchParameters The search parameters * @return a populared SearchResultSinglePage object * @throws ApplicationException */ public SearchResultSinglePage getFilterData(SearchParameters searchParameters) throws ApplicationException { //TODO: This function queries SOLR for the journal and article type list //We should migrate this away from config and into a database when it is //available //Does not impact unformattedQuery field. SearchParameters sp = cleanStrings(searchParameters); String q = searchParameters.getUnformattedQuery().trim(); //In this use case, if the query string is empty, we want to get facets for everything if (q.length() == 0) { q = "*:*"; } if (log.isDebugEnabled()) { log.debug("Solr Search performed to get facet data on the unformattedSearch String: " + q); } //We want a complete set of facet data. So first, lets get it all SolrQuery query = createQuery("*:*", 0, 0, false); //Remove facets we don't use in this case query.removeFacetField("author_facet"); query.removeFacetField("editor_facet"); query.removeFacetField("affiliate_facet"); //Add the one we do want in this case. query.addFacetField("cross_published_journal_key"); query.setFacetLimit(MAX_FACET_SIZE); //Related to JO: http://joborder.plos.org/view.php?id=17480 //(for now) we don't want to search on Issue Images query.addFilterQuery(createFilterNoIssueImageDocuments()); SearchResultSinglePage preFilterResults = search(query); setFilters(query, sp, true); query.setQuery(q); SearchResultSinglePage results = null; try { results = search(query); } catch (SolrException e) { query.setQuery("*:*"); if (log.isWarnEnabled()) { log.warn("Solr Search failed on the unformattedSearch String: { " + query.getQuery() + " } so the query will be re-run using the String *:* to populate the Filters" + " on the Advanced Search page.", e); } } if (results == null || results.getTotalNoOfResults() == 0) { //If no results, remove optional filters and try again for (String filter : query.getFilterQueries()) { if (filter.indexOf(createFilterFullDocuments()) < 0) { query.removeFilterQuery(filter); } } results = search(query); //If results are STILL empty. We must return something for subjects. //So let's use the global list if (results.getTotalNoOfResults() == 0) { results.setSubjectFacet(preFilterResults.getSubjectFacet()); } results.setFiltersReset(true); } //Lets always return ALL values for journals and article types //These lists will not be dependant on the user's other //selections other then the query //However, subjects will be! results.setJournalFacet(preFilterResults.getJournalFacet()); results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet()); return results; }
From source file:org.ambraproject.search.service.SolrSearchService.java
License:Apache License
private SolrQuery createQuery(String queryString, int startPage, int pageSize, boolean useDismax) { SolrQuery query = new SolrQuery(queryString); query.setTimeAllowed(queryTimeout);/*from w w w. j a v a 2 s . c o m*/ query.setIncludeScore(true); // The relevance (of each results element) to the search terms. query.setHighlight(true); if (useDismax) { query.set("defType", "dismax"); } //TODO: Put The "options" from the "queryField" picklist into a config file. //This list matches the "options" from the "queryField" picklist on unformattedSearch.ftl, //without the "date" fields. query.set("hl.fl", this.highlightFields); query.set("hl.requireFieldMatch", true); query.setStart(startPage * pageSize); // Which results element to return first in this batch. query.setRows(pageSize); // The number of results elements to return. // request only fields that we need to display query.setFields("id", "score", "title_display", "publication_date", "eissn", "journal", "article_type", "author_display"); query.addFacetField("subject_facet"); query.addFacetField("author_facet"); query.addFacetField("editor_facet"); query.addFacetField("article_type_facet"); query.addFacetField("affiliate_facet"); query.set("facet.method", "fc"); query.setFacetLimit(MAX_FACET_SIZE); query.setFacetMinCount(MIN_FACET_COUNT); // Add a filter to ensure that Solr never returns partial documents query.addFilterQuery(createFilterFullDocuments()); return query; }
From source file:org.ambraproject.search.service.SolrSearchService.java
License:Apache License
private SolrQuery createJournalFacetQuery(String queryString, boolean useDismax) { SolrQuery query = new SolrQuery(queryString); query.setTimeAllowed(queryTimeout);// w w w. j a va 2 s. c o m query.setIncludeScore(false); query.setHighlight(false); query.setRows(0); query.setFacetLimit(MAX_FACET_SIZE); query.setFacetMinCount(MIN_FACET_COUNT); if (useDismax) { query.set("defType", "dismax"); } query.addFacetField("cross_published_journal_key"); // Add a filter to ensure that Solr never returns partial documents query.addFilterQuery(createFilterFullDocuments()); return query; }
From source file:org.ambraproject.search.service.SolrSearchService.java
License:Apache License
private SolrQuery createKeywordFacetQuery(String queryString) { SolrQuery query = new SolrQuery(); query.setTimeAllowed(queryTimeout);//from www . j av a 2 s . com query.setIncludeScore(false); query.setHighlight(false); query.setRows(0); query.set("defType", "dismax"); query.set("qf", "doc_partial_body"); query.addFacetField("doc_partial_type"); query.setFacetLimit(MAX_FACET_SIZE); query.setFacetMinCount(MIN_FACET_COUNT); // Add a filter to ensure that Solr never returns partial documents query.addFilterQuery(createFilterPartialDocuments()); query.setQuery(queryString); return query; }