List of usage examples for org.apache.solr.handler.component ResponseBuilder getQparser
public QParser getQparser()
From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*w w w . j a va 2s .c o m*/ public void prepare(ResponseBuilder rb) throws IOException { SolrQueryRequest req = rb.req; SolrIndexSearcher searcher = req.getSearcher(); //IndexReader reader = req.getSearcher().getReader(); SolrParams params = req.getParams(); // A runtime param can skip if (!params.getBool(ENABLE, true)) { return; } Query query = rb.getQuery(); String qstr = rb.getQueryString(); if (query == null || qstr == null) { return; } ConstellioUser user; String userIdStr = params.get(ConstellioSolrQueryParams.USER_ID); if (userIdStr != null) { UserServices userServices = ConstellioSpringUtils.getUserServices(); try { user = userServices.get(new Long(userIdStr)); } catch (NumberFormatException e) { user = null; } } else { user = null; } String collectionName = params.get(ConstellioSolrQueryParams.COLLECTION_NAME); RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); FederationServices federationServices = ConstellioSpringUtils.getFederationServices(); RecordCollection collection = collectionServices.get(collectionName); List<TermQuery> restrictedCollectionQueries = new ArrayList<TermQuery>(); if (collection.isFederationOwner()) { List<RecordCollection> includedCollections = federationServices.listIncludedCollections(collection); for (RecordCollection includedCollection : includedCollections) { if (includedCollection.hasSearchPermission() && (user == null || !user.hasSearchPermission(includedCollection))) { restrictedCollectionQueries.add(new TermQuery( new Term(IndexField.COLLECTION_ID_FIELD, "" + includedCollection.getId()))); } } } // User must be logged in to see private records if (user != null) { String luceneQueryStr = params.get(ConstellioSolrQueryParams.LUCENE_QUERY); if (StringUtils.isBlank(luceneQueryStr)) { return; } IndexSchema schema = req.getSchema(); SolrQueryParser queryParser = new SolrQueryParser(rb.getQparser(), IndexField.DEFAULT_SEARCH_FIELD); Query luceneQuery; try { luceneQuery = queryParser.parse(luceneQueryStr); } catch (SyntaxError e) { log.error("Error parsing lucene query " + luceneQueryStr, e); return; } // Create a new query which will only include private records BooleanQuery privateRecordQuery = new BooleanQuery(true); privateRecordQuery.add(luceneQuery, BooleanClause.Occur.MUST); for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) { privateRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT); } TermQuery privateRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "F")); privateRecordQuery.add(privateRecordTQ, BooleanClause.Occur.MUST); DocSet privateRecordIdDocSet = searcher.getDocSet(privateRecordQuery); if (privateRecordIdDocSet.size() > 0) { RecordServices recordServices = ConstellioSpringUtils.getRecordServices(); ACLServices aclServices = ConstellioSpringUtils.getACLServices(); ConnectorManagerServices connectorManagerServices = ConstellioSpringUtils .getConnectorManagerServices(); List<Record> privateRecords = new ArrayList<Record>(); DocIterator docIt = privateRecordIdDocSet.iterator(); while (docIt.hasNext()) { int docId = docIt.nextDoc(); Document luceneDoc = searcher.doc(docId); Long recordId = new Long(luceneDoc.get(IndexField.RECORD_ID_FIELD)); Record record = recordServices.get(recordId, collection); privateRecords.add(record); } // First pass : Remove ACL authorized records List<Record> unevaluatedPrivateRecords = aclServices.removeAuthorizedRecords(privateRecords, user); if (!unevaluatedPrivateRecords.isEmpty()) { Set<UserCredentials> userCredentials = user.getUserCredentials(); // Second pass : Ask the connector manager ConnectorManager connectorManager = connectorManagerServices.getDefaultConnectorManager(); List<Record> authorizedRecords = connectorManagerServices .authorizeByConnector(unevaluatedPrivateRecords, userCredentials, connectorManager); List<Record> unauthorizedRecords = ListUtils.removeAll(unevaluatedPrivateRecords, authorizedRecords); if (!unauthorizedRecords.isEmpty()) { // Create a new query which will exclude unauthorized records BooleanQuery authorizedRecordQuery = new BooleanQuery(true); authorizedRecordQuery.add(query, BooleanClause.Occur.MUST); for (Record unauthorizedRecord : unauthorizedRecords) { TermQuery unauthorizedRecordTQ = new TermQuery( new Term(IndexField.RECORD_ID_FIELD, "" + unauthorizedRecord.getId())); authorizedRecordQuery.add(unauthorizedRecordTQ, BooleanClause.Occur.MUST_NOT); } rb.setQuery(authorizedRecordQuery); } } } } else { BooleanQuery publicRecordQuery = new BooleanQuery(true); publicRecordQuery.add(query, BooleanClause.Occur.MUST); TermQuery publicRecordTQ = new TermQuery(new Term(IndexField.PUBLIC_RECORD_FIELD, "T")); publicRecordQuery.add(publicRecordTQ, BooleanClause.Occur.MUST); for (TermQuery restrictionCollectionQuery : restrictedCollectionQueries) { publicRecordQuery.add(restrictionCollectionQuery, BooleanClause.Occur.MUST_NOT); } rb.setQuery(publicRecordQuery); } }
From source file:org.alfresco.solr.component.spellcheck.AlfrescoSpellCheckCollator.java
License:Open Source License
public List<AlfrescoSpellCheckCollation> collate(SpellingResult result, String originalQuery, ResponseBuilder ultimateResponse) { List<AlfrescoSpellCheckCollation> collations = new ArrayList<>(); QueryComponent queryComponent = null; if (ultimateResponse.components != null) { for (SearchComponent sc : ultimateResponse.components) { if (sc instanceof QueryComponent) { queryComponent = (QueryComponent) sc; break; }//from www . j a va2 s. c om } } boolean verifyCandidateWithQuery = true; int maxTries = maxCollationTries; int maxNumberToIterate = maxTries; if (maxTries < 1) { maxTries = 1; maxNumberToIterate = maxCollations; verifyCandidateWithQuery = false; } if (queryComponent == null && verifyCandidateWithQuery) { LOG.info( "Could not find an instance of QueryComponent. Disabling collation verification against the index."); maxTries = 1; verifyCandidateWithQuery = false; } docCollectionLimit = docCollectionLimit > 0 ? docCollectionLimit : 0; int maxDocId = -1; if (verifyCandidateWithQuery && docCollectionLimit > 0) { IndexReader reader = ultimateResponse.req.getSearcher().getIndexReader(); maxDocId = reader.maxDoc(); } JSONObject alfrescoJSON = (JSONObject) ultimateResponse.req.getContext().get(AbstractQParser.ALFRESCO_JSON); String originalAftsQuery = alfrescoJSON != null ? alfrescoJSON.getString("query") : ultimateResponse.getQueryString(); int tryNo = 0; int collNo = 0; PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions(), maxNumberToIterate, maxCollationEvaluations, suggestionsMayOverlap); while (tryNo < maxTries && collNo < maxCollations && possibilityIter.hasNext()) { PossibilityIterator.RankedSpellPossibility possibility = possibilityIter.next(); String collationQueryStr = getCollation(originalQuery, possibility.corrections); int hits = 0; String aftsQuery = null; if (verifyCandidateWithQuery) { tryNo++; SolrQueryRequest req = ultimateResponse.req; SolrParams origParams = req.getParams(); ModifiableSolrParams params = new ModifiableSolrParams(origParams); Iterator<String> origParamIterator = origParams.getParameterNamesIterator(); int pl = SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE.length(); while (origParamIterator.hasNext()) { String origParamName = origParamIterator.next(); if (origParamName.startsWith(SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE) && origParamName.length() > pl) { String[] val = origParams.getParams(origParamName); if (val.length == 1 && val[0].length() == 0) { params.set(origParamName.substring(pl), (String[]) null); } else { params.set(origParamName.substring(pl), val); } } } // we don't set the 'q' param, as we'll pass the query via JSON. // params.set(CommonParams.Q, collationQueryStr); params.remove(CommonParams.START); params.set(CommonParams.ROWS, "" + docCollectionLimit); // we don't want any stored fields params.set(CommonParams.FL, "id"); // we'll sort by doc id to ensure no scoring is done. params.set(CommonParams.SORT, "_docid_ asc"); // If a dismax query, don't add unnecessary clauses for scoring params.remove(DisMaxParams.TIE); params.remove(DisMaxParams.PF); params.remove(DisMaxParams.PF2); params.remove(DisMaxParams.PF3); params.remove(DisMaxParams.BQ); params.remove(DisMaxParams.BF); // Collate testing does not support Grouping (see SOLR-2577) params.remove(GroupParams.GROUP); boolean useQStr = true; if (alfrescoJSON != null) { try { aftsQuery = originalAftsQuery.replaceAll(Pattern.quote(originalQuery), Matcher.quoteReplacement(collationQueryStr)); alfrescoJSON.put("query", aftsQuery); req.getContext().put(AbstractQParser.ALFRESCO_JSON, alfrescoJSON); useQStr = false; } catch (JSONException e) { LOG.warn("Exception trying to get/set the query from/to ALFRESCO_JSON.]" + e); } } else { aftsQuery = collationQueryStr; } req.setParams(params); // creating a request here... make sure to close it! ResponseBuilder checkResponse = new ResponseBuilder(req, new SolrQueryResponse(), Arrays.<SearchComponent>asList(queryComponent)); checkResponse.setQparser(ultimateResponse.getQparser()); checkResponse.setFilters(ultimateResponse.getFilters()); checkResponse.components = Arrays.<SearchComponent>asList(queryComponent); if (useQStr) { checkResponse.setQueryString(collationQueryStr); } try { queryComponent.prepare(checkResponse); if (docCollectionLimit > 0) { int f = checkResponse.getFieldFlags(); checkResponse.setFieldFlags(f |= SolrIndexSearcher.TERMINATE_EARLY); } queryComponent.process(checkResponse); hits = (Integer) checkResponse.rsp.getToLog().get("hits"); } catch (EarlyTerminatingCollectorException etce) { assert (docCollectionLimit > 0); assert 0 < etce.getNumberScanned(); assert 0 < etce.getNumberCollected(); if (etce.getNumberScanned() == maxDocId) { hits = etce.getNumberCollected(); } else { hits = (int) (((float) (maxDocId * etce.getNumberCollected())) / (float) etce.getNumberScanned()); } } catch (Exception e) { LOG.warn( "Exception trying to re-query to check if a spell check possibility would return any hits." + e); } finally { checkResponse.req.close(); } } if (hits > 0 || !verifyCandidateWithQuery) { collNo++; AlfrescoSpellCheckCollation collation = new AlfrescoSpellCheckCollation(); collation.setCollationQuery(aftsQuery); collation.setCollationQueryString(collationQueryStr); collation.setHits(hits); collation.setInternalRank( suggestionsMayOverlap ? ((possibility.rank * 1000) + possibility.index) : possibility.rank); NamedList<String> misspellingsAndCorrections = new NamedList<>(); for (SpellCheckCorrection corr : possibility.corrections) { misspellingsAndCorrections.add(corr.getOriginal().toString(), corr.getCorrection()); } collation.setMisspellingsAndCorrections(misspellingsAndCorrections); collations.add(collation); } if (LOG.isDebugEnabled()) { LOG.debug("Collation: " + aftsQuery + (verifyCandidateWithQuery ? (" will return " + hits + " hits.") : "")); } } return collations; }