/** * @see #decodeFloat(byte[], int) * @see #encodeFloat(float) * @return the decoded float */ public static float decodeFloat(byte [] bytes){ return decodeFloat(bytes, 0); }
@Override public TokenOffsetPayloadTokenFilter create(TokenStream input) { return new TokenOffsetPayloadTokenFilter(input); } }
@Override public TypeAsPayloadTokenFilter create(TokenStream input) { return new TypeAsPayloadTokenFilter(input); } }
@Override public void inform(ResourceLoader loader) { if (encoderClass.equals("float")){ encoder = new FloatEncoder(); } else if (encoderClass.equals("integer")){ encoder = new IntegerEncoder(); } else if (encoderClass.equals("identity")){ encoder = new IdentityEncoder(); } else { encoder = loader.newInstance(encoderClass, PayloadEncoder.class); } } }
public static byte[] encodeFloat(float payload) { return encodeFloat(payload, new byte[4], 0); }
@Override public DelimitedPayloadTokenFilter create(TokenStream input) { return new DelimitedPayloadTokenFilter(input, delimiter, encoder); }
public static byte[] encodeInt(int payload){ return encodeInt(payload, new byte[4], 0); }
/** * Decode the payload that was encoded using {@link #encodeFloat(float)}. * NOTE: the length of the array must be at least offset + 4 long. * @param bytes The bytes to decode * @param offset The offset into the array. * @return The float that was encoded * * @see #encodeFloat(float) */ public static final float decodeFloat(byte [] bytes, int offset){ return Float.intBitsToFloat(decodeInt(bytes, offset)); }
/** Creates a new DelimitedPayloadTokenFilterFactory */ public DelimitedPayloadTokenFilterFactory(Map<String, String> args) { super(args); encoderClass = require(args, ENCODER_ATTR); delimiter = getChar(args, DELIMITER_ATTR, '|'); if (!args.isEmpty()) { throw new IllegalArgumentException("Unknown parameters: " + args); } }
/** Creates a new NumericPayloadTokenFilterFactory */ public NumericPayloadTokenFilterFactory(Map<String, String> args) { super(args); payload = requireFloat(args, "payload"); typeMatch = require(args, "typeMatch"); if (!args.isEmpty()) { throw new IllegalArgumentException("Unknown parameters: " + args); } }
@Override public BytesRef encode(char[] buffer) { return encode(buffer, 0, buffer.length); } }
@Override public NumericPayloadTokenFilter create(TokenStream input) { return new NumericPayloadTokenFilter(input,payload,typeMatch); } }
@Override public boolean incrementToken() throws IOException { if (input.incrementToken()) { final char[] buffer = termAtt.buffer(); final int length = termAtt.length(); for (int i = 0; i < length; i++) { if (buffer[i] == delimiter) { payAtt.setPayload(encoder.encode(buffer, i + 1, (length - (i + 1)))); termAtt.setLength(i); // simply set a new length return true; } } // we have not seen the delimiter payAtt.setPayload(null); return true; } else return false; } }
float payloadBoost() throws IOException { if (doc != docID()) { final int freq = postings.freq(); payloadBoost = 0; for (int i = 0; i < freq; ++i) { postings.nextPosition(); final BytesRef payload = postings.getPayload(); float boost; if (payload == null) { boost = 1; } else if (payload.length == 1) { boost = SmallFloat.byte315ToFloat(payload.bytes[payload.offset]); } else if (payload.length == 4) { // TODO: for bw compat only, remove this in 6.0 boost = PayloadHelper.decodeFloat(payload.bytes, payload.offset); } else { throw new IllegalStateException("Payloads are expected to have a length of 1 or 4 but got: " + payload); } payloadBoost += boost; } payloadBoost /= freq; doc = docID(); } return payloadBoost; }
public NumericPayloadTokenFilter(TokenStream input, float payload, String typeMatch) { super(input); if (typeMatch == null) { throw new IllegalArgumentException("typeMatch must not be null"); } //Need to encode the payload thePayload = new BytesRef(PayloadHelper.encodeFloat(payload)); this.typeMatch = typeMatch; }
public static byte[] encodeFloat(float payload, byte[] data, int offset){ return encodeInt(Float.floatToIntBits(payload), data, offset); }
@Override public BytesRef encode(char[] buffer) { return encode(buffer, 0, buffer.length); } }
@Override public BytesRef encode(char[] buffer, int offset, int length) { float payload = Float.parseFloat(new String(buffer, offset, length));//TODO: improve this so that we don't have to new Strings byte[] bytes = PayloadHelper.encodeFloat(payload); BytesRef result = new BytesRef(bytes); return result; } }
@Override public BytesRef encode(char[] buffer, int offset, int length) { int payload = ArrayUtil.parseInt(buffer, offset, length);//TODO: improve this so that we don't have to new Strings byte[] bytes = PayloadHelper.encodeInt(payload); BytesRef result = new BytesRef(bytes); return result; } }
@Override public final boolean incrementToken() throws IOException { if (input.incrementToken()) { byte[] data = new byte[8]; PayloadHelper.encodeInt(offsetAtt.startOffset(), data, 0); PayloadHelper.encodeInt(offsetAtt.endOffset(), data, 4); BytesRef payload = new BytesRef(data); payAtt.setPayload(payload); return true; } else { return false; } } }