Java Code Examples for org.apache.solr.search.QParser#getParser()

The following examples show how to use org.apache.solr.search.QParser#getParser() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlfrescoReRankQParserPlugin.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Query parse() throws SyntaxError {
    String reRankQueryString = localParams.get("reRankQuery");
    boolean scale = localParams.getBool("scale", false);
    QParser reRankParser = QParser.getParser(reRankQueryString, null, req);
    Query reRankQuery = reRankParser.parse();

    int reRankDocs  = localParams.getInt("reRankDocs", 200);
    reRankDocs = Math.max(1, reRankDocs); //

    double reRankWeight = localParams.getDouble("reRankWeight",2.0d);

    int start = params.getInt(CommonParams.START,0);
    int rows = params.getInt(CommonParams.ROWS,10);
    int length = start+rows;
    return new ReRankQuery(reRankQuery, reRankDocs, reRankWeight, length, scale);
}
 
Example 2
Source File: DirectUpdateHandler2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query getQuery(DeleteUpdateCommand cmd) {
  Query q;
  try {
    // move this higher in the stack?
    QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
    q = parser.getQuery();
    q = QueryUtils.makeQueryable(q);

    // Make sure not to delete newer versions
    if (ulog != null && cmd.getVersion() != 0 && cmd.getVersion() != -Long.MAX_VALUE) {
      BooleanQuery.Builder bq = new BooleanQuery.Builder();
      bq.add(q, Occur.MUST);
      SchemaField sf = ulog.getVersionInfo().getVersionField();
      ValueSource vs = sf.getType().getValueSource(sf, null);
      ValueSourceRangeFilter filt = new ValueSourceRangeFilter(vs, Long.toString(Math.abs(cmd.getVersion())), null, true, true);
      FunctionRangeQuery range = new FunctionRangeQuery(filt);
      bq.add(range, Occur.MUST_NOT);  // formulated in the "MUST_NOT" sense so we can delete docs w/o a version (some tests depend on this...)
      q = bq.build();
    }

    return q;

  } catch (SyntaxError e) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
  }
}
 
Example 3
Source File: TaggerRequestHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * The set of documents matching the provided 'fq' (filter query). Don't include deleted docs
 * either. If null is returned, then all docs are available.
 */
private Bits computeDocCorpus(SolrQueryRequest req) throws SyntaxError, IOException {
  final String[] corpusFilterQueries = req.getParams().getParams("fq");
  final SolrIndexSearcher searcher = req.getSearcher();
  final Bits docBits;
  if (corpusFilterQueries != null && corpusFilterQueries.length > 0) {
    List<Query> filterQueries = new ArrayList<Query>(corpusFilterQueries.length);
    for (String corpusFilterQuery : corpusFilterQueries) {
      QParser qParser = QParser.getParser(corpusFilterQuery, null, req);
      try {
        filterQueries.add(qParser.parse());
      } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
      }
    }

    final DocSet docSet = searcher.getDocSet(filterQueries);//hopefully in the cache

    docBits = docSet.getBits();
  } else {
    docBits = searcher.getSlowAtomicReader().getLiveDocs();
  }
  return docBits;
}
 
Example 4
Source File: HighlightComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {
  SolrParams params = rb.req.getParams();
  rb.doHighlights = solrConfigHighlighter.isHighlightingEnabled(params);
  if(rb.doHighlights){
    rb.setNeedDocList(true);
    String hlq = params.get(HighlightParams.Q);
    String hlparser = MoreObjects.firstNonNull(params.get(HighlightParams.QPARSER),
                                            params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE));
    if(hlq != null){
      try {
        QParser parser = QParser.getParser(hlq, hlparser, rb.req);
        rb.setHighlightQuery(parser.getHighlightQuery());
      } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
      }
    }
  }
}
 
Example 5
Source File: LireRequestHandler.java    From liresolr with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses the fq param and adds it as a list of filter queries or reverts to null if nothing is found
 * or an Exception is thrown.
 *
 * @param req
 * @return either a query from the QueryParser or null
 */
private List<Query> getFilterQueries(SolrQueryRequest req) {
    List<Query> filters = null;

    String[] fqs = req.getParams().getParams("fq");
    if (fqs != null && fqs.length != 0) {
        filters = new ArrayList<>(fqs.length);
        try {
            for (String fq : fqs) {
                if (fq != null && fq.trim().length() != 0) {
                    QParser fqp = QParser.getParser(fq, req);
                    fqp.setIsFilter(true);
                    filters.add(fqp.getQuery());
                }
            }
        } catch (SyntaxError e) {
            e.printStackTrace();
        }

        if (filters.isEmpty()) {
            filters = null;
        }
    }
    return filters;
}
 
Example 6
Source File: ChronixRetentionHandler.java    From chronix.server with Apache License 2.0 6 votes vote down vote up
/**
 * Searches the index, if older documents exists. Updates the solr query response.
 *
 * @param req - the solr query request information
 * @param rsp - the solr query response information
 * @return true if the hit count is greater zero, otherwise false
 * @throws SyntaxError, IOException if bad things happen
 */
private boolean olderDocumentsExists(String queryString, SolrQueryRequest req, SolrQueryResponse rsp) throws SyntaxError, IOException {
    String defType = req.getParams().get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);

    QParser queryParser = QParser.getParser(queryString, defType, req);
    Query query = queryParser.getQuery();

    TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
    req.getSearcher().search(query, totalHitCountCollector);

    rsp.add("query", String.format("%s:[* TO NOW-%s]", queryField, timeSeriesAge));
    rsp.add("queryTechnical", queryString);
    rsp.add("removedDocuments", totalHitCountCollector.getTotalHits());

    return totalHitCountCollector.getTotalHits() != 0;
}
 
Example 7
Source File: FacetProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Query parserFilter(String rawFilter, SolrQueryRequest req) {
  QParser parser = null;
  try {
    parser = QParser.getParser(rawFilter, req);
    parser.setIsFilter(true);
    Query symbolicFilter = parser.getQuery();
    if (symbolicFilter == null) {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "QParser yields null, perhaps unresolved parameter reference in: "+rawFilter);
    }
    return symbolicFilter;
  } catch (SyntaxError syntaxError) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, syntaxError);
  }
}
 
Example 8
Source File: FacetParser.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public FacetQuery parse(Object arg) throws SyntaxError {
  parseCommonParams(arg);

  String qstring = null;
  if (arg instanceof String) {
    // just the field name...
    qstring = (String)arg;

  } else if (arg instanceof Map) {
    @SuppressWarnings({"unchecked"})
    Map<String, Object> m = (Map<String, Object>) arg;
    qstring = getString(m, "q", null);
    if (qstring == null) {
      qstring = getString(m, "query", null);
    }

    // OK to parse subs before we have parsed our own query?
    // as long as subs don't need to know about it.
    parseSubs( m.get("facet") );
  } else if (arg != null) {
    // something lke json.facet.facet.query=2
    throw err("Expected string/map for facet query, received " + arg.getClass().getSimpleName() + "=" + arg);
  }

  // TODO: substats that are from defaults!!!

  if (qstring != null) {
    QParser parser = QParser.getParser(qstring, getSolrRequest());
    parser.setIsFilter(true);
    facet.q = parser.getQuery();
  }

  return facet;
}
 
Example 9
Source File: BJQParserTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testFiltersCache() throws SyntaxError, IOException {
  final String [] elFilterQuery = new String[] {"q", "{!filters param=$child.fq v=$gchq}",
      "child.fq", "childparent_s:e",
      "child.fq", "child_s:l",
      "gchq", "child_s:[* TO *]"};
  assertQ("precondition: single doc match", 
       req(elFilterQuery), elChild);
  final Query query;
  try(final SolrQueryRequest req = req(elFilterQuery)) {
    QParser parser = QParser.getParser(req.getParams().get("q"), null, req);
    query = parser.getQuery();
    final TopDocs topDocs = req.getSearcher().search(query, 10);
    assertEquals(1, topDocs.totalHits.value);
  }
  assertU(adoc("id", "12275", 
      "child_s", "l", "childparent_s", "e"));
  assertU(commit());

  assertQ("here we rely on autowarming for cathing cache leak",  //cache=false
        req(elFilterQuery), "//*[@numFound='2']");

  try(final SolrQueryRequest req = req()) {
      final int count = req.getSearcher().count(query);
      assertEquals("expecting new doc is visible to old query", 2, count);
  }
}
 
Example 10
Source File: ParseUtility.java    From semantic-knowledge-graph with Apache License 2.0 5 votes vote down vote up
public static Query parseQueryString(String qString, SolrQueryRequest req) {
    try {
        QParser parser = QParser.getParser(qString, req.getParams().get("defType"), req);
        return parser.getQuery();
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "Syntax error in query: " + qString + ".");
    }
}
 
Example 11
Source File: SparqlSearchComponent.java    From SolRDF with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the {@link QParser} associated with this request.
 * 
 * @param request the {@link SolrQueryRequest}.
 * @return the {@link QParser} associated with this request.
 * @throws SyntaxError in case of syntax errors.
 */
QParser qParser(final SolrQueryRequest request) throws SyntaxError {
	return QParser.getParser(
			queryString(request), 
			DEFAULT_DEF_TYPE, 
			request);		
}
 
Example 12
Source File: QueryCommand.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the group query from the specified groupQueryString.
 * The groupQueryString is parsed into a query.
 *
 * @param groupQueryString The group query string to parse
 * @param request The current request
 * @return this
 */
public Builder setQuery(String groupQueryString, SolrQueryRequest request) throws SyntaxError {
  QParser parser = QParser.getParser(groupQueryString, request);
  this.queryString = groupQueryString;
  return setQuery(parser.getQuery());
}
 
Example 13
Source File: ChronixCompactionHandler.java    From chronix.server with Apache License 2.0 2 votes vote down vote up
/**
 * @param query the query
 * @return a lucene qparser
 * @throws SyntaxError iff something goes wrong
 */
public QParser parser(String query) throws SyntaxError {
    return QParser.getParser(query, req);
}