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);
}
}
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());
}
}
}
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);
}
}
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);
}
}
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;
}
Aggregations