Search in sources :

Example 1 with SuggesterQueryData

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());
}
Also used : Query(org.apache.lucene.search.Query) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterQueryData(org.opengrok.web.api.v1.suggester.model.SuggesterQueryData) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) List(java.util.List) Map(java.util.Map) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData) SuggesterQueryBuilder(org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder) LoggerFactory(org.opengrok.indexer.logger.LoggerFactory) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) SuggesterQueryBuilder(org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder) Query(org.apache.lucene.search.Query) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData)

Example 2 with SuggesterQueryData

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());
}
Also used : Suggestions(org.opengrok.suggest.Suggester.Suggestions) WebApplicationException(jakarta.ws.rs.WebApplicationException) Instant(java.time.Instant) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData) SuggesterConfig(org.opengrok.indexer.configuration.SuggesterConfig) CorsEnable(org.opengrok.web.api.v1.filter.CorsEnable) Authorized(org.opengrok.web.api.v1.suggester.provider.filter.Authorized) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Aggregations

ParseException (org.apache.lucene.queryparser.classic.ParseException)2 SuggesterData (org.opengrok.web.api.v1.suggester.model.SuggesterData)2 GET (jakarta.ws.rs.GET)1 Produces (jakarta.ws.rs.Produces)1 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)1 Query (org.apache.lucene.search.Query)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 SuggesterConfig (org.opengrok.indexer.configuration.SuggesterConfig)1 LoggerFactory (org.opengrok.indexer.logger.LoggerFactory)1 QueryBuilder (org.opengrok.indexer.search.QueryBuilder)1 Suggestions (org.opengrok.suggest.Suggester.Suggestions)1 SuggesterQuery (org.opengrok.suggest.query.SuggesterQuery)1