List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
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 w ww . j a va 2 s . c om*/ */ 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
/** * Execute a Solr search composed from the contents of the * <code>SearchParameters.unformattedQuery</code> property. * The query is filtered by the journal and category fields also contained in the * <code>searchParameters</code> parameter. No filter is created for date ranges, since that * is assumed to be contained in <code>SearchParameters.unformattedQuery</code>. * * @param searchParameters Contains all the parameters necessary to execute a search against * the Solr query engine// www .ja v a2s.c om * @return A subset (determined by <code>SearchParameters.startPage</code> and * <code>SearchParameters.pageSize</code> of the results of the Solr query generated from the * contents of the <code>searchParameters</code> parameter * @throws ApplicationException Thrown during failed interactions with the Solr Server */ public SearchResultSinglePage advancedSearch(SearchParameters searchParameters) throws ApplicationException { SearchParameters sp = cleanStrings(searchParameters); // Does not impact unformattedQuery field. if (log.isDebugEnabled()) { log.debug("Solr Search performed on the unformattedSearch String: " + searchParameters.getUnformattedQuery().trim()); } SolrQuery query = createQuery(null, sp.getStartPage(), sp.getPageSize(), false); query.setQuery(searchParameters.getUnformattedQuery().trim()); SolrQuery journalFacetQuery = createJournalFacetQuery(query.getQuery(), false); setFilters(query, sp, true); setFilters(journalFacetQuery, sp, false); setSort(query, sp); FacetField journals = facetSearch(journalFacetQuery, "cross_published_journal_key"); SearchResultSinglePage results = search(query.setQuery(searchParameters.getUnformattedQuery().trim())); results.setJournalFacet(facetCountsToHashMap(journals)); return results; }
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 w ww . j av 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
/** * Execute a Solr search composed from the contents of the <i>Find An Article</i> search block * including the properties: <code>volume</code>, <code>eNumber</code>, and/or <code>id</code> (DOI). * <p/>/* w ww . ja va 2s . c o m*/ * The query is filtered by the <code>SearchParameters.filterJournals</code> property * also contained in the <code>searchParameters</code> parameter. * <p/> * No filter is created for date ranges or subject categories. * * @param searchParameters Contains all the parameters necessary to execute a search against * the Solr query engine * @return A subset (determined by <code>SearchParameters.startPage</code> and * <code>SearchParameters.pageSize</code> of the results of the Solr query generated from the * contents of the <code>searchParameters</code> parameter * @throws ApplicationException Thrown during failed interactions with the Solr Server */ public SearchResultSinglePage findAnArticleSearch(SearchParameters searchParameters) throws ApplicationException { SearchParameters sp = cleanStrings(searchParameters); // Does not impact unformattedQuery field. if (log.isDebugEnabled()) { log.debug("Solr Search performed on the following selection of the SearchParameters properties: " + "{ filterJournals=" + (sp.getFilterJournals() == null ? null : Arrays.asList(sp.getFilterJournals())) + "\', volume = " + sp.getVolume() + "\', eLocationId = " + sp.getELocationId() + "\', id = " + sp.getId() + "\' }"); } // We should always have exactly one journal. if (sp.getFilterJournals().length != 1) { throw new ApplicationException("Please select exactly one journal."); } SolrQuery query = createQuery(null, sp.getStartPage(), sp.getPageSize(), false); // If ID exists, then search on that first, ignoring all the other fields. if (sp.getId().length() > 0) { query.setQuery("id:" + sp.getId()); return search(query); //if (resultsFromId.getTotalNoOfResults() > 0) { // return resultsFromId; //} } // If no ID or if ID search gives no results, // then attempt a query based on the other submitted fields, if those fields exist int volume = 0; try { volume = Integer.parseInt(sp.getVolume()); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Unable to create an integer from the String volume = " + sp.getVolume()); } } StringBuilder q = new StringBuilder(); // The Query which will be submitted to Solr. if (volume > 0) { q.append(" volume:").append(volume); } if (sp.getELocationId().length() > 0) { if (q.length() > 0) { q.append(" AND "); } q.append(" elocation_id:").append(sp.getELocationId()); } if (log.isDebugEnabled()) { log.debug("findAnArticleSearch: query = " + q.toString()); } query.setQuery(q.toString()); // Form field description: "Journals". Query Filter. query.addFilterQuery(createFilterLimitForJournals(sp.getFilterJournals())); return search(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 w w w. j a v a 2 s. c om 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; }
From source file:org.ambraproject.service.search.SolrSearchService.java
License:Apache License
/** * Execute a Solr search composed from the contents of the <code>SearchParameters.unformattedQuery</code> property. * The query is filtered by the journal and category fields also contained in the <code>searchParameters</code> * parameter. No filter is created for date ranges, since that is assumed to be contained in * <code>SearchParameters.unformattedQuery</code>. * * @param searchParameters Contains all the parameters necessary to execute a search against the Solr query engine * @return A subset (determined by <code>SearchParameters.startPage</code> and <code>SearchParameters.pageSize</code> * of the results of the Solr query generated from the contents of the <code>searchParameters</code> * parameter//from w w w . j a v a 2 s . c om * @throws ApplicationException Thrown during failed interactions with the Solr Server */ public SearchResultSinglePage advancedSearch(SearchParameters searchParameters) throws ApplicationException { SearchParameters sp = cleanStrings(searchParameters); // Does not impact unformattedQuery field. if (log.isDebugEnabled()) { log.debug("Solr Search performed on the unformattedSearch String: " + searchParameters.getUnformattedQuery().trim()); } SolrQuery query = createQuery(null, sp.getStartPage(), sp.getPageSize(), false); query.setQuery(searchParameters.getUnformattedQuery().trim()); SolrQuery journalFacetsQuery = createFacetsQuery(query.getQuery(), "cross_published_journal_key", false); SolrQuery articleTypeFacetsQuery = createFacetsQuery(query.getQuery(), "article_type_facet", false); setFilters(query, sp, false, false); //The journals query doesn't get the journal filter and the articles query doesn't get the articles filter //Notice: there is some code duplication here. note above setFilters(journalFacetsQuery, sp, true, false); setFilters(articleTypeFacetsQuery, sp, false, true); setSort(query, sp); QueryResponse journalFacetsResponse = getSOLRResponse(journalFacetsQuery); QueryResponse articleTypeFacetsResponse = getSOLRResponse(articleTypeFacetsQuery); //Notice: there is some code duplication here. note above FacetField journals = journalFacetsResponse.getFacetField("cross_published_journal_key"); FacetField articleTypes = articleTypeFacetsResponse.getFacetField("article_type_facet"); SearchResultSinglePage results = search(query.setQuery(searchParameters.getUnformattedQuery().trim())); results.setJournalFacet(facetCountsToHashMap(journals)); results.setArticleTypeFacet(facetCountsToHashMap(articleTypes)); return results; }
From source file:org.ambraproject.service.search.SolrSearchService.java
License:Apache License
/** * Populate facets of the search object. * <p/>/*from w w w . jav a 2 s .c o m*/ * If no search results and hence facets are found remove defined filters and try the search again. Journals will * always be the complete list. * * @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.addFacetField("article_type"); 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, false, false); 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 and article type. //So let's use the global list if (results.getTotalNoOfResults() == 0) { results.setSubjectFacet(preFilterResults.getSubjectFacet()); results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet()); } results.setFiltersReset(true); } //Lets always return ALL values for journals //These lists will not be dependant on the user's other //selections other then the query //However, subjects and article type will be! results.setJournalFacet(preFilterResults.getJournalFacet()); results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet()); return results; }
From source file:org.ambraproject.service.search.SolrSearchService.java
License:Apache License
/** * Execute a Solr search composed from the contents of the <i>Find An Article</i> search block including the * properties: <code>volume</code>, <code>eNumber</code>, and/or <code>id</code> (DOI). * <p/>//from w ww . j av a2 s.co m * The query is filtered by the <code>SearchParameters.filterJournals</code> property also contained in the * <code>searchParameters</code> parameter. * <p/> * No filter is created for date ranges or subject categories. * * @param searchParameters Contains all the parameters necessary to execute a search against the Solr query engine * @return A subset (determined by <code>SearchParameters.startPage</code> and <code>SearchParameters.pageSize</code> * of the results of the Solr query generated from the contents of the <code>searchParameters</code> * parameter * @throws ApplicationException Thrown during failed interactions with the Solr Server */ public SearchResultSinglePage findAnArticleSearch(SearchParameters searchParameters) throws ApplicationException { SearchParameters sp = cleanStrings(searchParameters); // Does not impact unformattedQuery field. if (log.isDebugEnabled()) { log.debug("Solr Search performed on the following selection of the SearchParameters properties: " + "{ filterJournals=" + (sp.getFilterJournals() == null ? null : Arrays.asList(sp.getFilterJournals())) + "\', volume = " + sp.getVolume() + "\', eLocationId = " + sp.getELocationId() + "\', id = " + sp.getId() + "\' }"); } // We should always have exactly one journal. if (sp.getFilterJournals().length != 1) { throw new ApplicationException("Please select exactly one journal."); } SolrQuery query = createQuery(null, sp.getStartPage(), sp.getPageSize(), false); // If ID exists, then search on that first, ignoring all the other fields. if (sp.getId().length() > 0) { query.setQuery("id:\"" + sp.getId() + "\""); return search(query); //if (resultsFromId.getTotalNoOfResults() > 0) { // return resultsFromId; //} } // If no ID or if ID search gives no results, // then attempt a query based on the other submitted fields, if those fields exist int volume = 0; try { volume = Integer.parseInt(sp.getVolume()); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Unable to create an integer from the String volume = " + sp.getVolume()); } } StringBuilder q = new StringBuilder(); // The Query which will be submitted to Solr. if (volume > 0) { q.append(" volume:").append(volume); } if (sp.getELocationId().length() > 0) { if (q.length() > 0) { q.append(" AND "); } q.append(" elocation_id:").append(sp.getELocationId()); } if (log.isDebugEnabled()) { log.debug("findAnArticleSearch: query = " + q.toString()); } query.setQuery(q.toString()); // Form field description: "Journals". Query Filter. query.addFilterQuery(createFilterLimitForJournals(sp.getFilterJournals())); return search(query); }
From source file:org.ambraproject.service.search.SolrSearchService.java
License:Apache License
/** * @inheritDoc/*from w ww . ja v a2 s . c o m*/ */ public List savedSearchAlerts(SearchParameters sParams, Date lastSearchTime, Date currentSearchTime, int resultLimit) throws ApplicationException { SolrQuery query = null; SearchParameters sp = null; if (sParams.getUnformattedQuery() == null || sParams.getUnformattedQuery().equals("")) { if (log.isDebugEnabled()) { log.debug("Simple Saved Search performed on the unformattedSearch String: " + sParams.getQuery().trim()); } query = createQuery(sParams.getQuery(), 0, resultLimit, false); query.setQuery(sParams.getQuery()); //If the keywords parameter is specified, we need to change what field we're querying against //aka, body, conclusions, materials and methods ... etc ... if (sParams.getFilterKeyword().length() > 0) { String fieldkey = sParams.getFilterKeyword(); if (!validKeywords.containsKey(fieldkey)) { throw new ApplicationException("Invalid filterKeyword value of " + fieldkey + " specified"); } String fieldName = (String) validKeywords.get(fieldkey); //Set the field for dismax to use query.set("qf", fieldName); } setFilters(query, sParams, false, false); } else { log.debug("Advanced Saved Search performed on the unformattedSearch String: {}", sParams.getUnformattedQuery().trim()); sp = cleanStrings(sParams); query = createQuery(null, 0, resultLimit, false); query.setQuery(sParams.getUnformattedQuery()); setFilters(query, sp, false, false); } query.addFilterQuery(createFilterLimitForPublishDate(lastSearchTime, currentSearchTime)); SearchResultSinglePage results = search(query); return results.getHits(); }
From source file:org.apache.airavata.datacat.server.db.solr.SolrQueryBuilder.java
License:Apache License
public SolrQuery generateQueryFromParameters(List<PrimaryQueryParameter> primaryQueryParameters, String username, String[] userGroups, int startRow, int numberOfRows) { SolrQuery solrQuery = new SolrQuery(); String aclSubQuery = getAclSubQuery(username, userGroups); solrQuery.setQuery(aclSubQuery); PrimaryQueryParameter qp;/* w w w . ja v a 2 s. co m*/ for (int i = 0; i < primaryQueryParameters.size(); i++) { qp = primaryQueryParameters.get(i); switch (qp.getPrimaryQueryType()) { case EQUALS: solrQuery = solrQuery.addFilterQuery(qp.getField() + ":\"" + qp.getFirstParameter() + "\""); break; case PHRASE: solrQuery = solrQuery.addFilterQuery(qp.getField() + ":\"" + qp.getFirstParameter() + "\""); break; case SUBSTRING: solrQuery = solrQuery.addFilterQuery(qp.getField() + ":*" + qp.getFirstParameter() + "*"); break; case WILDCARD: solrQuery = solrQuery.addFilterQuery(qp.getField() + ":" + qp.getFirstParameter()); break; case RANGE: solrQuery = solrQuery.addFilterQuery( qp.getField() + ":[" + qp.getFirstParameter() + " TO " + qp.getSecondParameter() + "]"); break; } } solrQuery.setStart(startRow - 1); solrQuery.setRows(numberOfRows); return solrQuery; }