List of usage examples for org.apache.lucene.util CharsRefBuilder append
@Override
public CharsRefBuilder append(char c)
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 . j a va 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.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();/*from w ww . j a va2 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 w ww . j a v a2 s.co 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]")); } }
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 av a 2s . 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; }