Example usage for org.apache.lucene.util CharsRefBuilder toCharsRef

List of usage examples for org.apache.lucene.util CharsRefBuilder toCharsRef

Introduction

In this page you can find the example usage for org.apache.lucene.util CharsRefBuilder toCharsRef.

Prototype

public CharsRef toCharsRef() 

Source Link

Document

Build a new CharsRef that has the same content as this builder.

Usage

From source file:org.codelibs.elasticsearch.search.suggest.completion.context.ContextMappings.java

License:Apache License

/**
 * Wraps a {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/*from   www .  java2 s  .  com*/
 */
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;
}

From source file:org.elasticsearch.search.suggest.filteredsuggest.filter.FilterMappings.java

License:Apache License

/**
 * Wraps a {@link CompletionQuery} with context queries
 *
 * @param query/*from   w w w . j a va  2  s .c om*/
 *            base completion query to wrap
 * @param queryFilters
 *            a map of context mapping name and collected query contexts
 * @return context-enabled queries
 */
public List<ContextQuery> toContextQuery(CompletionQuery query,
        Map<String, List<ContextMapping.InternalQueryContext>> queryFilters) {
    List<ContextQuery> queries = new ArrayList<>(4);

    List<ContextMapping.InternalQueryContext> internalQueryContext = queryFilters.get("default_empty");
    if (internalQueryContext != null) {
        ContextQuery typedContextQuery = new ContextQuery(query);
        CharsRefBuilder scratch = new CharsRefBuilder();
        scratch.grow(1);

        ContextMapping.InternalQueryContext context = internalQueryContext.get(0);
        scratch.append(context.context);
        typedContextQuery.addContext(scratch.toCharsRef(), context.boost, !context.isPrefix);

        queries.add(typedContextQuery);

        return queries;
    }

    if (queryFilters.isEmpty() == false) {
        CharsRefBuilder scratch = new CharsRefBuilder();
        scratch.grow(1);
        for (String name : queryFilters.keySet()) {
            internalQueryContext = queryFilters.get(name);
            if (internalQueryContext != null) {
                ContextQuery typedContextQuery = new ContextQuery(query);
                for (ContextMapping.InternalQueryContext context : internalQueryContext) {
                    scratch.append(context.context);
                    typedContextQuery.addContext(scratch.toCharsRef(), context.boost, !context.isPrefix);
                    scratch.setLength(0);
                }
                queries.add(typedContextQuery);
            }
        }
    }

    return queries;
}