Search in sources :

Example 1 with SuggesterQueryBuilder

use of org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder 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)

Aggregations

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 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 Query (org.apache.lucene.search.Query)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 LoggerFactory (org.opengrok.indexer.logger.LoggerFactory)1 QueryBuilder (org.opengrok.indexer.search.QueryBuilder)1 SuggesterQuery (org.opengrok.suggest.query.SuggesterQuery)1 SuggesterData (org.opengrok.web.api.v1.suggester.model.SuggesterData)1 SuggesterQueryData (org.opengrok.web.api.v1.suggester.model.SuggesterQueryData)1 SuggesterQueryBuilder (org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder)1