Search in sources :

Example 11 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project ddf by codice.

the class GeoNamesQueryLuceneIndex method doQuery.

protected List<GeoEntry> doQuery(final String queryString, final int maxResults, final Directory directory) throws GeoEntryQueryException {
    if (StringUtils.isBlank(queryString)) {
        throw new IllegalArgumentException("The query string cannot be null or empty.");
    }
    if (maxResults < 1) {
        throw new IllegalArgumentException("maxResults must be positive.");
    }
    if (directory == null) {
        return Collections.emptyList();
    }
    try (final IndexReader indexReader = createIndexReader(directory)) {
        final IndexSearcher indexSearcher = createIndexSearcher(indexReader);
        final Query query = createQuery(queryString);
        final TopDocs topDocs = indexSearcher.search(query, maxResults);
        if (topDocs.totalHits > 0) {
            final List<GeoEntry> results = new ArrayList<>();
            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                final Document document = indexSearcher.doc(scoreDoc.doc);
                // The alternate names aren't being stored (they are only used for queries),
                // so we don't retrieve them here.
                results.add(new GeoEntry.Builder().name(document.get(GeoNamesLuceneConstants.NAME_FIELD)).latitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LATITUDE_FIELD))).longitude(Double.parseDouble(document.get(GeoNamesLuceneConstants.LONGITUDE_FIELD))).featureCode(document.get(GeoNamesLuceneConstants.FEATURE_CODE_FIELD)).population(Long.parseLong(document.get(GeoNamesLuceneConstants.POPULATION_FIELD))).countryCode(document.get(GeoNamesLuceneConstants.COUNTRY_CODE_FIELD)).build());
            }
            return results;
        } else {
            return Collections.emptyList();
        }
    } catch (IOException e) {
        throw new GeoEntryQueryException("Error reading the index", e);
    } catch (ParseException e) {
        throw new GeoEntryQueryException("Error parsing query", e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) GeoEntryQueryException(org.codice.ddf.spatial.geocoding.GeoEntryQueryException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) IndexReader(org.apache.lucene.index.IndexReader) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 12 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project che by eclipse.

the class LuceneSearcher method search.

@Override
public SearchResult search(QueryExpression query) throws ServerException {
    IndexSearcher luceneSearcher = null;
    try {
        final long startTime = System.currentTimeMillis();
        searcherManager.maybeRefresh();
        luceneSearcher = searcherManager.acquire();
        Query luceneQuery = createLuceneQuery(query);
        ScoreDoc after = null;
        final int numSkipDocs = Math.max(0, query.getSkipCount());
        if (numSkipDocs > 0) {
            after = skipScoreDocs(luceneSearcher, luceneQuery, numSkipDocs);
        }
        final int numDocs = query.getMaxItems() > 0 ? Math.min(query.getMaxItems(), RESULT_LIMIT) : RESULT_LIMIT;
        TopDocs topDocs = luceneSearcher.searchAfter(after, luceneQuery, numDocs);
        final int totalHitsNum = topDocs.totalHits;
        List<SearchResultEntry> results = newArrayList();
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            String filePath = luceneSearcher.doc(scoreDoc.doc).getField(PATH_FIELD).stringValue();
            results.add(new SearchResultEntry(filePath));
        }
        final long elapsedTimeMillis = System.currentTimeMillis() - startTime;
        boolean hasMoreToRetrieve = numSkipDocs + topDocs.scoreDocs.length + 1 < totalHitsNum;
        QueryExpression nextPageQueryExpression = null;
        if (hasMoreToRetrieve) {
            nextPageQueryExpression = createNextPageQuery(query, numSkipDocs + topDocs.scoreDocs.length);
        }
        return SearchResult.aSearchResult().withResults(results).withTotalHits(totalHitsNum).withNextPageQueryExpression(nextPageQueryExpression).withElapsedTimeMillis(elapsedTimeMillis).build();
    } catch (IOException | ParseException e) {
        throw new ServerException(e.getMessage(), e);
    } finally {
        try {
            searcherManager.release(luceneSearcher);
        } catch (IOException e) {
            LOG.error(e.getMessage());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ServerException(org.eclipse.che.api.core.ServerException) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ParseException(org.apache.lucene.queryparser.classic.ParseException) QueryExpression(org.eclipse.che.api.vfs.search.QueryExpression) SearchResultEntry(org.eclipse.che.api.vfs.search.SearchResultEntry)

Example 13 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project neo4j by neo4j.

the class IndexType method query.

Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
    if (value instanceof Query) {
        return (Query) value;
    }
    QueryParser parser = new QueryParser(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.classic.QueryParser) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 14 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project graylog2-server by Graylog2.

the class SearchResource method createRequestExceptionForParseFailure.

protected WebApplicationException createRequestExceptionForParseFailure(String query, SearchPhaseExecutionException e) {
    LOG.warn("Unable to execute search: {}", e.getMessage());
    QueryParseError errorMessage = QueryParseError.create(query, "Unable to execute search", e.getClass().getCanonicalName());
    // We're so going to hell for this…
    if (e.toString().contains("nested: QueryParsingException")) {
        final QueryParser queryParser = new QueryParser("", new StandardAnalyzer());
        try {
            queryParser.parse(query);
        } catch (ParseException parseException) {
            Token currentToken = null;
            try {
                // FIXME I have no idea why this is necessary but without that call currentToken will be null.
                final ParseException exception = queryParser.generateParseException();
                currentToken = exception.currentToken;
            } catch (NullPointerException npe) {
                // "Normal" exception and no need to spam the logs with it.
                LOG.debug("Exception thrown while generating parse exception.", npe);
            }
            if (currentToken == null) {
                LOG.warn("No position/token available for ParseException.", parseException);
                errorMessage = QueryParseError.create(query, parseException.getMessage(), parseException.getClass().getCanonicalName());
            } else {
                // scan for first usable token with position information
                int beginColumn = 0;
                int beginLine = 0;
                int endColumn = 0;
                int endLine = 0;
                while (currentToken != null && beginLine == 0) {
                    beginColumn = currentToken.beginColumn;
                    beginLine = currentToken.beginLine;
                    endColumn = currentToken.endColumn;
                    endLine = currentToken.endLine;
                    currentToken = currentToken.next;
                }
                errorMessage = QueryParseError.create(query, beginColumn, beginLine, endColumn, endLine, parseException.getMessage(), parseException.getClass().getCanonicalName());
            }
        }
        return new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(errorMessage).build());
    } else {
        return new InternalServerErrorException("Unable to fulfill search request", e);
    }
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) QueryParseError(org.graylog2.rest.resources.search.responses.QueryParseError) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Token(org.apache.lucene.queryparser.classic.Token) ParseException(org.apache.lucene.queryparser.classic.ParseException)

Example 15 with ParseException

use of org.apache.lucene.queryparser.classic.ParseException in project textdb by TextDB.

the class RegexMatcherSourceOperator method createLuceneQuery.

public static Query createLuceneQuery(RegexSourcePredicate predicate) throws StorageException {
    Query luceneQuery;
    String queryString;
    // Try to apply translator. If it fails, use scan query.
    try {
        queryString = RegexToGramQueryTranslator.translate(predicate.getRegex()).getLuceneQueryString();
    } catch (com.google.re2j.PatternSyntaxException e) {
        queryString = DataConstants.SCAN_QUERY;
    }
    // Try to parse the query string. It if fails, raise an exception.
    try {
        luceneQuery = new MultiFieldQueryParser(predicate.getAttributeNames().stream().toArray(String[]::new), RelationManager.getRelationManager().getTableAnalyzer(predicate.getTableName())).parse(queryString);
    } catch (ParseException e) {
        throw new StorageException(e);
    }
    return luceneQuery;
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) ParseException(org.apache.lucene.queryparser.classic.ParseException) StorageException(edu.uci.ics.textdb.api.exception.StorageException)

Aggregations

ParseException (org.apache.lucene.queryparser.classic.ParseException)20 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)12 Query (org.apache.lucene.search.Query)11 IOException (java.io.IOException)9 BooleanQuery (org.apache.lucene.search.BooleanQuery)6 ArrayList (java.util.ArrayList)5 TermQuery (org.apache.lucene.search.TermQuery)5 Analyzer (org.apache.lucene.analysis.Analyzer)4 IndexReader (org.apache.lucene.index.IndexReader)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 WildcardQuery (org.apache.lucene.search.WildcardQuery)4 Document (org.apache.lucene.document.Document)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 Map (java.util.Map)2 FullTextExpression (org.apache.jackrabbit.oak.query.fulltext.FullTextExpression)2 PropertyRestriction (org.apache.jackrabbit.oak.spi.query.Filter.PropertyRestriction)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)2 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)2