List of usage examples for org.apache.solr.search SolrQueryParser SolrQueryParser
public SolrQueryParser(QParser parser, String defaultField)
From source file:com.doculibre.constellio.solr.handler.component.ConstellioAuthorizationComponent.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* w w w. j av a 2 s . co 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:eu.europeana.assets.service.ir.text.bm25f.parser.BM25FQParser.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w ww .j a v a 2s . co m public Query parse() throws ParseException { /* * SolrParams solrParams = localParams == null ? params : new * DefaultSolrParams(localParams, params); */ /* Main User Query */ String userQuery = getString(); /* String parsedUserQuery = null; */ /* 27/03/2012 current version of the plugin does not manage Boolean queries */ if (userQuery.contains(" AND ") || userQuery.contains(" OR ")) { QParser p = new LuceneQParserPlugin().createParser(qstr, localParams, params, req); return p.parse(); } String k1s = this.getParam("k1"); if (k1s != null) { float k1 = Float.parseFloat(k1s); bmParams.setK1(k1); } String boostss = this.getParam("boosts"); if (boostss != null) { float[] boosts = gson.fromJson(boostss, float[].class); bmParams.setBoosts(boosts); } String bParamss = this.getParam("b"); if (bParamss != null) { float[] bParams = gson.fromJson(bParamss, float[].class); bmParams.setbParams(bParams); } SolrQueryParser sqp = new SolrQueryParser(req.getSchema(), bmParams.getMainField()); Query q = sqp.parse(userQuery); if (q instanceof BooleanQuery) { List<BooleanClause> clauses = ((BooleanQuery) q).clauses(); if (clauses.isEmpty()) return q; for (BooleanClause c : clauses) { Set<Term> terms = new HashSet<Term>(); c.getQuery().extractTerms(terms); for (Term t : terms) { if (!t.field().equals(bmParams.getMainField())) { /* TODO manage different fields with bm25f */ /* * if the query is on fields different from the main, we * process it as a standard solr query */ return q; } } } /* * if I'm here, the query is a BooleanQuery on the default field, so * I can use bm25f */ BM25FBooleanQuery bm25fQuery; try { bm25fQuery = new BM25FBooleanQuery(userQuery, req.getSchema().getQueryAnalyzer(), bmParams); } catch (IOException e) { // TODO Auto-generated catch block log.info("Error during the parsing of the BM25F-query " + userQuery); System.err.println("Error during the parsing of the query " + userQuery); /* we manage the error returning a standard solr query */ return q; } return bm25fQuery; } if (q instanceof TermQuery) { TermQuery tq = (TermQuery) q; if (tq.getTerm().field().equals(bmParams.getMainField())) { try { return new BM25FBooleanQuery(tq.getTerm().text(), req.getSchema().getQueryAnalyzer(), bmParams); } catch (IOException e) { log.info("Error during the parsing of the BM25F-query " + userQuery); log.error("Error during the parsing of the query " + userQuery); return tq; } } return tq; } return q; }
From source file:eu.europeana.ranking.bm25f.BM25FQueryParser.java
License:Apache License
@Override public Query parse() throws ParseException { BM25FParameters param = manageRuntimeBm25fParams(); if (param == null) param = bm25fparams;/*from w w w. ja v a 2s . c om*/ String[] fields = bm25fparams.getFields(); SolrQueryParser parser = new SolrQueryParser(this, mainField); parser.setDefaultOperator(Operator.AND); Query q = parser.parse(qstr); // if (q instanceof DisjunctionMaxQuery ){ // return new BM25FQuery((DisjunctionMaxQuery)q); // } if (q instanceof BooleanQuery) { BooleanQuery bq = (BooleanQuery) q; BM25FBooleanQuery bm25fQuery = new BM25FBooleanQuery(param); Set<Term> terms = new HashSet<Term>(); bm25fQuery.extractTerms(terms); for (Term t : terms) { // if is using external fields if (indexOf(t.field(), fields) < 0) { return bq; } } for (BooleanClause clause : bq) { Occur occur = clause.getOccur(); if (occur == Occur.SHOULD || !(clause.getQuery() instanceof TermQuery)) { return bq; } bm25fQuery.add(clause); } return bm25fQuery; } if (q instanceof TermQuery) { TermQuery tq = (TermQuery) q; String field = tq.getTerm().field(); if (indexOf(field, fields) < 0) return q; BM25FBooleanQuery bm25fQuery = new BM25FBooleanQuery(param); bm25fQuery.add(new BooleanClause(tq, Occur.MUST)); return bm25fQuery; } return q; }
From source file:org.socialhistoryservices.api.oai.OAIRequestHandler.java
License:Open Source License
private DocList runQuery(SolrQueryRequest request, List<String> q, int cursor, int len) throws ParseException, IOException { Query filter = null; // not implemented final SortField sortField = new SortField((String) Utils.getParam("field_sort_datestamp"), SortField.LONG, false);//w w w. ja va2 s . c o m final Sort sort = new Sort(sortField); final SolrQueryParser parser = new SolrQueryParser(request.getSchema(), null); String[] queryParts = q.toArray(new String[0]); final Query query = parser.parse(Utils.join(queryParts, " AND ")); return request.getSearcher().getDocList(query, filter, sort, cursor, len); }