@Override public Filter regexpFilter( Object value, int flags, int maxDeterminizedStates, @Nullable QueryParseContext context) { if (fieldType.indexOptions() != IndexOptions.NONE || context == null) { return super.regexpFilter(value, flags, maxDeterminizedStates, context); } Collection<String> queryTypes = context.queryTypes(); if (queryTypes.size() == 1) { return new RegexpFilter( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes( Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value))), flags, maxDeterminizedStates); } XBooleanFilter filter = new XBooleanFilter(); for (String queryType : queryTypes) { filter.add( new RegexpFilter( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value))), flags, maxDeterminizedStates), BooleanClause.Occur.SHOULD); } return filter; }
@Override protected void doAssertLuceneQuery( RangeQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException { if (getCurrentTypes().length == 0 || (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false)) { assertThat(query, instanceOf(TermRangeQuery.class)); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName())); assertThat(termRangeQuery.getLowerTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.from()))); assertThat(termRangeQuery.getUpperTerm(), equalTo(BytesRefs.toBytesRef(queryBuilder.to()))); assertThat(termRangeQuery.includesLower(), equalTo(queryBuilder.includeLower())); assertThat(termRangeQuery.includesUpper(), equalTo(queryBuilder.includeUpper())); } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME)) { // we can't properly test unmapped dates because LateParsingQuery is package private } else if (queryBuilder.fieldName().equals(INT_FIELD_NAME)) { assertThat(query, instanceOf(NumericRangeQuery.class)); NumericRangeQuery numericRangeQuery = (NumericRangeQuery) query; assertThat(numericRangeQuery.getField(), equalTo(queryBuilder.fieldName())); assertThat(numericRangeQuery.getMin(), equalTo(queryBuilder.from())); assertThat(numericRangeQuery.getMax(), equalTo(queryBuilder.to())); assertThat(numericRangeQuery.includesMin(), equalTo(queryBuilder.includeLower())); assertThat(numericRangeQuery.includesMax(), equalTo(queryBuilder.includeUpper())); } else { throw new UnsupportedOperationException(); } }
@Override public Query prefixQuery( Object value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) { if (fieldType.indexOptions() != IndexOptions.NONE || context == null) { return super.prefixQuery(value, method, context); } Collection<String> queryTypes = context.queryTypes(); if (queryTypes.size() == 1) { PrefixQuery prefixQuery = new PrefixQuery( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes( Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value)))); if (method != null) { prefixQuery.setRewriteMethod(method); } return prefixQuery; } BooleanQuery query = new BooleanQuery(); for (String queryType : queryTypes) { PrefixQuery prefixQuery = new PrefixQuery( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))); if (method != null) { prefixQuery.setRewriteMethod(method); } query.add(prefixQuery, BooleanClause.Occur.SHOULD); } return query; }
@Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { if (!fieldType.indexed()) { return new PrefixFilter( new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))); } return new TermFilter(names().createIndexNameTerm(BytesRefs.toBytesRef(value))); }
@Override public Query termsQuery(List values, @Nullable QueryShardContext context) { if (context == null) { return super.termsQuery(values, context); } List<String> types = new ArrayList<>(context.getMapperService().types().size()); for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) { if (!documentMapper.parentFieldMapper().active()) { types.add(documentMapper.type()); } } List<BytesRef> bValues = new ArrayList<>(values.size()); for (Object value : values) { BytesRef bValue = BytesRefs.toBytesRef(value); if (Uid.hasDelimiter(bValue)) { bValues.add(bValue); } else { // we use all non child types, cause we don't know if its exact or not... for (String type : types) { bValues.add(Uid.createUidAsBytes(type, bValue)); } } } return new TermsQuery(name(), bValues); }
@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(); // FIXME: nobody knows how Strings can arrive here if (value instanceof String) { if (isPcrePattern(value)) { return new RegexQuery(new Term(fieldName, (String) value)); } else { return toLuceneRegexpQuery(fieldName, BytesRefs.toBytesRef(value), context); } } if (value instanceof BytesRef) { if (isPcrePattern(value)) { return new RegexQuery(new Term(fieldName, (BytesRef) value)); } else { return toLuceneRegexpQuery(fieldName, (BytesRef) value, context); } } throw new IllegalArgumentException("Can only use ~ with patterns of type string"); }
@Override protected Query applyArrayReference( Reference arrayReference, Literal literal, Context context) throws IOException { String notLike = negateWildcard(convertWildcardToRegex(BytesRefs.toString(literal.value()))); return new RegexpQuery( new Term(arrayReference.info().ident().columnIdent().fqn(), notLike), RegexpFlag.COMPLEMENT.value()); }
@Override public Query regexpQuery( String value, int flags, int maxDeterminizedStates, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) { if (indexOptions() != IndexOptions.NONE || context == null) { return super.regexpQuery(value, flags, maxDeterminizedStates, method, context); } Collection<String> queryTypes = context.queryTypes(); if (queryTypes.size() == 1) { RegexpQuery regexpQuery = new RegexpQuery( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes( Iterables.getFirst(queryTypes, null), BytesRefs.toBytesRef(value))), flags, maxDeterminizedStates); if (method != null) { regexpQuery.setRewriteMethod(method); } return regexpQuery; } BooleanQuery.Builder query = new BooleanQuery.Builder(); for (String queryType : queryTypes) { RegexpQuery regexpQuery = new RegexpQuery( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value))), flags, maxDeterminizedStates); if (method != null) { regexpQuery.setRewriteMethod(method); } query.add(regexpQuery, BooleanClause.Occur.SHOULD); } return query.build(); }
@Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { if (context == null) { return super.termFilter(value, context); } BytesRef bValue = BytesRefs.toBytesRef(value); // we use all types, cause we don't know if its exact or not... BytesRef[] typesValues = new BytesRef[context.mapperService().types().size()]; int i = 0; for (String type : context.mapperService().types()) { typesValues[i++] = Uid.createUidAsBytes(type, bValue); } return new TermsFilter(names.indexName(), typesValues); }
@Override public Filter termsFilter(List values, @Nullable QueryParseContext context) { if (context == null) { return super.termsFilter(values, context); } List<BytesRef> bValues = new ArrayList<BytesRef>(values.size()); for (Object value : values) { BytesRef bValue = BytesRefs.toBytesRef(value); // we use all types, cause we don't know if its exact or not... for (String type : context.mapperService().types()) { bValues.add(Uid.createUidAsBytes(type, bValue)); } } return new TermsFilter(names.indexName(), bValues); }
/** Test the WordScorer emitted by the smoothing model */ public void testBuildWordScorer() throws IOException { SmoothingModel testModel = createTestModel(); Map<String, Analyzer> mapping = new HashMap<>(); mapping.put("field", new WhitespaceAnalyzer()); PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new WhitespaceAnalyzer(), mapping); IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(wrapper)); Document doc = new Document(); doc.add(new Field("field", "someText", TextField.TYPE_NOT_STORED)); writer.addDocument(doc); DirectoryReader ir = DirectoryReader.open(writer); WordScorer wordScorer = testModel .buildWordScorerFactory() .newScorer( ir, MultiFields.getTerms(ir, "field"), "field", 0.9d, BytesRefs.toBytesRef(" ")); assertWordScorer(wordScorer, testModel); }
@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(); // FIXME: nobody knows how Strings can arrive here if (value instanceof String || 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"); }
@Override public Query prefixQuery( String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context) { if (indexOptions() != IndexOptions.NONE || context == null) { return super.prefixQuery(value, method, context); } Collection<String> queryTypes = context.queryTypes(); BooleanQuery.Builder query = new BooleanQuery.Builder(); for (String queryType : queryTypes) { PrefixQuery prefixQuery = new PrefixQuery( new Term( UidFieldMapper.NAME, Uid.createUidAsBytes(queryType, BytesRefs.toBytesRef(value)))); if (method != null) { prefixQuery.setRewriteMethod(method); } query.add(prefixQuery, BooleanClause.Occur.SHOULD); } return query.build(); }
public void testSimpleStreams() throws Exception { assumeTrue("requires a 64-bit JRE ... ?!", Constants.JRE_IS_64BIT); BytesStreamOutput out = new BytesStreamOutput(); out.writeBoolean(false); out.writeByte((byte) 1); out.writeShort((short) -1); out.writeInt(-1); out.writeVInt(2); out.writeLong(-3); out.writeVLong(4); out.writeOptionalLong(11234234L); out.writeFloat(1.1f); out.writeDouble(2.2); int[] intArray = {1, 2, 3}; out.writeGenericValue(intArray); int[] vIntArray = {4, 5, 6}; out.writeVIntArray(vIntArray); long[] longArray = {1, 2, 3}; out.writeGenericValue(longArray); long[] vLongArray = {4, 5, 6}; out.writeVLongArray(vLongArray); float[] floatArray = {1.1f, 2.2f, 3.3f}; out.writeGenericValue(floatArray); double[] doubleArray = {1.1, 2.2, 3.3}; out.writeGenericValue(doubleArray); out.writeString("hello"); out.writeString("goodbye"); out.writeGenericValue(BytesRefs.toBytesRef("bytesref")); out.writeStringArray(new String[] {"a", "b", "cat"}); out.writeBytesReference(new BytesArray("test")); out.writeOptionalBytesReference(new BytesArray("test")); out.writeOptionalDouble(null); out.writeOptionalDouble(1.2); out.writeTimeZone(DateTimeZone.forID("CET")); out.writeOptionalTimeZone(DateTimeZone.getDefault()); out.writeOptionalTimeZone(null); final byte[] bytes = BytesReference.toBytes(out.bytes()); StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes())); assertEquals(in.available(), bytes.length); assertThat(in.readBoolean(), equalTo(false)); assertThat(in.readByte(), equalTo((byte) 1)); assertThat(in.readShort(), equalTo((short) -1)); assertThat(in.readInt(), equalTo(-1)); assertThat(in.readVInt(), equalTo(2)); assertThat(in.readLong(), equalTo(-3L)); assertThat(in.readVLong(), equalTo(4L)); assertThat(in.readOptionalLong(), equalTo(11234234L)); assertThat((double) in.readFloat(), closeTo(1.1, 0.0001)); assertThat(in.readDouble(), closeTo(2.2, 0.0001)); assertThat(in.readGenericValue(), equalTo((Object) intArray)); assertThat(in.readVIntArray(), equalTo(vIntArray)); assertThat(in.readGenericValue(), equalTo((Object) longArray)); assertThat(in.readVLongArray(), equalTo(vLongArray)); assertThat(in.readGenericValue(), equalTo((Object) floatArray)); assertThat(in.readGenericValue(), equalTo((Object) doubleArray)); assertThat(in.readString(), equalTo("hello")); assertThat(in.readString(), equalTo("goodbye")); assertThat(in.readGenericValue(), equalTo((Object) BytesRefs.toBytesRef("bytesref"))); assertThat(in.readStringArray(), equalTo(new String[] {"a", "b", "cat"})); assertThat(in.readBytesReference(), equalTo(new BytesArray("test"))); assertThat(in.readOptionalBytesReference(), equalTo(new BytesArray("test"))); assertNull(in.readOptionalDouble()); assertThat(in.readOptionalDouble(), closeTo(1.2, 0.0001)); assertEquals(DateTimeZone.forID("CET"), in.readTimeZone()); assertEquals(DateTimeZone.getDefault(), in.readOptionalTimeZone()); assertNull(in.readOptionalTimeZone()); assertEquals(0, in.available()); in.close(); out.close(); }
@Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); boolean cache = true; CacheKeyFilter.Key cacheKey = null; String fieldName = null; Object from = null; Object to = null; boolean includeLower = true; boolean includeUpper = true; String filterName = null; String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else { if ("from".equals(currentFieldName)) { from = parser.objectBytes(); } else if ("to".equals(currentFieldName)) { to = parser.objectBytes(); } else if ("include_lower".equals(currentFieldName) || "includeLower".equals(currentFieldName)) { includeLower = parser.booleanValue(); } else if ("include_upper".equals(currentFieldName) || "includeUpper".equals(currentFieldName)) { includeUpper = parser.booleanValue(); } else if ("gt".equals(currentFieldName)) { from = parser.objectBytes(); includeLower = false; } else if ("gte".equals(currentFieldName) || "ge".equals(currentFieldName)) { from = parser.objectBytes(); includeLower = true; } else if ("lt".equals(currentFieldName)) { to = parser.objectBytes(); includeUpper = false; } else if ("lte".equals(currentFieldName) || "le".equals(currentFieldName)) { to = parser.objectBytes(); includeUpper = true; } else { throw new QueryParsingException( parseContext.index(), "[range] filter does not support [" + currentFieldName + "]"); } } } } else if (token.isValue()) { if ("_name".equals(currentFieldName)) { filterName = parser.text(); } else if ("_cache".equals(currentFieldName)) { cache = parser.booleanValue(); } else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) { cacheKey = new CacheKeyFilter.Key(parser.text()); } else { throw new QueryParsingException( parseContext.index(), "[range] filter does not support [" + currentFieldName + "]"); } } } if (fieldName == null) { throw new QueryParsingException(parseContext.index(), "No field specified for range filter"); } Filter filter = null; MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); if (smartNameFieldMappers != null) { if (smartNameFieldMappers.hasMapper()) { filter = smartNameFieldMappers .mapper() .rangeFilter(from, to, includeLower, includeUpper, parseContext); } } if (filter == null) { filter = new TermRangeFilter( fieldName, BytesRefs.toBytesRef(from), BytesRefs.toBytesRef(to), includeLower, includeUpper); } if (cache) { filter = parseContext.cacheFilter(filter, cacheKey); } filter = wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); } return filter; }
@Override public BytesRef value() { return BytesRefs.toBytesRef(row.tableParameters().get(paramName)); }