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

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

Introduction

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

Prototype

public void setCharAt(int offset, char b) 

Source Link

Document

Set a char.

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   w  w w  . ja  va 2s  . 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.index.mapper.completion.CompletionFieldMapperTests.java

License:Apache License

public void testFieldValueValidation() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties")
            .startObject("completion").field("type", "completion").endObject().endObject().endObject()
            .endObject().string();// ww w. j a v  a  2 s . c o m

    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
    CharsRefBuilder charsRefBuilder = new CharsRefBuilder();
    charsRefBuilder.append("sugg");
    charsRefBuilder.setCharAt(2, '\u001F');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x1F]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x1f]"));
    }

    charsRefBuilder.setCharAt(2, '\u0000');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x0]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x0]"));
    }

    charsRefBuilder.setCharAt(2, '\u001E');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x1E]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x1e]"));
    }
}

From source file:org.elasticsearch.index.mapper.CompletionFieldMapperTests.java

License:Apache License

public void testFieldValueValidation() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties")
            .startObject("completion").field("type", "completion").endObject().endObject().endObject()
            .endObject().string();/*from www  .  ja va2 s.c  o  m*/

    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1",
            new CompressedXContent(mapping));
    CharsRefBuilder charsRefBuilder = new CharsRefBuilder();
    charsRefBuilder.append("sugg");
    charsRefBuilder.setCharAt(2, '\u001F');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x1F]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x1f]"));
    }

    charsRefBuilder.setCharAt(2, '\u0000');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x0]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x0]"));
    }

    charsRefBuilder.setCharAt(2, '\u001E');
    try {
        defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject()
                .field("completion", charsRefBuilder.get().toString()).endObject().bytes());
        fail("No error indexing value with reserved character [0x1E]");
    } catch (MapperParsingException e) {
        Throwable cause = e.unwrapCause().getCause();
        assertThat(cause, instanceOf(IllegalArgumentException.class));
        assertThat(cause.getMessage(), containsString("[0x1e]"));
    }
}