/** * Helper method for executing a query.<p> * * @param cms the CMS context to use * @param index the index to use for the query * @param query the query to perform * @param start the start offset * @param rows the number of results to return * * @return the list of search results * @throws CmsSearchException if something goes wrong */ CmsSolrResultList solrSearch(CmsObject cms, CmsSolrIndex index, String query, int start, int rows) throws CmsSearchException { CmsSolrQuery q = new CmsSolrQuery(null, CmsRequestUtil.createParameterMap(query)); q.setStart(new Integer(start)); q.setRows(new Integer(rows)); CmsSolrResultList resultPage = index.search(cms, q, true); return resultPage; }
/** * Public constructor.<p> * * @param cms the current OpenCms context * @param queryParams the Solr query parameters */ public CmsSolrQuery(CmsObject cms, Map<String, String[]> queryParams) { setQuery(DEFAULT_QUERY); setFields(ALL_RETURN_FIELDS); setRequestHandler(DEFAULT_QUERY_TYPE); setRows(DEFAULT_ROWS); // set the values from the request context if (cms != null) { setLocales(Collections.singletonList(cms.getRequestContext().getLocale())); setSearchRoots(Collections.singletonList(cms.getRequestContext().getSiteRoot() + "/")); } if (queryParams != null) { m_queryParameters = queryParams; } ensureParameters(); ensureReturnFields(); ensureExpiration(); }
/** * Sets the resource types only if not set in the query parameters.<p> * * @param resourceTypes the resourceTypes to set */ public void setResourceTypes(String... resourceTypes) { setResourceTypes(Arrays.asList(resourceTypes)); }
/** * Removes the given filter queries, if already set and then adds the filter queries again.<p> * * @param fqs the filter queries to remove */ private void replaceFilterQueries(String[] fqs) { removeFilterQueries(fqs); addFilterQuery(fqs); } }
/** * Creates and adds a filter query.<p> * * @param fieldName the field name to create a filter query on * @param vals the values that should match for the given field * @param all <code>true</code> to combine the given values with 'AND', <code>false</code> for 'OR' * @param useQuotes <code>true</code> to surround the given values with double quotes, <code>false</code> otherwise */ public void addFilterQuery(String fieldName, List<String> vals, boolean all, boolean useQuotes) { if (getFilterQueries() != null) { for (String fq : getFilterQueries()) { if (fq.startsWith(fieldName + ":")) { removeFilterQuery(fq); } } } addFilterQuery(createFilterQuery(fieldName, vals, all, useQuotes)); }
/** * @see java.lang.Object#clone() */ @Override public CmsSolrQuery clone() { CmsSolrQuery sq = new CmsSolrQuery(null, CmsRequestUtil.createParameterMap(toString())); sq.m_ignoreExpiration = m_ignoreExpiration; return sq; }
final CmsSolrQuery query = new CmsSolrQuery(); query.setCategories(m_categories); query.addFilterQuery(CmsSearchField.FIELD_CONTAINER_TYPES, m_containerTypes, false, false); query.addFilterQuery( CmsSearchUtil.getDateCreatedTimeRangeFilterQuery( CmsSearchField.FIELD_DATE_CREATED, query.addFilterQuery( CmsSearchUtil.getDateCreatedTimeRangeFilterQuery( CmsSearchField.FIELD_DATE_LASTMODIFIED, query.setFields(m_fields.toArray(new String[m_fields.size()])); addFoldersToSearchIn(m_galleries); setSearchFolders(cms); query.addFilterQuery( CmsSearchField.FIELD_PARENT_FOLDERS, new ArrayList<String>(m_foldersToSearchIn), query.addFilterQuery( "-" + CmsSearchField.FIELD_SEARCH_EXCLUDE, Arrays.asList(new String[] {"all", "gallery"}), query.setRows(new Integer(m_matchesPerPage)); query.setResourceTypes(m_resourceTypes);
CmsSolrIndex index = OpenCms.getSearchManager().getIndexSolr(m_settings.getSource()); if (index != null) { CmsSolrQuery query = new CmsSolrQuery( null, CmsRequestUtil.createParameterMap(m_settings.getQuery() + "&fl=path,type")); query.setSearchRoots(m_settings.getPaths()); if ((m_settings.getTypesArray() != null) && (m_settings.getTypesArray().length > 0)) { query.setResourceTypes(m_settings.getTypesArray()); query.setRows(new Integer(999999999)); query.ensureParameters(); try { resources.addAll(index.search(cmsObject, query, true));
query.addFilterQuery(CmsSearchField.FIELD_SEARCH_EXCLUDE + ":\"false\""); SolrQuery initQuery = query.clone(); query.setHighlight(false); LocalSolrQueryRequest solrQueryRequest = null; try { int rows = query.getRows() != null ? query.getRows().intValue() : CmsSolrQuery.DEFAULT_ROWS.intValue(); if (!ignoreMaxRows && (rows > ROWS_MAX)) { rows = ROWS_MAX; int start = query.getStart() != null ? query.getStart().intValue() : 0; int end = start + rows; int page = 0; query.setStart(new Integer(0)); query.setRows(new Integer((5 * rows * page) + start)); new Long(result.getHighlightEndTime() != 0 ? result.getHighlightEndTime() - startTime : 0)}; LOG.debug( query.toString() + "\n" + Messages.get().getBundle().key(Messages.LOG_SOLR_SEARCH_EXECUTED_5, logParams)); Messages.get().container( Messages.LOG_SOLR_ERR_SEARCH_EXECUTION_FAILD_1, CmsEncoder.decode(query.toString()), e),
/** * @see org.opencms.file.collectors.A_CmsResourceCollector#getCreateTypeId(org.opencms.file.CmsObject, java.lang.String, java.lang.String) */ @Override public int getCreateTypeId(CmsObject cms, String collectorName, String param) throws CmsException { int result = -1; Map<String, String> paramsAsMap = getParamsAsMap(param); if (paramsAsMap.get(PARAM_CREATE_PATH) != null) { String solrParams = paramsAsMap.get(SOLR_PART); CmsSolrQuery q = new CmsSolrQuery( null, CmsRequestUtil.createParameterMap( solrParams, Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(), cms.getRequestContext().getEncoding())); String type = CmsSolrQuery.getResourceType(q.getFilterQueries()); if (type != null) { result = OpenCms.getResourceManager().getResourceType(type).getTypeId(); } } return result; }
/** * Adds the content info for the collected resources used in the "This page" publish dialog. */ private void addContentInfo() { if (!m_cms.getRequestContext().getCurrentProject().isOnlineProject() && (null == m_searchController.getCommon().getConfig().getSolrIndex()) && (null != m_addContentInfoForEntries)) { CmsSolrQuery query = new CmsSolrQuery(); m_searchController.addQueryParts(query); query.setStart(Integer.valueOf(0)); query.setRows(m_addContentInfoForEntries); CmsContentLoadCollectorInfo info = new CmsContentLoadCollectorInfo(); info.setCollectorClass(this.getClass().getName()); info.setCollectorParams(query.getQuery()); info.setId((new CmsUUID()).getStringValue()); if (CmsJspTagEditable.getDirectEditProvider(pageContext) != null) { try { CmsJspTagEditable.getDirectEditProvider(pageContext).insertDirectEditListMetadata( pageContext, info); } catch (JspException e) { // TODO: improve + localize error message LOG.error("Could not write content info.", e); } } } }
CmsSolrQuery query = new CmsSolrQuery(); query.setResourceTypes(CmsMessageBundleEditorTypes.BundleType.DESCRIPTOR.toString()); query.setFilterQueries("filename:\"" + basename + CmsMessageBundleEditorTypes.Descriptor.POSTFIX + "\""); query.add("fl", "path"); CmsSolrResultList results; try {
/** * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String) */ public List<CmsResource> getResults(CmsObject cms, String name, String param, int numResults) throws CmsException { name = name == null ? COLLECTORS[1] : name; Map<String, String> paramsAsMap = getParamsAsMap(param); Map<String, String[]> pm = CmsRequestUtil.createParameterMap( paramsAsMap.get(SOLR_PART), Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(), cms.getRequestContext().getEncoding()); CmsSolrQuery q = COLLECTORS_LIST.indexOf(name) == 0 ? new CmsSolrQuery(null, pm) : new CmsSolrQuery(cms, pm); boolean excludeTimerange = Boolean.valueOf( paramsAsMap.get(CmsCollectorData.PARAM_EXCLUDETIMERANGE)).booleanValue(); if (excludeTimerange) { q.removeExpiration(); } if (numResults > 0) { q.setRows(Integer.valueOf(numResults)); } CmsSolrIndex index = CmsSearchManager.getIndexSolr(cms, pm); return new ArrayList<CmsResource>(index.search(cms, q, true)); }
if ((locales == null) || locales.isEmpty()) { m_textSearchFields.add(CmsSearchField.FIELD_TEXT); if (getFilterQueries() != null) { for (String fq : getFilterQueries()) { if (fq.startsWith(CmsSearchField.FIELD_CONTENT_LOCALES + ":")) { removeFilterQuery(fq); addFilterQuery(CmsSearchField.FIELD_CONTENT_LOCALES, localeStrings, false, false); setText(m_text);
/** * Performs a search.<p> * * @param cms the cms object * @param solrQuery the Solr query * * @return a list of documents * * @throws CmsSearchException if something goes wrong * * @see #search(CmsObject, CmsSolrQuery, boolean) */ public CmsSolrResultList search(CmsObject cms, String solrQuery) throws CmsSearchException { return search(cms, new CmsSolrQuery(null, CmsRequestUtil.createParameterMap(solrQuery)), false); }
/** * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery) */ @Override public void addQueryParts(CmsSolrQuery query) { query.setRows(Integer.valueOf(m_config.getPageSize())); final int start = (m_state.getCurrentPage() - 1) * m_config.getPageSize(); query.setStart(Integer.valueOf(start)); }
/** Add query part for the facet, without filters. * @param query The query part that is extended for the facet */ protected void addFacetPart(CmsSolrQuery query) { query.set("facet", "true"); String excludes = ""; if (m_config.getIgnoreAllFacetFilters() || (!m_state.getCheckedEntries().isEmpty() && !m_config.getIsAndFacet())) { excludes = "{!ex=" + m_config.getIgnoreTags() + "}"; } for (I_CmsFacetQueryItem q : m_config.getQueryList()) { query.add("facet.query", excludes + q.getQuery()); } }
/** * Sets the search roots only if not set as query parameter.<p> * * @param searchRoots the searchRoots to set */ public void setSearchRoots(String... searchRoots) { setSearchRoots(Arrays.asList(searchRoots)); }
/** * Ensures that expired and not yet released resources are not returned by default.<p> */ private void ensureExpiration() { boolean expirationDateSet = false; boolean releaseDateSet = false; if (getFilterQueries() != null) { for (String fq : getFilterQueries()) { if (fq.startsWith(CmsSearchField.FIELD_DATE_EXPIRED + ":")) { expirationDateSet = true; } if (fq.startsWith(CmsSearchField.FIELD_DATE_RELEASED + ":")) { releaseDateSet = true; } } } if (!expirationDateSet) { addFilterQuery(CmsSearchField.FIELD_DATE_EXPIRED + ":[NOW TO *]"); } if (!releaseDateSet) { addFilterQuery(CmsSearchField.FIELD_DATE_RELEASED + ":[* TO NOW]"); } }
/** * @see org.opencms.jsp.search.controller.I_CmsSearchController#addQueryParts(CmsSolrQuery) */ @Override public void addQueryParts(CmsSolrQuery query) { final Iterator<I_CmsSearchController> it = m_controllers.iterator(); it.next().addQueryParts(query); while (it.hasNext()) { it.next().addQueryParts(query); } // fix for highlighting bug if ((getHighlighting() != null) && !((query.getParams("df") != null) || (query.getParams("type") != null))) { String df = getHighlighting().getConfig().getHightlightField().trim(); int index = df.indexOf(' '); query.add("df", (index > 0 ? df.substring(0, index) : df)); } }