List of usage examples for org.apache.solr.search LuceneQParserPlugin LuceneQParserPlugin
LuceneQParserPlugin
From source file:eu.europeana.assets.service.ir.text.bm25f.parser.BM25FQParser.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from www . jav a 2s .c o 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:net.yacy.search.index.SingleDocumentMatcher.java
License:Open Source License
/** * @param query a Solr query string to parse * @param targetCore an open Solr index core that is the target of the query * @return a lucene Query instance parsed from the given Solr query string on the provided Solr core. * @throws SyntaxError when the query syntax is not valid * @throws SolrException when a query required element is missing, or when a problem occurred when accessing the target core *//*w w w . ja va 2 s. co m*/ public static Query toLuceneQuery(final String query, final SolrCore targetCore) throws SyntaxError, SolrException { if (query == null || targetCore == null) { throw new IllegalArgumentException("All parameters must be non null"); } final SolrQuery solrQuery = new SolrQuery(query); solrQuery.setParam(CommonParams.DF, CollectionSchema.text_t.getSolrFieldName()); final SolrQueryRequestBase solrRequest = new SolrQueryRequestBase(targetCore, solrQuery) { }; final LuceneQParserPlugin luceneParserPlugin = new LuceneQParserPlugin(); final QParser solrParser = luceneParserPlugin.createParser(query, null, solrRequest.getParams(), solrRequest); return solrParser.parse(); }