Example usage for org.apache.lucene.search.suggest.document ContextQuery addContext

List of usage examples for org.apache.lucene.search.suggest.document ContextQuery addContext

Introduction

In this page you can find the example usage for org.apache.lucene.search.suggest.document ContextQuery addContext.

Prototype

public void addContext(CharSequence context, float boost, boolean exact) 

Source Link

Document

Adds a context with boost, set exact to false if the context is a prefix of any indexed contexts

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/*w  w w  . java  2  s.  c o m*/
 */
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// w w  w .  j ava2s . com
 *            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;
}