org.elasticsearch.common.lucene.BytesRefs Java Examples

The following examples show how to use org.elasticsearch.common.lucene.BytesRefs. 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: LiteralValueFormatter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void format(Object value, StringBuilder builder) {
    if (value == null) {
        builder.append("NULL");
    } else if (value instanceof Map) {
        formatMap((Map<String, Object>) value, builder);
    } else if(value instanceof Set) {
        formatIterable(Sorted.sortRecursive((Collection) value), builder);
    } else if(value instanceof Collection) {
        formatIterable((Iterable<?>) value, builder);
    } else if (value instanceof Object[]) {
        formatIterable(ImmutableList.copyOf((Object[]) value), builder);

    } else if (value.getClass().isArray()) {
        formatArray(value, builder);
    } else if (value instanceof CharSequence || value instanceof Character || value instanceof BytesRef) {
        builder.append(Literals.quoteStringLiteral(BytesRefs.toString(value)));
    } else {
        builder.append(value.toString());
    }

}
 
Example #2
Source File: GroupByOptimizedIterator.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Iterable<Row> getRows(Map<BytesRef, Object[]> groupedStates,
                                     RamAccounting ramAccounting,
                                     List<AggregationContext> aggregations,
                                     AggregateMode mode) {
    return () -> groupedStates.entrySet().stream()
        .map(new Function<Map.Entry<BytesRef, Object[]>, Row>() {

            final Object[] cells = new Object[1 + aggregations.size()];
            final RowN row = new RowN(cells);

            @Override
            public Row apply(Map.Entry<BytesRef, Object[]> entry) {
                cells[0] = BytesRefs.toString(entry.getKey());
                Object[] states = entry.getValue();
                for (int i = 0, c = 1; i < states.length; i++, c++) {
                    //noinspection unchecked
                    cells[c] = mode.finishCollect(ramAccounting, aggregations.get(i).function(), states[i]);
                }
                return row;
            }
        })
        .iterator();
}
 
Example #3
Source File: LowerFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef evaluate(Input<Object>... args) {
    Object stringValue = args[0].value();
    if (stringValue == null) {
        return null;
    }

    BytesRef inputByteRef = BytesRefs.toBytesRef(stringValue);

    char[] ref = new char[inputByteRef.length];
    int len = UnicodeUtil.UTF8toUTF16(inputByteRef.bytes, inputByteRef.offset, inputByteRef.length, ref);
    charUtils.toLowerCase(ref, 0, len);

    byte[] res = new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * len];
    len = UnicodeUtil.UTF16toUTF8(ref, 0, len, res);
    return new BytesRef(res, 0, len);
}
 
Example #4
Source File: UpperFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public BytesRef evaluate(Input<Object>... args) {
    Object stringValue = args[0].value();
    if (stringValue == null) {
        return null;
    }

    BytesRef inputByteRef = BytesRefs.toBytesRef(stringValue);

    char[] ref = new char[inputByteRef.length];
    int len = UnicodeUtil.UTF8toUTF16(inputByteRef.bytes, inputByteRef.offset, inputByteRef.length, ref);
    charUtils.toUpperCase(ref, 0, len);

    byte[] res = new byte[UnicodeUtil.MAX_UTF8_BYTES_PER_CHAR * len];
    len = UnicodeUtil.UTF16toUTF8(ref, 0, len, res);
    return new BytesRef(res, 0, len);
}
 
Example #5
Source File: PartitionInfos.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
private static Map<String, Object> buildValuesMap(PartitionName partitionName, MappingMetaData mappingMetaData) throws Exception{
    int i = 0;
    Map<String, Object> valuesMap = new HashMap<>();
    Iterable<Tuple<ColumnIdent, DataType>> partitionColumnInfoIterable = PartitionedByMappingExtractor.extractPartitionedByColumns(mappingMetaData.sourceAsMap());
    for (Tuple<ColumnIdent, DataType> columnInfo : partitionColumnInfoIterable) {
        String columnName = columnInfo.v1().sqlFqn();
        // produce string type values as string, not bytesref
        Object value = BytesRefs.toString(partitionName.values().get(i));
        if (!columnInfo.v2().equals(DataTypes.STRING)) {
            value = columnInfo.v2().value(value);
        }
        valuesMap.put(columnName, value);
        i++;
    }
    return valuesMap;
}
 
Example #6
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public MatchQueryBuilder(MapperService mapperService,
                         IndexCache indexCache,
                         @Nullable BytesRef matchType,
                         @Nullable Map options) throws IOException {
    this.mapperService = mapperService;
    this.indexCache = indexCache;
    if (matchType == null) {
        this.matchType = MultiMatchQueryBuilder.Type.BEST_FIELDS;
    } else {
        this.matchType = SUPPORTED_TYPES.get(matchType);
        if (this.matchType == null) {
            throw illegalMatchType(BytesRefs.toString(matchType));
        }
    }
    this.options = OptionParser.parse(this.matchType, options);
}
 
Example #7
Source File: ExtendedFsStats.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Info(String path,
            @Nullable String dev,
            long total,
            long free,
            long available,
            long used,
            long diskReads,
            long diskWrites,
            long diskReadBytes,
            long diskWriteBytes) {
    this.path = new BytesRef(path);
    this.dev = BytesRefs.toBytesRef(dev);
    this.total = total;
    this.free = free;
    this.available = available;
    this.used = used;
    this.diskReads = diskReads;
    this.diskWrites = diskWrites;
    this.diskReadBytes = diskReadBytes;
    this.diskWriteBytes = diskWriteBytes;
}
 
Example #8
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Query apply(Function input, Context context) throws IOException {
    Tuple<Reference, Literal> prepare = prepare(input);
    if (prepare == null) { return null; }
    String fieldName = prepare.v1().info().ident().columnIdent().fqn();
    Object value = prepare.v2().value();

    if (value instanceof BytesRef) {
        RegexQuery query = new RegexQuery(new Term(fieldName, BytesRefs.toBytesRef(value)));
        query.setRegexImplementation(new JavaUtilRegexCapabilities(
                JavaUtilRegexCapabilities.FLAG_CASE_INSENSITIVE |
                JavaUtilRegexCapabilities.FLAG_UNICODE_CASE));
        return query;
    }
    throw new IllegalArgumentException("Can only use ~* with patterns of type string");
}
 
Example #9
Source File: ShardRecoveryExpression.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addChildImplementations(final RecoveryState recoveryState) {
    childImplementations.put(TOTAL_TIME, new SimpleObjectExpression<Long>() {
        @Override
        public Long value() {
            return recoveryState.getTimer().time();
        }
    });
    childImplementations.put(STAGE, new SimpleObjectExpression<BytesRef>() {
        @Override
        public BytesRef value() {
            return BytesRefs.toBytesRef(recoveryState.getStage().name());
        }
    });
    childImplementations.put(TYPE, new SimpleObjectExpression<BytesRef>() {
        @Override
        public BytesRef value() {
            return BytesRefs.toBytesRef(recoveryState.getType().name());
        }
    });
    childImplementations.put(SIZE, new ShardRecoverySizeExpression(recoveryState));
    childImplementations.put(FILES, new ShardRecoveryFilesExpression(recoveryState));
}
 
Example #10
Source File: BytesRefUtils.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static String[] objectArrayToStringArray(Object[] values) {
    String[] strings = new String[values.length];
    for (int i = 0; i < strings.length; i++) {
        strings[i] = BytesRefs.toString(values[i]);
    }
    return strings;
}
 
Example #11
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected IllegalArgumentException illegalMatchType(String matchType) {
    throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "Unknown matchType \"%s\". Possible matchTypes are: %s", matchType,
            Joiner.on(", ").join(Iterables.transform(SUPPORTED_TYPES.keySet(), new Function<BytesRef, String>() {
                        @Nullable
                        @Override
                        public String apply(@Nullable BytesRef input) {
                            return BytesRefs.toString(input);
                        }
                    }

            ))));
}
 
Example #12
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Query applyArrayReference(Reference arrayReference, Literal literal, Context context) throws IOException {
    String regexString = LikeOperator.patternToRegex(BytesRefs.toString(literal.value()), LikeOperator.DEFAULT_ESCAPE, false);
    regexString = regexString.substring(1, regexString.length() - 1);
    String notLike = negateWildcard(regexString);

    return new RegexpQuery(new Term(
            arrayReference.info().ident().columnIdent().fqn(),
            notLike),
            RegexpFlag.COMPLEMENT.value()
    );
}
 
Example #13
Source File: ClientMessages.java    From crate with Apache License 2.0 5 votes vote down vote up
static void sendBindMessage(ByteBuf buffer,
                            String portalName,
                            String statementName,
                            List<Object> params) {
    buffer.writeByte('B');
    byte[] portalBytes = portalName.getBytes(StandardCharsets.UTF_8);
    byte[] statementBytes = statementName.getBytes(StandardCharsets.UTF_8);

    int beforeLengthWriterIndex = buffer.writerIndex();
    buffer.writeInt(0);
    writeCString(buffer, portalBytes);
    writeCString(buffer, statementBytes);
    buffer.writeShort(0); // formatCode use 0 to default to text for all
    buffer.writeShort(params.size());

    int paramsLength = 0;
    for (Object param : params) {
        BytesRef value = BytesRefs.toBytesRef(param);
        buffer.writeInt(value.length);
        // the strings here are _not_ zero-padded because we specify the length upfront
        buffer.writeBytes(value.bytes, value.offset, value.length);
        paramsLength += 4 + value.length;
    }
    buffer.writeShort(0); // numResultFormatCodes - 0 to default to text for all

    buffer.setInt(beforeLengthWriterIndex,
        4 +
        portalBytes.length + 1 +
        statementBytes.length + 1 +
        2 + // numFormatCodes
        2 + // numParams
        paramsLength +
        2); // numResultColumnFormatCodes
}
 
Example #14
Source File: Uid.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static BytesRef[] createUidsForTypesAndIds(Collection<String> types, Collection<?> ids) {
    BytesRef[] uids = new BytesRef[types.size() * ids.size()];
    BytesRefBuilder typeBytes = new BytesRefBuilder();
    BytesRefBuilder idBytes = new BytesRefBuilder();
    int index = 0;
    for (String type : types) {
        typeBytes.copyChars(type);
        for (Object id : ids) {
            uids[index++] = Uid.createUidAsBytes(typeBytes.get(), BytesRefs.toBytesRef(id, idBytes));
        }
    }
    return uids;
}
 
Example #15
Source File: TypeFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() == IndexOptions.NONE) {
        return new ConstantScoreQuery(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))));
    }
    return new ConstantScoreQuery(new TermQuery(createTerm(value)));
}
 
Example #16
Source File: StringFieldType.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions,
        boolean transpositions) {
    failIfNotIndexed();
    return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)),
            fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions);
}
 
Example #17
Source File: LikeQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
public static Query like(DataType dataType, @Nullable MappedFieldType fieldType, Object value, boolean ignoreCase) {
    if (fieldType == null) {
        // column doesn't exist on this index -> no match
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }

    if (dataType.equals(DataTypes.STRING)) {
        return createCaseAwareQuery(
            fieldType.name(),
            BytesRefs.toString(value),
            ignoreCase);
    }
    return fieldType.termQuery(value, null);
}
 
Example #18
Source File: Literal.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    return "Literal{" +
            "value=" + BytesRefs.toString(value) +
            ", type=" + type +
            '}';
}
 
Example #19
Source File: InsertFromValuesAnalyzedStatement.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public List<String> generatePartitions() {
    List<String> partitionValues = new ArrayList<>(partitionMaps.size());
    for (Map<String, String> map : partitionMaps) {
        List<BytesRef> values = new ArrayList<>(map.size());
        List<String> columnNames = partitionedByColumnNames();
        for (String columnName : columnNames) {
            values.add(BytesRefs.toBytesRef(map.get(columnName)));
        }
        PartitionName partitionName = new PartitionName(tableInfo().ident(), values);
        partitionValues.add(partitionName.asIndexName());
    }
    return partitionValues;
}
 
Example #20
Source File: InsertFromValuesAnalyzer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void addPrimaryKeyValue(int index, Object value, List<BytesRef> primaryKeyValues) {
    if (value == null) {
        throw new IllegalArgumentException("Primary key value must not be NULL");
    }
    if (primaryKeyValues.size() > index) {
        primaryKeyValues.add(index, BytesRefs.toBytesRef(value));
    } else {
        primaryKeyValues.add(BytesRefs.toBytesRef(value));
    }
}
 
Example #21
Source File: RowShardResolver.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void setNextRow(Row row) {
    for (CollectExpression<Row, ?> expression : visitorContext.collectExpressions()) {
        expression.setNextRow(row);
    }
    id = idFunction.apply(pkValues(primaryKeyInputs));
    if (routingInput == null) {
        routing = null;
    } else {
        routing = BytesRefs.toString(routingInput.value());
    }
}
 
Example #22
Source File: Inputs.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public BytesRef apply(@Nullable Input<?> input) {
    if (input == null) {
        return null;
    }
    return BytesRefs.toBytesRef(input.value());
}
 
Example #23
Source File: RowContextReferenceResolver.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ImmutableMap<ColumnIdent, RowCollectExpressionFactory> getSysJobsExpressions() {
    return ImmutableMap.<ColumnIdent, RowCollectExpressionFactory>builder()
            .put(SysJobsTableInfo.Columns.ID, new RowCollectExpressionFactory() {
                @Override
                public RowCollectExpression create() {
                    return new RowContextCollectorExpression<JobContext, BytesRef>() {
                        @Override
                        public BytesRef value() {
                            return BytesRefs.toBytesRef(row.id);
                        }
                    };
                }
            })
            .put(SysJobsTableInfo.Columns.STMT, new RowCollectExpressionFactory() {
                @Override
                public RowCollectExpression create() {
                    return new RowContextCollectorExpression<JobContext, BytesRef>() {
                        @Override
                        public BytesRef value() {
                            return new BytesRef(row.stmt);
                        }
                    };
                }
            })
            .put(SysJobsTableInfo.Columns.STARTED, new RowCollectExpressionFactory() {
                @Override
                public RowCollectExpression create() {
                    return new RowContextCollectorExpression<JobContext, Long>() {
                        @Override
                        public Long value() {
                            return row.started;
                        }
                    };
                }
            })
            .build();
}
 
Example #24
Source File: SubstrFunction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BytesRef evaluate(Input[] args) {
    assert (args.length >= 2 && args.length <= 3);
    if (hasNullInputs(args)) {
        return null;
    }
    final Object val = args[0].value();
    if (args.length == 3) {
        return evaluate(BytesRefs.toBytesRef(val),
                ((Number) args[1].value()).intValue(),
                ((Number) args[2].value()).intValue());

    }
    return evaluate(BytesRefs.toBytesRef(val), ((Number) args[1].value()).intValue());
}
 
Example #25
Source File: GeoJSONUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static void validateGeoJson(Map value) {
    String type = BytesRefs.toString(value.get(TYPE_FIELD));
    if (type == null) {
        throw new IllegalArgumentException(invalidGeoJSON("type field missing"));
    }

    type = GEOSJON_TYPES.get(type);
    if (type == null) {
        throw new IllegalArgumentException(invalidGeoJSON("invalid type"));
    }

    if (GEOMETRY_COLLECTION.equals(type)) {
        Object geometries = value.get(GeoJSONUtils.GEOMETRIES_FIELD);
        if (geometries == null) {
            throw new IllegalArgumentException(invalidGeoJSON("geometries field missing"));
        }

        ForEach.forEach(geometries, new ForEach.Acceptor() {
            @Override
            public void accept(Object input) {
                if (!(input instanceof Map)) {
                    throw new IllegalArgumentException(invalidGeoJSON("invalid GeometryCollection"));
                } else {
                    validateGeoJson((Map)input);
                }
            }
        });
    } else {
        Object coordinates = value.get(COORDINATES_FIELD);
        if (coordinates == null) {
            throw new IllegalArgumentException(invalidGeoJSON("coordinates field missing"));
        }
        switch(type) {
            case POINT:
                validateCoordinate(coordinates);
                break;
            case MULTI_POINT:
            case LINE_STRING:
                validateCoordinates(coordinates, 1);
                break;
            case POLYGON:
            case MULTI_LINE_STRING:
                validateCoordinates(coordinates, 2);
                break;
            case MULTI_POLYGON:
                validateCoordinates(coordinates, 3);
                break;
            default:
                // shouldn't happen
                throw new IllegalArgumentException(invalidGeoJSON("invalid type"));
        }
    }
}
 
Example #26
Source File: GeoJSONUtils.java    From crate with Apache License 2.0 4 votes vote down vote up
public static void validateGeoJson(Map value) {
    String type = BytesRefs.toString(value.get(TYPE_FIELD));
    if (type == null) {
        throw new IllegalArgumentException(invalidGeoJSON("type field missing"));
    }

    type = GEOJSON_TYPES.get(type);
    if (type == null) {
        throw new IllegalArgumentException(invalidGeoJSON("invalid type"));
    }

    if (GEOMETRY_COLLECTION.equals(type)) {
        Object geometries = value.get(GeoJSONUtils.GEOMETRIES_FIELD);
        if (geometries == null) {
            throw new IllegalArgumentException(invalidGeoJSON("geometries field missing"));
        }

        ForEach.forEach(geometries, input -> {
            if (!(input instanceof Map)) {
                throw new IllegalArgumentException(invalidGeoJSON("invalid GeometryCollection"));
            } else {
                validateGeoJson((Map) input);
            }
        });
    } else {
        Object coordinates = value.get(COORDINATES_FIELD);
        if (coordinates == null) {
            throw new IllegalArgumentException(invalidGeoJSON("coordinates field missing"));
        }
        switch (type) {
            case POINT:
                validateCoordinate(coordinates);
                break;
            case MULTI_POINT:
            case LINE_STRING:
                validateCoordinates(coordinates, 1);
                break;
            case POLYGON:
            case MULTI_LINE_STRING:
                validateCoordinates(coordinates, 2);
                break;
            case MULTI_POLYGON:
                validateCoordinates(coordinates, 3);
                break;
            default:
                // shouldn't happen
                throw new IllegalArgumentException(invalidGeoJSON("invalid type"));
        }
    }
}
 
Example #27
Source File: TermBasedFieldType.java    From crate with Apache License 2.0 4 votes vote down vote up
/** Returns the indexed value used to construct search "values".
 *  This method is used for the default implementations of most
 *  query factory methods such as {@link #termQuery}. */
protected BytesRef indexedValueForSearch(Object value) {
    return BytesRefs.toBytesRef(value);
}
 
Example #28
Source File: MappedFieldType.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) {
    return new FuzzyQuery(createTerm(value), fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions);
}
 
Example #29
Source File: MappedFieldType.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/** Returns the indexed value used to construct search "values". */
public BytesRef indexedValueForSearch(Object value) {
    return BytesRefs.toBytesRef(value);
}
 
Example #30
Source File: InformationSchemaExpressionFactories.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static Map<ColumnIdent, RowCollectExpressionFactory> columnsFactories() {
    return ImmutableMap.<ColumnIdent, RowCollectExpressionFactory>builder()
            .put(InformationColumnsTableInfo.Columns.SCHEMA_NAME, new RowCollectExpressionFactory() {

                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression.ColumnsSchemaNameExpression();
                }
            })
            .put(InformationColumnsTableInfo.Columns.TABLE_NAME, new RowCollectExpressionFactory() {

                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression.ColumnsTableNameExpression();
                }
            })
            .put(InformationColumnsTableInfo.Columns.COLUMN_NAME, new RowCollectExpressionFactory() {

                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression.ColumnsColumnNameExpression();
                }
            })
            .put(InformationColumnsTableInfo.Columns.ORDINAL_POSITION, new RowCollectExpressionFactory() {

                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression.ColumnsOrdinalExpression();
                }
            })
            .put(InformationColumnsTableInfo.Columns.DATA_TYPE, new RowCollectExpressionFactory() {

                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression.ColumnsDataTypeExpression();
                }
            })
            .put(InformationColumnsTableInfo.Columns.IS_GENERATED, new RowCollectExpressionFactory() {
                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression<Boolean>() {
                        @Override
                        public Boolean value() {
                            return row.info instanceof GeneratedReferenceInfo;
                        }
                    };
                }
            })
            .put(InformationColumnsTableInfo.Columns.GENERATION_EXPRESSION, new RowCollectExpressionFactory() {
                @Override
                public RowCollectExpression create() {
                    return new InformationColumnsExpression<BytesRef>() {
                        @Override
                        public BytesRef value() {
                            if (row.info instanceof GeneratedReferenceInfo) {
                                return BytesRefs.toBytesRef(((GeneratedReferenceInfo) row.info).formattedGeneratedExpression());
                            }
                            return null;
                        }
                    };
                }
            }).build();
}