List of usage examples for org.apache.solr.client.solrj SolrQuery getParams
@Override
public String[] getParams(String param)
From source file:ddf.catalog.cache.solr.impl.SolrFilterDelegate.java
License:Open Source License
private void combineXpathFilterQueries(SolrQuery query, List<SolrQuery> subQueries, String operator) { List<String> queryParams = new ArrayList<String>(); // Use Set to remove duplicates now that the namespaces have been stripped out Set<String> xpathFilters = new TreeSet<String>(); Set<String> xpathIndexes = new TreeSet<String>(); for (SolrQuery subQuery : subQueries) { String[] params = subQuery.getParams(FILTER_QUERY_PARAM_NAME); if (params != null) { for (String param : params) { if (StringUtils.startsWith(param, XPATH_QUERY_PARSER_PREFIX)) { if (StringUtils.contains(param, XPATH_FILTER_QUERY_INDEX)) { xpathIndexes/*from w w w . j a va 2s . c o m*/ .add(StringUtils.substringAfter(StringUtils.substringBeforeLast(param, "\""), XPATH_FILTER_QUERY_INDEX + ":\"")); } else if (StringUtils.startsWith(param, XPATH_QUERY_PARSER_PREFIX + XPATH_FILTER_QUERY)) { xpathFilters.add(StringUtils.substringAfter( StringUtils.substringBeforeLast(param, "\""), XPATH_FILTER_QUERY + ":\"")); } } Collections.addAll(queryParams, param); } } } if (xpathFilters.size() > 1) { // More than one XPath found, need to combine String filter = XPATH_QUERY_PARSER_PREFIX + XPATH_FILTER_QUERY + ":\"(" + StringUtils.join(xpathFilters, operator.toLowerCase()) + ")\""; List<String> indexes = new ArrayList<String>(); for (String xpath : xpathIndexes) { indexes.add("(" + XPATH_FILTER_QUERY_INDEX + ":\"" + xpath + "\")"); } String index = XPATH_QUERY_PARSER_PREFIX + StringUtils.join(indexes, operator); query.setParam(FILTER_QUERY_PARAM_NAME, filter, index); } else if (queryParams.size() > 0) { // Pass through original filter queries if only a single XPath is present query.setParam(FILTER_QUERY_PARAM_NAME, queryParams.toArray(new String[queryParams.size()])); } }
From source file:ddf.catalog.source.solr.SolrFilterDelegate.java
License:Open Source License
private void combineXpathFilterQueries(SolrQuery query, List<SolrQuery> subQueries, String operator) { List<String> queryParams = new ArrayList<>(); // Use Set to remove duplicates now that the namespaces have been stripped out Set<String> xpathFilters = new TreeSet<>(); Set<String> xpathIndexes = new TreeSet<>(); for (SolrQuery subQuery : subQueries) { String[] params = subQuery.getParams(FILTER_QUERY_PARAM_NAME); if (params != null) { for (String param : params) { if (StringUtils.startsWith(param, XPATH_QUERY_PARSER_PREFIX)) { if (StringUtils.contains(param, XPATH_FILTER_QUERY_INDEX)) { xpathIndexes//from w w w. j ava 2 s .c om .add(StringUtils.substringAfter(StringUtils.substringBeforeLast(param, "\""), XPATH_FILTER_QUERY_INDEX + ":\"")); } else if (StringUtils.startsWith(param, XPATH_QUERY_PARSER_PREFIX + XPATH_FILTER_QUERY)) { xpathFilters.add(StringUtils.substringAfter( StringUtils.substringBeforeLast(param, "\""), XPATH_FILTER_QUERY + ":\"")); } } Collections.addAll(queryParams, param); } } } if (xpathFilters.size() > 1) { // More than one XPath found, need to combine String filter = XPATH_QUERY_PARSER_PREFIX + XPATH_FILTER_QUERY + ":\"(" + StringUtils.join(xpathFilters, operator.toLowerCase()) + ")\""; List<String> indexes = new ArrayList<>(); for (String xpath : xpathIndexes) { indexes.add("(" + XPATH_FILTER_QUERY_INDEX + ":\"" + xpath + "\")"); } String index = XPATH_QUERY_PARSER_PREFIX + StringUtils.join(indexes, operator); query.setParam(FILTER_QUERY_PARAM_NAME, filter, index); } else if (queryParams.size() > 0) { // Pass through original filter queries if only a single XPath is present query.setParam(FILTER_QUERY_PARAM_NAME, queryParams.toArray(new String[queryParams.size()])); } }
From source file:org.opencms.search.solr.CmsSolrIndex.java
License:Open Source License
/** * Performs the actual search.<p>/*from ww w . j av a2 s . co m*/ * * @param cms the current OpenCms context * @param ignoreMaxRows <code>true</code> to return all all requested rows, <code>false</code> to use max rows * @param query the OpenCms Solr query * @param response the servlet response to write the query result to, may also be <code>null</code> * @param ignoreSearchExclude if set to false, only contents with search_exclude unset or "false" will be found - typical for the the non-gallery case * @param filter the resource filter to use * * @return the found documents * * @throws CmsSearchException if something goes wrong * * @see #search(CmsObject, CmsSolrQuery, boolean) */ @SuppressWarnings("unchecked") public CmsSolrResultList search(CmsObject cms, final CmsSolrQuery query, boolean ignoreMaxRows, ServletResponse response, boolean ignoreSearchExclude, CmsResourceFilter filter) throws CmsSearchException { // check if the user is allowed to access this index checkOfflineAccess(cms); if (!ignoreSearchExclude) { query.addFilterQuery(CmsSearchField.FIELD_SEARCH_EXCLUDE + ":\"false\""); } int previousPriority = Thread.currentThread().getPriority(); long startTime = System.currentTimeMillis(); // remember the initial query SolrQuery initQuery = query.clone(); query.setHighlight(false); LocalSolrQueryRequest solrQueryRequest = null; try { // initialize the search context CmsObject searchCms = OpenCms.initCmsObject(cms); // change thread priority in order to reduce search impact on overall system performance if (getPriority() > 0) { Thread.currentThread().setPriority(getPriority()); } // the lists storing the found documents that will be returned List<CmsSearchResource> resourceDocumentList = new ArrayList<CmsSearchResource>(); SolrDocumentList solrDocumentList = new SolrDocumentList(); // Initialize rows, offset, end and the current page. 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; if (rows > 0) { page = Math.round(start / rows) + 1; } // set the start to '0' and expand the rows before performing the query query.setStart(new Integer(0)); query.setRows(new Integer((5 * rows * page) + start)); // perform the Solr query and remember the original Solr response QueryResponse queryResponse = m_solr.query(query); long solrTime = System.currentTimeMillis() - startTime; // initialize the counts long hitCount = queryResponse.getResults().getNumFound(); start = -1; end = -1; if ((rows > 0) && (page > 0) && (hitCount > 0)) { // calculate the final size of the search result start = rows * (page - 1); end = start + rows; // ensure that both i and n are inside the range of foundDocuments.size() start = new Long((start > hitCount) ? hitCount : start).intValue(); end = new Long((end > hitCount) ? hitCount : end).intValue(); } else { // return all found documents in the search result start = 0; end = new Long(hitCount).intValue(); } long visibleHitCount = hitCount; float maxScore = 0; // If we're using a postprocessor, (re-)initialize it before using it if (m_postProcessor != null) { m_postProcessor.init(); } // process found documents List<CmsSearchResource> allDocs = new ArrayList<CmsSearchResource>(); int cnt = 0; for (int i = 0; (i < queryResponse.getResults().size()) && (cnt < end); i++) { try { SolrDocument doc = queryResponse.getResults().get(i); CmsSolrDocument searchDoc = new CmsSolrDocument(doc); if (needsPermissionCheck(searchDoc)) { // only if the document is an OpenCms internal resource perform the permission check CmsResource resource = filter == null ? getResource(searchCms, searchDoc) : getResource(searchCms, searchDoc, filter); if (resource != null) { // permission check performed successfully: the user has read permissions! if (cnt >= start) { if (m_postProcessor != null) { doc = m_postProcessor.process(searchCms, resource, (SolrInputDocument) searchDoc.getDocument()); } resourceDocumentList.add(new CmsSearchResource(resource, searchDoc)); if (null != doc) { solrDocumentList.add(doc); } maxScore = maxScore < searchDoc.getScore() ? searchDoc.getScore() : maxScore; } allDocs.add(new CmsSearchResource(resource, searchDoc)); cnt++; } else { visibleHitCount--; } } else { // if permission check is not required for this index, // add a pseudo resource together with document to the results resourceDocumentList.add(new CmsSearchResource(PSEUDO_RES, searchDoc)); solrDocumentList.add(doc); maxScore = maxScore < searchDoc.getScore() ? searchDoc.getScore() : maxScore; cnt++; } } catch (Exception e) { // should not happen, but if it does we want to go on with the next result nevertheless LOG.warn(Messages.get().getBundle().key(Messages.LOG_SOLR_ERR_RESULT_ITERATION_FAILED_0), e); } } // the last documents were all secret so let's take the last found docs if (resourceDocumentList.isEmpty() && (allDocs.size() > 0)) { page = Math.round(allDocs.size() / rows) + 1; int showCount = allDocs.size() % rows; showCount = showCount == 0 ? rows : showCount; start = allDocs.size() - new Long(showCount).intValue(); end = allDocs.size(); if (allDocs.size() > start) { resourceDocumentList = allDocs.subList(start, end); for (CmsSearchResource r : resourceDocumentList) { maxScore = maxScore < r.getDocument().getScore() ? r.getDocument().getScore() : maxScore; solrDocumentList.add(((CmsSolrDocument) r.getDocument()).getSolrDocument()); } } } long processTime = System.currentTimeMillis() - startTime - solrTime; // create and return the result solrDocumentList.setStart(start); solrDocumentList.setMaxScore(new Float(maxScore)); solrDocumentList.setNumFound(visibleHitCount); queryResponse.getResponse().setVal(queryResponse.getResponse().indexOf(QUERY_RESPONSE_NAME, 0), solrDocumentList); queryResponse.getResponseHeader().setVal(queryResponse.getResponseHeader().indexOf(QUERY_TIME_NAME, 0), new Integer(new Long(System.currentTimeMillis() - startTime).intValue())); long highlightEndTime = System.currentTimeMillis(); SolrCore core = m_solr instanceof EmbeddedSolrServer ? ((EmbeddedSolrServer) m_solr).getCoreContainer().getCore(getCoreName()) : null; CmsSolrResultList result = null; try { SearchComponent highlightComponenet = null; if (core != null) { highlightComponenet = core.getSearchComponent("highlight"); solrQueryRequest = new LocalSolrQueryRequest(core, queryResponse.getResponseHeader()); } SolrQueryResponse solrQueryResponse = null; if (solrQueryRequest != null) { // create and initialize the solr response solrQueryResponse = new SolrQueryResponse(); solrQueryResponse.setAllValues(queryResponse.getResponse()); int paramsIndex = queryResponse.getResponseHeader().indexOf(HEADER_PARAMS_NAME, 0); NamedList<Object> header = null; Object o = queryResponse.getResponseHeader().getVal(paramsIndex); if (o instanceof NamedList) { header = (NamedList<Object>) o; header.setVal(header.indexOf(CommonParams.ROWS, 0), new Integer(rows)); header.setVal(header.indexOf(CommonParams.START, 0), new Long(start)); } // set the OpenCms Solr query as parameters to the request solrQueryRequest.setParams(initQuery); // perform the highlighting if ((header != null) && (initQuery.getHighlight()) && (highlightComponenet != null)) { header.add(HighlightParams.HIGHLIGHT, "on"); if ((initQuery.getHighlightFields() != null) && (initQuery.getHighlightFields().length > 0)) { header.add(HighlightParams.FIELDS, CmsStringUtil.arrayAsString(initQuery.getHighlightFields(), ",")); } String formatter = initQuery.getParams(HighlightParams.FORMATTER) != null ? initQuery.getParams(HighlightParams.FORMATTER)[0] : null; if (formatter != null) { header.add(HighlightParams.FORMATTER, formatter); } if (initQuery.getHighlightFragsize() != 100) { header.add(HighlightParams.FRAGSIZE, new Integer(initQuery.getHighlightFragsize())); } if (initQuery.getHighlightRequireFieldMatch()) { header.add(HighlightParams.FIELD_MATCH, new Boolean(initQuery.getHighlightRequireFieldMatch())); } if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(initQuery.getHighlightSimplePost())) { header.add(HighlightParams.SIMPLE_POST, initQuery.getHighlightSimplePost()); } if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(initQuery.getHighlightSimplePre())) { header.add(HighlightParams.SIMPLE_PRE, initQuery.getHighlightSimplePre()); } if (initQuery.getHighlightSnippets() != 1) { header.add(HighlightParams.SNIPPETS, new Integer(initQuery.getHighlightSnippets())); } ResponseBuilder rb = new ResponseBuilder(solrQueryRequest, solrQueryResponse, Collections.singletonList(highlightComponenet)); try { rb.doHighlights = true; DocListAndSet res = new DocListAndSet(); SchemaField idField = OpenCms.getSearchManager().getSolrServerConfiguration() .getSolrSchema().getUniqueKeyField(); int[] luceneIds = new int[rows]; int docs = 0; for (SolrDocument doc : solrDocumentList) { String idString = (String) doc.getFirstValue(CmsSearchField.FIELD_ID); int id = solrQueryRequest.getSearcher().getFirstMatch( new Term(idField.getName(), idField.getType().toInternal(idString))); luceneIds[docs++] = id; } res.docList = new DocSlice(0, docs, luceneIds, null, docs, 0); rb.setResults(res); rb.setQuery(QParser.getParser(initQuery.getQuery(), null, solrQueryRequest).getQuery()); rb.setQueryString(initQuery.getQuery()); highlightComponenet.prepare(rb); highlightComponenet.process(rb); highlightComponenet.finishStage(rb); } catch (Exception e) { LOG.error(e.getMessage() + " in query: " + initQuery, new Exception(e)); } // Make highlighting also available via the CmsSolrResultList queryResponse.setResponse(solrQueryResponse.getValues()); highlightEndTime = System.currentTimeMillis(); } } result = new CmsSolrResultList(initQuery, queryResponse, solrDocumentList, resourceDocumentList, start, new Integer(rows), end, page, visibleHitCount, new Float(maxScore), startTime, highlightEndTime); if (LOG.isDebugEnabled()) { Object[] logParams = new Object[] { new Long(System.currentTimeMillis() - startTime), new Long(result.getNumFound()), new Long(solrTime), new Long(processTime), 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)); } if (response != null) { writeResp(response, solrQueryRequest, solrQueryResponse); } } finally { if (solrQueryRequest != null) { solrQueryRequest.close(); } if (core != null) { core.close(); } } return result; } catch (Exception e) { throw new CmsSearchException(Messages.get().container(Messages.LOG_SOLR_ERR_SEARCH_EXECUTION_FAILD_1, CmsEncoder.decode(query.toString()), e), e); } finally { if (solrQueryRequest != null) { solrQueryRequest.close(); } // re-set thread to previous priority Thread.currentThread().setPriority(previousPriority); } }
From source file:org.opencommercesearch.RuleManager.java
License:Apache License
void setRuleParams(SolrQuery query, boolean isSearch, boolean isRuleBasedPage, String categoryPath, FilterQuery[] filterQueries, RepositoryItem catalog, boolean isOutletPage, String brandId) throws RepositoryException, SolrServerException { if (getRules() == null) { String categoryFilterQuery = extractCategoryFilterQuery(filterQueries); String includeExp[] = (String[]) query.getParams("includeRules"); String excludeExp[] = (String[]) query.getParams("excludeRules"); Set<String> includeExperiments = new HashSet<String>(); if (includeExp != null) { includeExperiments = new HashSet<String>(Arrays.asList(includeExp)); }/*from w ww .j a v a 2 s. co m*/ Set<String> excludeExperiments = new HashSet<String>(); if (excludeExp != null) { excludeExperiments = new HashSet<String>(Arrays.asList(excludeExp)); } loadRules(query.getQuery(), categoryPath, categoryFilterQuery, isSearch, isRuleBasedPage, catalog, isOutletPage, brandId, includeExperiments, excludeExperiments); } setRuleParams(query, getRules()); setFilterQueries(filterQueries, catalog.getRepositoryId(), query); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testConstructSolrQueryWithFacetPrefix() { FacetQuery query = new SimpleFacetQuery(new Criteria("field_1").is("value_1")); FacetOptions facetOptions = new FacetOptions(new SimpleField("facet_1"), new SimpleField("facet_2")); facetOptions.setFacetPrefix("prefix"); query.setFacetOptions(facetOptions); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertNotNull(solrQuery);// w ww. j a va 2s . c o m assertQueryStringPresent(solrQuery); assertPaginationNotPresent(solrQuery); assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1", "facet_2"); Assert.assertEquals(facetOptions.getFacetPrefix(), solrQuery.getParams("facet.prefix")[0]); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testConstructSolrQueryWithFieldFacetParameters() { FacetQuery query = new SimpleFacetQuery(new Criteria("field_1").is("value_1")); FieldWithFacetParameters fieldWithFacetParameters = new FieldWithFacetParameters("facet_2") .setPrefix("prefix").setSort(FacetSort.INDEX).setLimit(3).setOffset(2).setMethod("method") .setMissing(true);/*from ww w. j ava 2s . c om*/ FacetOptions facetOptions = new FacetOptions(new SimpleField("facet_1"), fieldWithFacetParameters); query.setFacetOptions(facetOptions); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertNotNull(solrQuery); assertQueryStringPresent(solrQuery); assertPaginationNotPresent(solrQuery); assertProjectionNotPresent(solrQuery); assertGroupingNotPresent(solrQuery); assertFactingPresent(solrQuery, "facet_1", "facet_2"); Assert.assertEquals(fieldWithFacetParameters.getPrefix(), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.prefix")[0]); Assert.assertEquals(FacetParams.FACET_SORT_INDEX, solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.sort")[0]); Assert.assertEquals(Integer.toString(fieldWithFacetParameters.getOffset()), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.offset")[0]); Assert.assertEquals(Integer.toString(fieldWithFacetParameters.getLimit()), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.limit")[0]); Assert.assertEquals(fieldWithFacetParameters.getMethod(), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.method")[0]); Assert.assertEquals(fieldWithFacetParameters.getMissing().toString(), solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.missing")[0]); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testConstructSolrQueryWithCustomFieldFacetParameters() { FacetQuery query = new SimpleFacetQuery(new Criteria("field_1").is("value_1")); FieldWithFacetParameters fieldWithFacetParameters = new FieldWithFacetParameters("facet_2") .addFacetParameter(new FacetParameter(FacetParams.FACET_ZEROS, "on")); FacetOptions facetOptions = new FacetOptions(new SimpleField("facet_1"), fieldWithFacetParameters); query.setFacetOptions(facetOptions); SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals("on", solrQuery.getParams("f." + fieldWithFacetParameters.getName() + ".facet.zeros")[0]); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testConstructSorlQueryWithHighlightOptionFormatter() { SimpleHighlightQuery query = new SimpleHighlightQuery(new SimpleStringCriteria("field_1:value_1")); HighlightOptions options = new HighlightOptions(); options.setFormatter("formatter"); query.setHighlightOptions(options);//from www . j a v a 2s .c om SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertEquals(options.getFormatter(), solrQuery.getParams(HighlightParams.FORMATTER)[0]); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
@Test public void testConstructSorlQueryWithFieldSpecificHighlightOptions() { SimpleHighlightQuery query = new SimpleHighlightQuery(new SimpleStringCriteria("field_1:value_1")); HighlightOptions options = new HighlightOptions(); HighlightOptions.FieldWithHighlightParameters fieldWithHighlightParameters = new HighlightOptions.FieldWithHighlightParameters( "field_2"); fieldWithHighlightParameters.setFormatter("formatter"); fieldWithHighlightParameters.setFragsize(10); options.addField(fieldWithHighlightParameters); query.setHighlightOptions(options);/*from w w w . j a v a 2 s . c o m*/ SolrQuery solrQuery = queryParser.constructSolrQuery(query); Assert.assertArrayEquals(new String[] { "field_2" }, solrQuery.getHighlightFields()); Assert.assertEquals(fieldWithHighlightParameters.getFormatter(), solrQuery.getParams("f.field_2." + HighlightParams.FORMATTER)[0]); Assert.assertEquals(fieldWithHighlightParameters.getFragsize().toString(), solrQuery.getParams("f.field_2." + HighlightParams.FRAGSIZE)[0]); }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
/** * @see DATASOLR-160/*ww w .ja v a2s.c o m*/ */ @Test public void testConstructSolrQueryWithStatFields() { StatsOptions statsOptions = new StatsOptions()// .addField(new SimpleField("field_1"))// .addField(new SimpleField("field_2")); SimpleQuery query = new SimpleQuery("*:*"); query.setStatsOptions(statsOptions); SolrQuery solrQuery = queryParser.constructSolrQuery(query); List<String> fields = Arrays.asList(solrQuery.getParams(StatsParams.STATS_FIELD)); Collections.sort(fields); Assert.assertEquals(2, fields.size()); Assert.assertEquals(Arrays.asList("field_1", "field_2"), fields); }