List of usage examples for org.apache.solr.handler.component ResponseBuilder getQueryString
public String getQueryString()
From source file:com.billiger.solr.handler.component.QLTBComponent.java
License:Apache License
/** * Add boost terms to the query if it matches a know query. * * The query string is analyzed and compared to the known query strings * from the XML file. If a matching (i.e. equal) query string is found, * the associated terms (ConstantScoreQuery) are added: the original * query (object, not string) is added as a MUST term in a newly created * BooleanQuery, whereas the new terms are added either as Occur.SHOULD * for positive boost, or Occur.MUST_NOT for zero or negative boost. * * prepare() might trigger a reload of the XML file if it resides in * the data/ directory and the reader is new. * *///from w w w . j ava2 s . c o m @Override public final void prepare(final ResponseBuilder rb) { if (disabled(rb)) { return; } Query query = rb.getQuery(); String queryStr = rb.getQueryString(); if (query == null || queryStr == null) { return; } IndexReader reader = rb.req.getSearcher().getIndexReader(); List<Query> boostTerms = null; try { queryStr = getAnalyzedQuery(queryStr); boostTerms = getQLTBMap(reader, rb.req.getCore()).get(queryStr); if (boostTerms == null || boostTerms.isEmpty()) { return; } log.debug("QLTBComponent.prepare() query: \"" + queryStr + "\" with " + boostTerms.size() + " boost terms"); } catch (Exception ex) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "error loading QLTB", ex); } BooleanQuery newq = new BooleanQuery(true); newq.add(query, BooleanClause.Occur.MUST); for (Query term : boostTerms) { if (term.getBoost() > 0.0) { newq.add(new BooleanClause(term, BooleanClause.Occur.SHOULD)); } else { newq.add(new BooleanClause(term, BooleanClause.Occur.MUST_NOT)); } } rb.setQuery(newq); }
From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from ww w. j av a 2 s.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:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java
License:Apache License
@Override @SuppressWarnings("unchecked") public void process(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); // Date startTime =new Date(); String event = params.get("event"); String ids = params.get("ids"); String shardUrl = params.get("shard.url"); // filter the query warming clause and di request if (!((event != null && event.equals("firstSearcher")) || (ids != null))) { String[] shardUrlStrs = shardUrl.split("\\\\|")[0].split("/"); String collectionName = shardUrlStrs[shardUrlStrs.length - 1]; String queryText = rb.getQueryString(); String queryTextAnalyzed = queryText; // String queryTextAnalyzed = // AnalyzerUtils.analyze(queryText,collection); int searchPage = params.getInt("page", 0); String simpleSearchStr = getSimpleSearchStr(params); String simpleSearchId = getSimpleSearchId(simpleSearchStr); String searchLogDocId = generateSearchLogDocId(simpleSearchId); String simpleSearchQueryAnalyzedStr = params.toString(); // String simpleSearchQueryAnalyzedStr // =getSimpleSearchStr(simpleSearch, true); long timeCost = rb.rsp.getEndTime() - rb.req.getStartTime(); SolrInputDocument doc = new SolrInputDocument(); doc.setField(StatsConstants.INDEX_FIELD_ID, searchLogDocId); doc.setField(StatsConstants.INDEX_FIELD_COLLECTION_NAME, collectionName); doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_ID, simpleSearchId); doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH, simpleSearchStr); doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_QUERY_ANALYZED, simpleSearchQueryAnalyzedStr); doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT, queryText); doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT_ANALYZED, queryTextAnalyzed); doc.setField(StatsConstants.INDEX_FIELD_NUM_FOUND, rb.getNumberDocumentsFound()); doc.setField(StatsConstants.INDEX_FIELD_RESPONSE_TIME, timeCost); doc.setField(StatsConstants.INDEX_FIELD_SEARCH_DATE, new Date()); doc.setField(StatsConstants.INDEX_FIELD_SEARCH_PAGE, searchPage); try {// w w w .j a va 2s .c o m // searchLogCache.add(doc); // if (searchLogCache.size() >= commitThreshold) { int port = Integer.parseInt(shardUrl.substring(shardUrl.indexOf(":") + 1, shardUrl.indexOf("/"))); if (searchLogServer == null || localPort != port) { localPort = port; searchLogServer = new HttpSolrServer( "http://localhost:" + localPort + "/solr/" + searchLogCoreName); } // searchLogServer.add(searchLogCache); searchLogServer.add(doc); searchLogServer.commit(); // searchLogCache.clear(); // } } catch (SolrServerException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.out.println("premier phase:"+timeCost); } // Date endTime =new Date(); // System.out.println("total time:"+(endTime.getTime()-startTime.getTime())); }
From source file:lux.solr.AppServerComponent.java
License:Mozilla Public License
@Override public void prepare(ResponseBuilder rb) throws IOException { SolrQueryRequest req = rb.req;/*ww w . j a v a2 s . c o m*/ SolrParams params = req.getParams(); if (rb.getQueryString() == null) { queryPath = rb.req.getParams().get(LUX_XQUERY); if (!StringUtils.isBlank(queryPath)) { String baseUri; String contextBase = (String) params.get("lux.serverBaseUri"); if (params.get("lux.baseUri") != null) { baseUri = (String) params.get("lux.baseUri"); } else if (params.get("lux.serverBaseUri") != null) { baseUri = contextBase; } else { baseUri = ""; } if (File.separatorChar == '\\') { baseUri = baseUri.replace('\\', '/'); } if (!baseUri.endsWith("/")) { // add trailing slash baseUri = baseUri + '/'; } if (baseUri.startsWith("/") || (File.separatorChar == '\\' && baseUri.matches("^[A-Za-z]:/.*$"))) { baseUri = "file://" + baseUri; } //System.out.println ("BASE URI = " + baseUri); String resourceBase = null; if (baseUri.startsWith(RESOURCE_SCHEME)) { resourceBase = baseUri.substring(RESOURCE_SCHEME.length()); } else if (baseUri.startsWith(CONTEXT_SCHEME)) { baseUri = contextBase + baseUri.substring(CONTEXT_SCHEME.length()); } String contents = null; if (resourceBase != null) { InputStream in = AppServerComponent.class.getResourceAsStream(resourceBase + queryPath); queryPath = baseUri + queryPath; if (in == null) { throw new SolrException(ErrorCode.NOT_FOUND, queryPath + " not found"); } else { try { contents = IOUtils.toString(in); } catch (IOException e) { LoggerFactory.getLogger(AppServerComponent.class) .error("An error occurred while reading " + queryPath, e); } IOUtils.closeQuietly(in); } } else { // url provided with scheme queryPath = baseUri + queryPath; URL url = new URL(queryPath); String scheme = url.getProtocol(); if (scheme.equals("lux")) { // TODO implement lux: uri resolution throw new SolrException(ErrorCode.NOT_FOUND, queryPath + " not found (actually lux: scheme is not implemented)"); } else { InputStream in = null; try { if (url.getProtocol().equals("file")) { File f = new File(url.getPath()); if (!f.exists()) { throw new SolrException(ErrorCode.NOT_FOUND, f + " not found"); } if (f.isDirectory() || !f.canRead()) { throw new SolrException(ErrorCode.FORBIDDEN, "access to " + f + " denied by rule"); } in = new FileInputStream(f); } else { // in = url.openStream(); LoggerFactory.getLogger(AppServerComponent.class) .error("URL scheme not supported: " + url.getProtocol()); } contents = IOUtils.toString(in); } catch (IOException e) { LoggerFactory.getLogger(AppServerComponent.class) .error("An error occurred while reading " + url, e); } if (in != null) { IOUtils.closeQuietly(in); } } } rb.setQueryString(contents); } } super.prepare(rb); }
From source file:lux.solr.XQueryComponent.java
License:Mozilla Public License
@Override public void prepare(ResponseBuilder rb) throws IOException { if (searchHandler == null) { // bleah -- we need a link to the search handler to pass down in to the bowels of // XQuery evaluation so we can recurse when we come to a search call. To get that, // we can only traverse the core registry, but due to order of initialization, the // handler won't have been linked to this component until after all the inform() calls // are done. // A possible alternative here would be to write our own search handler that extends // the Solr one and adds itself to the ResponseBuilder... findSearchHandler();/*from w w w.jav a2 s . com*/ } SolrQueryRequest req = rb.req; SolrParams params = req.getParams(); if (rb.getQueryString() == null) { rb.setQueryString(params.get(CommonParams.Q)); } String contentType = params.get("lux.contentType"); // TODO: make this a local variable in or near #addResult, not an instance variable: it's not threadsafe serializer = solrIndexConfig.checkoutSerializer(); if (contentType != null) { if (contentType.equals("text/html")) { serializer.setOutputProperty(Serializer.Property.METHOD, "html"); } else if (contentType.equals("text/xml")) { serializer.setOutputProperty(Serializer.Property.METHOD, "xml"); } } else { serializer.setOutputProperty(Serializer.Property.METHOD, getDefaultSerialization()); } if (queryPath == null) { // allow subclasses to override... queryPath = rb.req.getParams().get(LUX_XQUERY); } resultByteSize = 0; }
From source file:lux.solr.XQueryComponent.java
License:Mozilla Public License
protected void evaluateQuery(ResponseBuilder rb, int start, int len) { String query = rb.getQueryString(); SolrQueryRequest req = rb.req;//from www.j a v a 2s.c o m SolrQueryResponse rsp = rb.rsp; if (StringUtils.isBlank(query)) { rsp.add("xpath-error", "query was blank"); return; } SolrParams params = req.getParams(); long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1); XQueryExecutable expr; LuxSearcher searcher = new LuxSearcher(rb.req.getSearcher()); DocWriter docWriter = new SolrDocWriter(this, rb.req.getCore()); Compiler compiler = solrIndexConfig.getCompiler(); SolrQueryContext context = new SolrQueryContext(this, req); if (rb.shards != null && rb.req.getParams().getBool("distrib", true)) { // This is a distributed request; pass in the ResponseBuilder so it will be // available to a subquery. context.setResponseBuilder(rb); // also capture the current set of shards shards = rb.shards; slices = rb.slices; } SolrSearchService searchService = new SolrSearchService(context, new LuxSearchQueryParser()); Evaluator eval = new Evaluator(compiler, searcher, docWriter, searchService); // track which evaluator we are using in a threadlocal container evalHolder.set(eval); TransformErrorListener errorListener = eval.getErrorListener(); try { URI baseURI = queryPath == null ? null : java.net.URI.create(queryPath); expr = compiler.compile(query, errorListener, baseURI, null); } catch (LuxException ex) { // ex.printStackTrace(); String err = formatError(query, errorListener); if (StringUtils.isEmpty(err)) { err = ex.getMessage(); } rsp.add("xpath-error", err); // don't close: this forces a commit() // evaluator.close(); return; } // SolrIndexSearcher.QueryResult result = new // SolrIndexSearcher.QueryResult(); NamedList<Object> xpathResults = new NamedList<Object>(); long tstart = System.currentTimeMillis(); int count = 0; bindRequestVariables(rb, req, expr, compiler, eval, context); Iterator<XdmItem> queryResults = eval.iterator(expr, context); String err = null; while (queryResults.hasNext()) { XdmItem xpathResult = queryResults.next(); if (++count < start) { continue; } if (count == 1 && !xpathResult.isAtomicValue()) { net.sf.saxon.s9api.QName name = ((XdmNode) xpathResult).getNodeName(); if (name != null && name.getNamespaceURI().equals(EXPATH_HTTP_NS) && name.getLocalName().equals("response")) { err = handleEXPathResponse(req, rsp, xpathResults, xpathResult); if (queryResults.hasNext()) { logger.warn( "Ignoring results following http:response, which should be the sole item in its result"); } break; } } err = safeAddResult(xpathResults, xpathResult); if (err != null) { xpathResult = null; break; } if ((len > 0 && xpathResults.size() >= len) || (timeAllowed > 0 && (System.currentTimeMillis() - tstart) > timeAllowed)) { break; } } ArrayList<TransformerException> errors = eval.getErrorListener().getErrors(); if (!errors.isEmpty()) { err = formatError(query, errors, eval.getQueryStats()); if (xpathResults.size() == 0) { xpathResults = null; // throw a 400 error; don't return partial // results } } if (err != null) { rsp.add("xpath-error", err); } if (rb.getResults() == null) { // create a dummy doc list if previous query processing didn't retrieve any docs // In distributed operation, there will be doc results, otherwise none. SolrIndexSearcher.QueryResult result = new SolrIndexSearcher.QueryResult(); result.setDocList(new DocSlice(0, 0, null, null, eval.getQueryStats().docCount, 0)); rb.setResult(result); rsp.add("response", rb.getResults().docList); } if (xpathResults != null) { rsp.add("xpath-results", xpathResults); if (logger.isDebugEnabled()) { logger.debug("retrieved: " + eval.getDocReader().getCacheMisses() + " docs, " + xpathResults.size() + " results, " + (System.currentTimeMillis() - tstart) + "ms"); } } else { logger.warn("xquery evaluation error: " + eval.getDocReader().getCacheMisses() + " docs, " + "0 results, " + (System.currentTimeMillis() - tstart) + "ms"); } if (err == null && context.isCommitPending()) { doCommit(); } }
From source file:net.sr_sl.solr.ssq.SsqQueryComponent.java
License:Apache License
/** * Modify the query parameters//from www . j ava 2 s .co m */ private boolean modifyQueryRequest(ResponseBuilder rb) throws IOException { SolrQueryRequest req = rb.req; SolrParams params = req.getParams(); // check whether server side queries is active for this request if (!params.getBool(SSQ_PREFIX, false)) return false; // get parameters to use String ssqQuery = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY)); String ssqParam = params.get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_PARAM), SSQ_PARAM_DFT); // when ssqQuery or ssqParam is not set, don't modify if (ssqQuery == null || ssqQuery.isEmpty() || ssqParam.isEmpty()) return false; // Get original value for ssqParam and return when already set String ssqParamVal = params.get(ssqParam); if (ssqParamVal != null && !ssqParamVal.isEmpty()) return false; // Get original query string value String origQueryString = rb.getQueryString(); String origQVal = req.getOriginalParams().get(CommonParams.Q); // Retrieve value to use as query-term; when empty, use q.alt String qVal = origQVal; if (qVal == null || qVal.isEmpty()) { String alt_q = params.get(DisMaxParams.ALTQ); if (alt_q != null && !alt_q.isEmpty()) { qVal = alt_q; } } // Get value for ssqQuery String ssqQueryVal = params .get(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_DELIM).concat(ssqQuery)); // When value not found, assume that ssqQuery is the query to execute // per default if (ssqQueryVal == null || ssqQueryVal.isEmpty()) ssqQueryVal = ssqQuery; // Perform replacement ModifiableSolrParams mparams = new ModifiableSolrParams(); // Set flag to indicate that replacement is performed mparams.set(SSQ_PREFIX.concat(SSQ_APPLIED_SUFFIX), Boolean.toString(true)); // Store original querystring when <> q if (origQVal != null && !origQVal.equals(origQueryString)) mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERYSTRING).concat(SSQ_APPLIED_SUFFIX), origQueryString); // Perform the switch (qVal --> ssqParam) mparams.set(ssqParam, qVal); mparams.set(SSQ_PREFIX.concat(SSQ_DELIM).concat(SSQ_QUERY).concat(SSQ_APPLIED_SUFFIX), ssqQueryVal); // set the extra parameters req.setParams(SolrParams.wrapAppended(req.getParams(), mparams)); // set queryString to query rb.setQueryString(ssqQueryVal); return true; }
From source file:opennlp.tools.similarity.apps.solr.IterativeQueryComponent.java
License:Apache License
private ResponseBuilder substituteField(ResponseBuilder rb, String newFieldName) { SolrParams params = rb.req.getParams(); String query = params.get("q"); String currField = StringUtils.substringBetween(" " + query, " ", ":"); if (currField != null && newFieldName != null) query = query.replace(currField, newFieldName); NamedList values = params.toNamedList(); values.remove("q"); values.add("q", query); params = SolrParams.toSolrParams(values); rb.req.setParams(params);//from w ww . j av a 2 s .c o m rb.setQueryString(query); String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE); // get it from the response builder to give a different component a chance // to set it. String queryString = rb.getQueryString(); if (queryString == null) { // this is the normal way it's set. queryString = params.get(CommonParams.Q); rb.setQueryString(queryString); } QParser parser = null; try { parser = QParser.getParser(rb.getQueryString(), defType, rb.req); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Query q = null; try { q = parser.getQuery(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (q == null) { // normalize a null query to a query that matches nothing q = new BooleanQuery(); } rb.setQuery(q); try { rb.setSortSpec(parser.getSort(true)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } rb.setQparser(parser); /* try { rb.setScoreDoc(parser.getPaging()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } */ String[] fqs = rb.req.getParams().getParams(CommonParams.FQ); if (fqs != null && fqs.length != 0) { List<Query> filters = rb.getFilters(); if (filters == null) { filters = new ArrayList<Query>(fqs.length); } for (String fq : fqs) { if (fq != null && fq.trim().length() != 0) { QParser fqp = null; try { fqp = QParser.getParser(fq, null, rb.req); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { filters.add(fqp.getQuery()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // only set the filters if they are not empty otherwise // fq=&someotherParam= will trigger all docs filter for every request // if filter cache is disabled if (!filters.isEmpty()) { rb.setFilters(filters); } } return rb; }
From source file:org.alfresco.solr.component.AsyncBuildSuggestComponent.java
License:Open Source License
/** * Responsible for using the specified suggester to get the suggestions * for the query and write the results // ww w .ja v a 2 s.c o m * */ @Override public void process(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); LOG.debug("SuggestComponent process with : " + params); if (!params.getBool(COMPONENT_NAME, false) || suggesters.isEmpty()) { return; } boolean buildAll = params.getBool(SUGGEST_BUILD_ALL, false); boolean reloadAll = params.getBool(SUGGEST_RELOAD_ALL, false); Set<SolrSuggester> querySuggesters; try { querySuggesters = getSuggesters(params); } catch (IllegalArgumentException ex) { if (!buildAll && !reloadAll) { throw ex; } else { querySuggesters = new HashSet<>(); } } String query = params.get(SUGGEST_Q); if (query == null) { query = rb.getQueryString(); if (query == null) { query = params.get(CommonParams.Q); } } if (query != null) { int count = params.getInt(SUGGEST_COUNT, 1); boolean highlight = params.getBool(SUGGEST_HIGHLIGHT, false); boolean allTermsRequired = params.getBool(SUGGEST_ALL_TERMS_REQUIRED, true); String contextFilter = params.get(SUGGEST_CONTEXT_FILTER_QUERY); if (contextFilter != null) { contextFilter = contextFilter.trim(); if (contextFilter.length() == 0) { contextFilter = null; } } SuggesterOptions options = new SuggesterOptions(new CharsRef(query), count, contextFilter, allTermsRequired, highlight); Map<String, SimpleOrderedMap<NamedList<Object>>> namedListResults = new HashMap<>(); for (SolrSuggester suggester : querySuggesters) { SuggesterResult suggesterResult = suggester.getSuggestions(options); toNamedList(suggesterResult, namedListResults); } rb.rsp.add(SuggesterResultLabels.SUGGEST, namedListResults); } }
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 w w w . j a v a 2 s . c o m*/ } } 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; }