Java Code Examples for org.apache.lucene.util.CharsRefBuilder#get()

The following examples show how to use org.apache.lucene.util.CharsRefBuilder#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SynonymMap.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Sugar: just joins the provided terms with {@link
 *  SynonymMap#WORD_SEPARATOR}.  reuse and its chars
 *  must not be null. */
public static CharsRef join(String[] words, CharsRefBuilder reuse) {
  int upto = 0;
  char[] buffer = reuse.chars();
  for (String word : words) {
    final int wordLen = word.length();
    final int needed = (0 == upto ? wordLen : 1 + upto + wordLen); // Add 1 for WORD_SEPARATOR
    if (needed > buffer.length) {
      reuse.grow(needed);
      buffer = reuse.chars();
    }
    if (upto > 0) {
      buffer[upto++] = SynonymMap.WORD_SEPARATOR;
    }

    word.getChars(0, wordLen, buffer, upto);
    upto += wordLen;
  }
  reuse.setLength(upto);
  return reuse.get();
}
 
Example 2
Source File: SynonymMap.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Sugar: analyzes the text with the analyzer and
 *  separates by {@link SynonymMap#WORD_SEPARATOR}.
 *  reuse and its chars must not be null. */
public CharsRef analyze(String text, CharsRefBuilder reuse) throws IOException {
  try (TokenStream ts = analyzer.tokenStream("", text)) {
    CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
    PositionIncrementAttribute posIncAtt = ts.addAttribute(PositionIncrementAttribute.class);
    ts.reset();
    reuse.clear();
    while (ts.incrementToken()) {
      int length = termAtt.length();
      if (length == 0) {
        throw new IllegalArgumentException("term: " + text + " analyzed to a zero-length token");
      }
      if (posIncAtt.getPositionIncrement() != 1) {
        throw new IllegalArgumentException("term: " + text + " analyzed to a token (" + termAtt +
                                           ") with position increment != 1 (got: " + posIncAtt.getPositionIncrement() + ")");
      }
      reuse.grow(reuse.length() + length + 1); /* current + word + separator */
      int end = reuse.length();
      if (reuse.length() > 0) {
        reuse.setCharAt(end++, SynonymMap.WORD_SEPARATOR);
        reuse.setLength(reuse.length() + 1);
      }
      System.arraycopy(termAtt.buffer(), 0, reuse.chars(), end, length);
      reuse.setLength(reuse.length() + length);
    }
    ts.end();
  }
  if (reuse.length() == 0) {
    throw new IllegalArgumentException("term: " + text + " was completely eliminated by analyzer");
  }
  return reuse.get();
}
 
Example 3
Source File: EnumFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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());
  output.setLength(stringValue.length());
  stringValue.getChars(0, output.length(), output.chars(), 0);
  return output.get();
}
 
Example 4
Source File: BoolField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public CharsRef indexedToReadable(BytesRef input, CharsRefBuilder charsRef) {
  if (input.length > 0 && input.bytes[input.offset] == 'T') {
    charsRef.copyChars(TRUE);
  } else {
    charsRef.copyChars(FALSE);
  }
  return charsRef.get();
}
 
Example 5
Source File: PointField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public CharsRef indexedToReadable(BytesRef indexedForm, CharsRefBuilder charsRef) {
  final String value = indexedToReadable(indexedForm);
  charsRef.grow(value.length());
  charsRef.setLength(value.length());
  value.getChars(0, charsRef.length(), charsRef.chars(), 0);
  return charsRef.get();
}
 
Example 6
Source File: TrieField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public CharsRef indexedToReadable(BytesRef indexedForm, CharsRefBuilder charsRef) {
  final String value;
  switch (type) {
    case INTEGER:
      value = Integer.toString( LegacyNumericUtils.prefixCodedToInt(indexedForm) );
      break;
    case FLOAT:
      value = Float.toString( NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(indexedForm)) );
      break;
    case LONG:
      value = Long.toString( LegacyNumericUtils.prefixCodedToLong(indexedForm) );
      break;
    case DOUBLE:
      value = Double.toString( NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(indexedForm)) );
      break;
    case DATE:
      value = Instant.ofEpochMilli(LegacyNumericUtils.prefixCodedToLong(indexedForm)).toString();
      break;
    default:
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
  }
  charsRef.grow(value.length());
  charsRef.setLength(value.length());
  value.getChars(0, charsRef.length(), charsRef.chars(), 0);
  return charsRef.get();
}
 
Example 7
Source File: EnumField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public CharsRef indexedToReadable(BytesRef input, CharsRefBuilder output) {
  final Integer intValue = LegacyNumericUtils.prefixCodedToInt(input);
  final String stringValue = enumMapping.intValueToStringValue(intValue);
  output.grow(stringValue.length());
  output.setLength(stringValue.length());
  stringValue.getChars(0, output.length(), output.chars(), 0);
  return output.get();
}
 
Example 8
Source File: FieldType.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Given an indexed term, append the human readable representation*/
public CharsRef indexedToReadable(BytesRef input, CharsRefBuilder output) {
  output.copyUTF8Bytes(input);
  return output.get();
}