use of org.opengrok.web.api.v1.suggester.model.SuggesterQueryData in project OpenGrok by OpenGrok.
the class SuggesterQueryDataParser method parse.
/**
* Parses the {@link SuggesterQueryData}.
* @param data data to parse
* @return parsed data for the suggester use
* @throws ParseException could not parse the search data into a valid {@link Query}
*/
public static SuggesterData parse(final SuggesterQueryData data) throws ParseException {
List<String> projectList = data.getProjects().stream().filter(p -> RuntimeEnvironment.getInstance().getProjectNames().contains(p)).collect(Collectors.toList());
Map<String, String> fieldQueries = getFieldQueries(data);
ProcessedQueryData queryData = processQuery(fieldQueries.get(data.getField()), data.getCaretPosition());
fieldQueries.put(data.getField(), queryData.query);
SuggesterQueryBuilder builder = new SuggesterQueryBuilder(data.getField(), queryData.identifier);
builder.setFreetext(fieldQueries.get(QueryBuilder.FULL)).setDefs(fieldQueries.get(QueryBuilder.DEFS)).setRefs(fieldQueries.get(QueryBuilder.REFS)).setPath(fieldQueries.get(QueryBuilder.PATH)).setHist(fieldQueries.get(QueryBuilder.HIST)).setType(fieldQueries.get(QueryBuilder.TYPE));
Query query;
try {
query = builder.build();
} catch (ParseException e) {
// the position might be still wrong if the parse error was at the end of the identifier
throw new ParseException(e.getMessage().replaceAll(queryData.identifier, ""));
}
SuggesterQuery suggesterQuery = builder.getSuggesterQuery();
// builder can return the suggester query if it was simple query, we ignore it in that case
if (query.equals(suggesterQuery)) {
query = null;
}
return new SuggesterData(suggesterQuery, projectList, query, builder.getQueryTextWithPlaceholder(), builder.getIdentifier());
}
use of org.opengrok.web.api.v1.suggester.model.SuggesterQueryData in project OpenGrok by OpenGrok.
the class SuggesterController method getSuggestions.
/**
* Returns suggestions based on the search criteria specified in {@code data}.
* @param data suggester form data
* @return list of suggestions and other related information
* @throws ParseException if the Lucene query created from {@code data} could not be parsed
*/
@GET
@Authorized
@CorsEnable
@Produces(MediaType.APPLICATION_JSON)
public Result getSuggestions(@Valid @BeanParam final SuggesterQueryData data) throws ParseException {
Instant start = Instant.now();
SuggesterData suggesterData = SuggesterQueryDataParser.parse(data);
if (suggesterData.getSuggesterQuery() == null) {
throw new ParseException("Could not determine suggester query");
}
SuggesterConfig config = env.getSuggesterConfig();
modifyDataBasedOnConfiguration(suggesterData, config);
if (!satisfiesConfiguration(suggesterData, config)) {
logger.log(Level.FINER, "Suggester request with data {0} does not satisfy configuration settings", data);
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
Suggestions suggestions = suggester.getSuggestions(suggesterData.getProjects(), suggesterData.getSuggesterQuery(), suggesterData.getQuery());
Instant end = Instant.now();
long timeInMs = Duration.between(start, end).toMillis();
return new Result(suggestions.getItems(), suggesterData.getIdentifier(), suggesterData.getSuggesterQueryFieldText(), timeInMs, suggestions.isPartialResult());
}
Aggregations