Search in sources :

Example 1 with ContextQuery

use of org.apache.lucene.search.suggest.document.ContextQuery in project elasticsearch by elastic.

the class ContextMappings method toContextQuery.

/**
     * Wraps a {@link CompletionQuery} with context queries
     *
     * @param query base completion query to wrap
     * @param queryContexts a map of context mapping name and collected query contexts
     * @return a context-enabled query
     */
public ContextQuery toContextQuery(CompletionQuery query, Map<String, List<ContextMapping.InternalQueryContext>> queryContexts) {
    ContextQuery typedContextQuery = new ContextQuery(query);
    if (queryContexts.isEmpty() == false) {
        CharsRefBuilder scratch = new CharsRefBuilder();
        scratch.grow(1);
        for (int typeId = 0; typeId < contextMappings.size(); typeId++) {
            scratch.setCharAt(0, (char) typeId);
            scratch.setLength(1);
            ContextMapping mapping = contextMappings.get(typeId);
            List<ContextMapping.InternalQueryContext> internalQueryContext = queryContexts.get(mapping.name());
            if (internalQueryContext != null) {
                for (ContextMapping.InternalQueryContext context : internalQueryContext) {
                    scratch.append(context.context);
                    typedContextQuery.addContext(scratch.toCharsRef(), context.boost, !context.isPrefix);
                    scratch.setLength(1);
                }
            }
        }
    }
    return typedContextQuery;
}
Also used : ContextQuery(org.apache.lucene.search.suggest.document.ContextQuery) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder)

Aggregations

ContextQuery (org.apache.lucene.search.suggest.document.ContextQuery)1 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)1