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

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

Introduction

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

Prototype

public CharsRef get() 

Source Link

Document

Return a CharsRef that points to the internal content of this builder.

Usage

From source file:org.apache.solr.schema.EnumFieldType.java

License:Apache License

@Override
public CharsRef indexedToReadable(BytesRef input, CharsRefBuilder output) {
    final Integer intValue = NumericUtils.sortableBytesToInt(input.bytes, 0);
    final String stringValue = enumMapping.intValueToStringValue(intValue);
    output.grow(stringValue.length());/*from  ww w .j  a  v a  2 s  .c o  m*/
    output.setLength(stringValue.length());
    stringValue.getChars(0, output.length(), output.chars(), 0);
    return output.get();
}

From source file:org.apache.solr.schema.PointField.java

License:Apache License

@Override
public CharsRef indexedToReadable(BytesRef indexedForm, CharsRefBuilder charsRef) {
    final String value = indexedToReadable(indexedForm);
    charsRef.grow(value.length());/*from ww w.  j  a v a2 s  .c om*/
    charsRef.setLength(value.length());
    value.getChars(0, charsRef.length(), charsRef.chars(), 0);
    return charsRef.get();
}

From source file:org.codelibs.elasticsearch.search.suggest.phrase.DirectCandidateGenerator.java

License:Apache License

public static int analyze(Analyzer analyzer, BytesRef toAnalyze, String field, TokenConsumer consumer,
        CharsRefBuilder spare) throws IOException {
    spare.copyUTF8Bytes(toAnalyze);/*from  ww w.  j a  v a 2 s  .c  o  m*/
    CharsRef charsRef = spare.get();
    try (TokenStream ts = analyzer.tokenStream(field,
            new FastCharArrayReader(charsRef.chars, charsRef.offset, charsRef.length))) {
        return analyze(ts, consumer);
    }
}

From source file:org.elasticsearch.action.search.TransportSearchHelper.java

License:Apache License

static ParsedScrollId parseScrollId(String scrollId) {
    CharsRefBuilder spare = new CharsRefBuilder();
    try {//from w ww .j a v  a  2 s.co m
        byte[] decode = Base64.decode(scrollId, Base64.URL_SAFE);
        spare.copyUTF8Bytes(decode, 0, decode.length);
    } catch (Exception e) {
        throw new IllegalArgumentException("Failed to decode scrollId", e);
    }
    String[] elements = Strings.splitStringToArray(spare.get(), ';');
    if (elements.length < 2) {
        throw new IllegalArgumentException("Malformed scrollId [" + scrollId + "]");
    }

    int index = 0;
    String type = elements[index++];
    int contextSize = Integer.parseInt(elements[index++]);
    if (elements.length < contextSize + 2) {
        throw new IllegalArgumentException("Malformed scrollId [" + scrollId + "]");
    }

    ScrollIdForNode[] context = new ScrollIdForNode[contextSize];
    for (int i = 0; i < contextSize; i++) {
        String element = elements[index++];
        int sep = element.indexOf(':');
        if (sep == -1) {
            throw new IllegalArgumentException("Malformed scrollId [" + scrollId + "]");
        }
        context[i] = new ScrollIdForNode(element.substring(sep + 1), Long.parseLong(element.substring(0, sep)));
    }
    Map<String, String> attributes;
    int attributesSize = Integer.parseInt(elements[index++]);
    if (attributesSize == 0) {
        attributes = ImmutableMap.of();
    } else {
        attributes = Maps.newHashMapWithExpectedSize(attributesSize);
        for (int i = 0; i < attributesSize; i++) {
            String element = elements[index++];
            int sep = element.indexOf(':');
            attributes.put(element.substring(0, sep), element.substring(sep + 1));
        }
    }
    return new ParsedScrollId(scrollId, type, context, attributes);
}

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();//w  w w. j av  a  2s  .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  w w . j a  va 2 s.c  om

    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.completion.old.CompletionSuggester.java

License:Apache License

@Override
protected Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> innerExecute(
        String name, CompletionSuggestionContext suggestionContext, IndexSearcher searcher,
        CharsRefBuilder spare) throws IOException {
    if (suggestionContext.fieldType() == null) {
        throw new ElasticsearchException(
                "Field [" + suggestionContext.getField() + "] is not a completion suggest field");
    }/*from  ww w  .  ja v  a2s  . c o m*/
    final IndexReader indexReader = searcher.getIndexReader();
    CompletionSuggestion completionSuggestion = new CompletionSuggestion(name, suggestionContext.getSize());
    spare.copyUTF8Bytes(suggestionContext.getText());

    CompletionSuggestion.Entry completionSuggestEntry = new CompletionSuggestion.Entry(
            new StringText(spare.toString()), 0, spare.length());
    completionSuggestion.addTerm(completionSuggestEntry);

    String fieldName = suggestionContext.getField();
    Map<String, CompletionSuggestion.Entry.Option> results = Maps
            .newHashMapWithExpectedSize(indexReader.leaves().size() * suggestionContext.getSize());
    for (LeafReaderContext atomicReaderContext : indexReader.leaves()) {
        LeafReader atomicReader = atomicReaderContext.reader();
        Terms terms = atomicReader.fields().terms(fieldName);
        if (terms instanceof Completion090PostingsFormat.CompletionTerms) {
            final Completion090PostingsFormat.CompletionTerms lookupTerms = (Completion090PostingsFormat.CompletionTerms) terms;
            final Lookup lookup = lookupTerms.getLookup(suggestionContext.fieldType(), suggestionContext);
            if (lookup == null) {
                // we don't have a lookup for this segment.. this might be possible if a merge dropped all
                // docs from the segment that had a value in this segment.
                continue;
            }
            List<Lookup.LookupResult> lookupResults = lookup.lookup(spare.get(), false,
                    suggestionContext.getSize());
            for (Lookup.LookupResult res : lookupResults) {

                final String key = res.key.toString();
                final float score = res.value;
                final CompletionSuggestion.Entry.Option value = results.get(key);
                if (value == null) {
                    final CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(
                            new StringText(key), score,
                            res.payload == null ? null : new BytesArray(res.payload));
                    results.put(key, option);
                } else if (value.getScore() < score) {
                    value.setScore(score);
                    value.setPayload(res.payload == null ? null : new BytesArray(res.payload));
                }
            }
        }
    }
    final List<CompletionSuggestion.Entry.Option> options = new ArrayList<>(results.values());
    CollectionUtil.introSort(options, scoreComparator);

    int optionCount = Math.min(suggestionContext.getSize(), options.size());
    for (int i = 0; i < optionCount; i++) {
        completionSuggestEntry.addOption(options.get(i));
    }

    return completionSuggestion;
}