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

The following examples show how to use org.apache.lucene.util.BytesRefBuilder#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: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private BytesRef valueForSearch(Object value) {
    if (value == null) return null;
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(parseValue(value), 0, bytesRef); // 0 because of exact match
    return bytesRef.get();
}
 
Example 2
Source File: TermBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef term(Long value) {
    BytesRefBuilder builder = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(value, 0, builder);
    return builder.get();
}
 
Example 3
Source File: GroupConverter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static Collection<SearchGroup<BytesRef>> fromMutable(SchemaField field, Collection<SearchGroup<MutableValue>> values) {
  if (values == null) {
    return null;
  }
  FieldType fieldType = field.getType();
  List<SearchGroup<BytesRef>> result = new ArrayList<>(values.size());
  for (SearchGroup<MutableValue> original : values) {
    SearchGroup<BytesRef> converted = new SearchGroup<>();
    converted.sortValues = original.sortValues;
    if (original.groupValue.exists) {
      BytesRefBuilder binary = new BytesRefBuilder();
      fieldType.readableToIndexed(Utils.OBJECT_TO_STRING.apply(original.groupValue.toObject()), binary);
      converted.groupValue = binary.get();
    } else {
      converted.groupValue = null;
    }
    result.add(converted);
  }
  return result;
}
 
Example 4
Source File: LongFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(parseLongValue(value), 0, bytesRef);  // 0 because of exact match
    return bytesRef.get();
}
 
Example 5
Source File: GroupConverter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
static TopGroups<BytesRef> fromMutable(SchemaField field, TopGroups<MutableValue> values) {
  if (values == null) {
    return null;
  }
  
  FieldType fieldType = field.getType();

  GroupDocs<BytesRef> groupDocs[] = new GroupDocs[values.groups.length];

  for (int i = 0; i < values.groups.length; i++) {
    GroupDocs<MutableValue> original = values.groups[i];
    final BytesRef groupValue;
    if (original.groupValue.exists) {
      BytesRefBuilder binary = new BytesRefBuilder();
      fieldType.readableToIndexed(Utils.OBJECT_TO_STRING.apply(original.groupValue.toObject()), binary);
      groupValue = binary.get();
    } else {
      groupValue = null;
    }
    groupDocs[i] = new GroupDocs<>(original.score, original.maxScore, original.totalHits, original.scoreDocs, groupValue, original.groupSortValues);
  }
  
  return new TopGroups<>(values.groupSort, values.withinGroupSort, values.totalHitCount, values.totalGroupedHitCount, groupDocs, values.maxScore);
}
 
Example 6
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Returns the indexed ID for this document.  The returned BytesRef is retained across multiple calls, and should not be modified. */
public BytesRef getIndexedId() {
  if (indexedId == null) {
    IndexSchema schema = req.getSchema();
    SchemaField sf = schema.getUniqueKeyField();
    if (sf != null) {
      if (solrDoc != null) {
        SolrInputField field = solrDoc.getField(sf.getName());

        int count = field==null ? 0 : field.getValueCount();
        if (count == 0) {
          if (overwrite) {
            throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document is missing mandatory uniqueKey field: " + sf.getName());
          }
        } else if (count  > 1) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document contains multiple values for uniqueKey field: " + field);
        } else {
          BytesRefBuilder b = new BytesRefBuilder();
          sf.getType().readableToIndexed(field.getFirstValue().toString(), b);
          indexedId = b.get();
        }
      }
    }
  }
  return indexedId;
}
 
Example 7
Source File: Correction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public BytesRef join(BytesRef separator, BytesRefBuilder result, BytesRef preTag, BytesRef postTag) {
    BytesRef[] toJoin = new BytesRef[this.candidates.length];
    int len = separator.length * this.candidates.length - 1;
    for (int i = 0; i < toJoin.length; i++) {
        Candidate candidate = candidates[i];
        if (preTag == null || candidate.userInput) {
            toJoin[i] = candidate.term;
        } else {
            final int maxLen = preTag.length + postTag.length + candidate.term.length;
            final BytesRefBuilder highlighted = new BytesRefBuilder();// just allocate once
            highlighted.grow(maxLen);
            if (i == 0 || candidates[i-1].userInput) {
                highlighted.append(preTag);
            }
            highlighted.append(candidate.term);
            if (toJoin.length == i + 1 || candidates[i+1].userInput) {
                highlighted.append(postTag);
            }
            toJoin[i] = highlighted.get();
        }
        len += toJoin[i].length;
    }
    result.grow(len);
    return SuggestUtils.join(separator, result, toJoin);
}
 
Example 8
Source File: BaseEditorialTransformer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected BytesRef getKey(SolrDocument doc) {
  Object obj = doc.get(idFieldName);
  if (obj instanceof IndexableField) {
    IndexableField f = (IndexableField) obj;
    BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
    Number n = f.numericValue();
    if (n != null) {
      ft.readableToIndexed(n.toString(), bytesRefBuilder);
    } else {
      ft.readableToIndexed(f.stringValue(), bytesRefBuilder);
    }
    return bytesRefBuilder.get();
  } else if (obj instanceof String) { // Allows the idField to be stored=false, docValues=true
    return new BytesRef(((String)obj));
  }
  throw new AssertionError("Expected an IndexableField but got: " + obj.getClass());
}
 
Example 9
Source File: IpFieldMapper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(parseValue(value), 0, bytesRef); // 0 because of exact match
    return bytesRef.get();
}
 
Example 10
Source File: SearchGroupsResultTransformer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private SearchGroup<BytesRef> deserializeOneSearchGroup(SchemaField groupField, String groupValue,
    SortField[] groupSortField, List<Comparable> rawSearchGroupData) {
  SearchGroup<BytesRef> searchGroup = new SearchGroup<>();
  searchGroup.groupValue = null;
  if (groupValue != null) {
    if (groupField != null) {
      BytesRefBuilder builder = new BytesRefBuilder();
      groupField.getType().readableToIndexed(groupValue, builder);
      searchGroup.groupValue = builder.get();
    } else {
      searchGroup.groupValue = new BytesRef(groupValue);
    }
  }
  searchGroup.sortValues = rawSearchGroupData.toArray(new Comparable[rawSearchGroupData.size()]);
  for (int i = 0; i < searchGroup.sortValues.length; i++) {
    SchemaField field = groupSortField[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSortField[i].getField()) : null;
    searchGroup.sortValues[i] = ShardResultTransformerUtils.unmarshalSortValue(searchGroup.sortValues[i], field);
  }
  return searchGroup;
}
 
Example 11
Source File: FieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Unmarshals a string-based field value.
 */
protected static Object unmarshalStringSortValue(Object value) {
  if (null == value) {
    return null;
  }
  BytesRefBuilder spare = new BytesRefBuilder();
  String stringVal = (String)value;
  spare.copyChars(stringVal);
  return spare.get();
}
 
Example 12
Source File: SortFormTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
private BytesRef sortKeyFromTokenStream(TokenStream stream) throws Exception {
    TermToBytesRefAttribute termAttr = stream.getAttribute(TermToBytesRefAttribute.class);
    BytesRefBuilder b = new BytesRefBuilder();
    stream.reset();
    while (stream.incrementToken()) {
        b.append(termAttr.getBytesRef());
    }
    stream.close();
    return b.get();
}
 
Example 13
Source File: DoubleFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    long longValue = NumericUtils.doubleToSortableLong(parseDoubleValue(value));
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(longValue, 0, bytesRef);   // 0 because of exact match
    return bytesRef.get();
}
 
Example 14
Source File: FloatFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    int intValue = NumericUtils.floatToSortableInt(parseValue(value));
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.intToPrefixCoded(intValue, 0, bytesRef);   // 0 because of exact match
    return bytesRef.get();
}
 
Example 15
Source File: BytesRefs.java    From crate with Apache License 2.0 5 votes vote down vote up
public static BytesRef toBytesRef(Object value, BytesRefBuilder spare) {
    if (value == null) {
        return null;
    }
    if (value instanceof BytesRef) {
        return (BytesRef) value;
    }
    spare.copyChars(value.toString());
    return spare.get();
}
 
Example 16
Source File: SuggestUtils.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static BytesRef join(BytesRef separator, BytesRefBuilder result, BytesRef... toJoin) {
    result.clear();
    for (int i = 0; i < toJoin.length - 1; i++) {
        result.append(toJoin[i]);
        result.append(separator);
    }
    result.append(toJoin[toJoin.length-1]);
    return result.get();
}
 
Example 17
Source File: TermBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef term(Double value) {
    long longValue = NumericUtils.doubleToSortableLong(value);
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.longToPrefixCoded(longValue, 0, bytesRef);
    return bytesRef.get();
}
 
Example 18
Source File: SuggestUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected BytesRef fillBytesRef(BytesRefBuilder spare) {
    spare.copyChars(charTermAttr);
    return spare.get();
}
 
Example 19
Source File: ByteFieldMapper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public BytesRef indexedValueForSearch(Object value) {
    BytesRefBuilder bytesRef = new BytesRefBuilder();
    NumericUtils.intToPrefixCoded(parseValue(value), 0, bytesRef); // 0 because of exact match
    return bytesRef.get();
}
 
Example 20
Source File: PointField.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public BytesRef toInternalByteRef(String val) {
  final BytesRefBuilder bytes = new BytesRefBuilder();
  readableToIndexed(val, bytes);
  return bytes.get();
}