Search in sources :

Example 11 with ParseException

use of org.apache.lucene.queryParser.ParseException in project jackrabbit by apache.

the class LuceneQueryFactory method create.

/**
 * Creates a lucene query for the given QOM full text search.
 *
 * @param fts the full text search constraint.
 * @return the lucene query for the given constraint.
 * @throws RepositoryException if an error occurs while creating the query.
 */
public Query create(FullTextSearchImpl fts) throws RepositoryException {
    String fieldname;
    if (fts.getPropertyName() == null) {
        // fulltext on node
        fieldname = FieldNames.FULLTEXT;
    } else {
        // final path element is a property name
        Name propName = fts.getPropertyQName();
        StringBuffer tmp = new StringBuffer();
        tmp.append(nsMappings.getPrefix(propName.getNamespaceURI()));
        tmp.append(":").append(FieldNames.FULLTEXT_PREFIX);
        tmp.append(propName.getLocalName());
        fieldname = tmp.toString();
    }
    QueryParser parser = new JackrabbitQueryParser(fieldname, index.getTextAnalyzer(), index.getSynonymProvider(), cache);
    try {
        StaticOperand expr = fts.getFullTextSearchExpression();
        return parser.parse(evaluator.getValue(expr).getString());
    } catch (ParseException e) {
        throw new RepositoryException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) StaticOperand(javax.jcr.query.qom.StaticOperand) RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.lucene.queryParser.ParseException) NodeName(javax.jcr.query.qom.NodeName) NodeLocalName(javax.jcr.query.qom.NodeLocalName) Name(org.apache.jackrabbit.spi.Name)

Example 12 with ParseException

use of org.apache.lucene.queryParser.ParseException in project jackrabbit by apache.

the class JackrabbitQueryParser method getPrefixQuery.

/**
 * {@inheritDoc}
 */
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
    // only create a prefix query when the term is a single word / token
    Analyzer a = getAnalyzer();
    TokenStream ts = a.tokenStream(field, new StringReader(termStr));
    int count = 0;
    boolean isCJ = false;
    try {
        TypeAttribute t = ts.addAttribute(TypeAttribute.class);
        ts.reset();
        while (ts.incrementToken()) {
            count++;
            isCJ = StandardTokenizer.TOKEN_TYPES[StandardTokenizer.CJ].equals(t.type());
        }
        ts.end();
    } catch (IOException e) {
        throw new ParseException(e.getMessage());
    } finally {
        try {
            ts.close();
        } catch (IOException e) {
        // ignore
        }
    }
    if (count > 1 && isCJ) {
        return getFieldQuery(field, termStr);
    } else {
        return getWildcardQuery(field, termStr + "*");
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) TypeAttribute(org.apache.lucene.analysis.tokenattributes.TypeAttribute) StringReader(java.io.StringReader) IOException(java.io.IOException) ParseException(org.apache.lucene.queryParser.ParseException) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 13 with ParseException

use of org.apache.lucene.queryParser.ParseException in project onebusaway-application-modules by camsys.

the class StopsBeanServiceImpl method getStopsByBoundsAndQuery.

private StopsBean getStopsByBoundsAndQuery(SearchQueryBean queryBean) throws ServiceException {
    CoordinateBounds bounds = queryBean.getBounds();
    String query = queryBean.getQuery();
    int maxCount = queryBean.getMaxCount();
    CoordinatePoint center = SphericalGeometryLibrary.getCenterOfBounds(bounds);
    SearchResult<AgencyAndId> stops;
    try {
        stops = _searchService.searchForStopsByCode(query, 10, MIN_SCORE);
    } catch (ParseException e) {
        throw new InvalidArgumentServiceException("query", "queryParseError");
    } catch (IOException e) {
        _log.error("error executing stop search: query=" + query, e);
        e.printStackTrace();
        throw new ServiceException();
    }
    Min<StopBean> closest = new Min<StopBean>();
    List<StopBean> stopBeans = new ArrayList<StopBean>();
    for (AgencyAndId aid : stops.getResults()) {
        StopBean stopBean = _stopBeanService.getStopForId(aid);
        if (bounds.contains(stopBean.getLat(), stopBean.getLon()))
            stopBeans.add(stopBean);
        double distance = SphericalGeometryLibrary.distance(center.getLat(), center.getLon(), stopBean.getLat(), stopBean.getLon());
        closest.add(distance, stopBean);
    }
    boolean limitExceeded = BeanServiceSupport.checkLimitExceeded(stopBeans, maxCount);
    // If nothing was found in range, add the closest result
    if (stopBeans.isEmpty() && !closest.isEmpty())
        stopBeans.add(closest.getMinElement());
    return constructResult(stopBeans, limitExceeded);
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) Min(org.onebusaway.collections.Min) InvalidArgumentServiceException(org.onebusaway.exceptions.InvalidArgumentServiceException) ServiceException(org.onebusaway.exceptions.ServiceException) NoSuchAgencyServiceException(org.onebusaway.exceptions.NoSuchAgencyServiceException) InvalidArgumentServiceException(org.onebusaway.exceptions.InvalidArgumentServiceException) StopBean(org.onebusaway.transit_data.model.StopBean) ParseException(org.apache.lucene.queryParser.ParseException) CoordinateBounds(org.onebusaway.geospatial.model.CoordinateBounds)

Example 14 with ParseException

use of org.apache.lucene.queryParser.ParseException in project graphdb by neo4j-attic.

the class IndexType method query.

Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
    if (value instanceof Query) {
        return (Query) value;
    }
    QueryParser parser = new QueryParser(Version.LUCENE_30, keyOrNull, analyzer);
    parser.setAllowLeadingWildcard(true);
    parser.setLowercaseExpandedTerms(toLowerCase);
    if (contextOrNull != null && contextOrNull.getDefaultOperator() != null) {
        parser.setDefaultOperator(contextOrNull.getDefaultOperator());
    }
    try {
        return parser.parse(value.toString());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ParseException(org.apache.lucene.queryParser.ParseException)

Aggregations

ParseException (org.apache.lucene.queryParser.ParseException)14 IOException (java.io.IOException)8 QueryParser (org.apache.lucene.queryParser.QueryParser)8 Query (org.apache.lucene.search.Query)7 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 Name (org.apache.jackrabbit.spi.Name)3 Sort (org.apache.lucene.search.Sort)3 TermQuery (org.apache.lucene.search.TermQuery)3 RepositoryException (javax.jcr.RepositoryException)2 NodeLocalName (javax.jcr.query.qom.NodeLocalName)2 NodeName (javax.jcr.query.qom.NodeName)2 StaticOperand (javax.jcr.query.qom.StaticOperand)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Hits (org.apache.lucene.search.Hits)2 ContextualEvaluationCriteria (ddf.catalog.pubsub.criteria.contextual.ContextualEvaluationCriteria)1 ContextualEvaluationCriteriaImpl (ddf.catalog.pubsub.criteria.contextual.ContextualEvaluationCriteriaImpl)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1