private static QueryBuilder termRange(Query q) { QueryBuilder qb = null; TermRangeQuery trq = (TermRangeQuery) q; if (!StringUtils.isBlank(trq.getField())) { String from = trq.getLowerTerm() != null ? Term.toString(trq.getLowerTerm()) : "*"; String to = trq.getUpperTerm() != null ? Term.toString(trq.getUpperTerm()) : "*"; boolean nestedMode = nestedMode() && trq.getField().matches(PROPS_REGEX); qb = rangeQuery(nestedMode ? getValueFieldNameFromRange(from, to) : trq.getField()); if ("*".equals(from) && "*".equals(to)) { qb = matchAllQuery(); } if (!"*".equals(from)) { ((RangeQueryBuilder) qb).from(getNumericValue(from)).includeLower(trq.includesLower()); } if (!"*".equals(to)) { ((RangeQueryBuilder) qb).to(getNumericValue(to)).includeUpper(trq.includesUpper()); } if (nestedMode) { qb = nestedPropsQuery(keyValueBoolQuery(trq.getField(), qb)); } } return qb; }
public void testReaderChaining() throws Exception { assertTrue(reader.getRefCount() > 0); IndexReader wrappedReader = SlowCompositeReaderWrapper.wrap(reader); wrappedReader = new ParallelAtomicReader((AtomicReader) wrappedReader); IndexSearcher searcher = newSearcher(wrappedReader); TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true); searcher.search(query, 5); reader.close(); // close original child reader try { searcher.search(query, 5); } catch (AlreadyClosedException ace) { assertEquals( "this IndexReader cannot be used anymore as one of its child readers was closed", ace.getMessage() ); } finally { // shutdown executor: in case of wrap-wrap-wrapping searcher.getIndexReader().close(); } }
@Override public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) { String f = field.getName(); BytesRef low = part1 == null ? null : getCollationKey(f, part1); BytesRef high = part2 == null ? null : getCollationKey(f, part2); if (!field.indexed() && field.hasDocValues()) { if (field.multiValued()) { return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange( field.getName(), low, high, minInclusive, maxInclusive)); } else { return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange( field.getName(), low, high, minInclusive, maxInclusive)); } } else { return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive); } }
protected String toString(TermRangeQuery termRangeQuery, String field) { StringBuilder buffer = new StringBuilder(); boolean includeLower = termRangeQuery.includesLower(); BytesRef lowerTerm = termRangeQuery.getLowerTerm(); BytesRef upperTerm = termRangeQuery.getUpperTerm(); boolean includeUpper = termRangeQuery.includesUpper(); if (!termRangeQuery.getField().equals(field)) { buffer.append(termRangeQuery.getField()); buffer.append(":"); } buffer.append(includeLower ? '[' : '{'); // TODO: all these toStrings for queries should just output the bytes, // it might not be UTF-8! buffer.append( lowerTerm != null ? ("*".equals(Term.toString(lowerTerm)) ? "\\*" : Term.toString(lowerTerm)) : "*"); buffer.append(" TO "); buffer.append( upperTerm != null ? ("*".equals(Term.toString(upperTerm)) ? "\\*" : Term.toString(upperTerm)) : "*"); buffer.append(includeUpper ? ']' : '}'); return buffer.toString(); }
/** * Builds a new {@link TermRangeQuery} instance * * @param field * Field * @param part1 * min * @param part2 * max * @param startInclusive * true if the start of the range is inclusive * @param endInclusive * true if the end of the range is inclusive * @return new {@link TermRangeQuery} instance */ protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) { final BytesRef start; final BytesRef end; if (part1 == null) { start = null; } else { start = analyzeRangeTerms ? getAnalyzer().normalize(field, part1) : new BytesRef(part1); } if (part2 == null) { end = null; } else { end = analyzeRangeTerms ? getAnalyzer().normalize(field, part2) : new BytesRef(part2); } final TermRangeQuery query = new TermRangeQuery(field, start, end, startInclusive, endInclusive); query.setRewriteMethod(multiTermRewriteMethod); return query; }
private void addLocaleSpecificTextRange(String expandedFieldName, String part1, String part2, boolean includeLower, boolean includeUpper, BooleanQuery booleanQuery, Locale locale, AnalysisMode analysisMode, IndexTokenisationMode tokenisationMode, IndexTokenisationMode preferredtokenisationMode) throws ParseException { String field = getFieldName(expandedFieldName, locale, tokenisationMode, preferredtokenisationMode); StringBuilder builder = new StringBuilder(); builder.append("\u0000").append(locale.toString()).append("\u0000").append(part1); String first = getToken(field, builder.toString(), analysisMode); if ((first == null) && (false == field.endsWith(".u"))) { first = getToken(field + ".u", builder.toString(), analysisMode); } builder = new StringBuilder(); builder.append("\u0000").append(locale.toString()).append("\u0000").append(part2); String last = getToken(field, builder.toString(), analysisMode); if ((last == null) && (false == field.endsWith(".u"))) { last = getToken(field + ".u", builder.toString(), analysisMode); } Query query = new TermRangeQuery(field, first, last, includeLower, includeUpper); booleanQuery.add(query, Occur.SHOULD); }
@Test public void testStringClose() { Map<String, ColumnMapper> map = new HashMap<>(); map.put("name", new ColumnMapperBoolean()); Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName()); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", "beta", true, true); Query query = rangeCondition.query(mappers); Assert.assertNotNull(query); Assert.assertEquals(TermRangeQuery.class, query.getClass()); Assert.assertEquals("name", ((TermRangeQuery) query).getField()); Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString()); Assert.assertEquals("beta", ((TermRangeQuery) query).getUpperTerm().utf8ToString()); Assert.assertEquals(true, ((TermRangeQuery) query).includesLower()); Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper()); Assert.assertEquals(0.5f, query.getBoost(), 0); }
@Test public void testStringOpen() { Map<String, ColumnMapper> map = new HashMap<>(); map.put("name", new ColumnMapperBoolean()); Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName()); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", null, true, false); Query query = rangeCondition.query(mappers); Assert.assertNotNull(query); Assert.assertEquals(TermRangeQuery.class, query.getClass()); Assert.assertEquals("name", ((TermRangeQuery) query).getField()); Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString()); Assert.assertEquals(null, ((TermRangeQuery) query).getUpperTerm()); Assert.assertNull(((TermRangeQuery) query).getUpperTerm()); Assert.assertEquals(true, ((TermRangeQuery) query).includesLower()); Assert.assertEquals(false, ((TermRangeQuery) query).includesUpper()); Assert.assertEquals(0.5f, query.getBoost(), 0); }
@Test public void testInetV4() { Map<String, ColumnMapper> map = new HashMap<>(); map.put("name", new ColumnMapperInet()); Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName()); RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "192.168.0.01", "192.168.0.045", true, true); Query query = rangeCondition.query(mappers); Assert.assertNotNull(query); Assert.assertEquals(TermRangeQuery.class, query.getClass()); Assert.assertEquals("name", ((TermRangeQuery) query).getField()); Assert.assertEquals("192.168.0.1", ((TermRangeQuery) query).getLowerTerm().utf8ToString()); Assert.assertEquals("192.168.0.45", ((TermRangeQuery) query).getUpperTerm().utf8ToString()); Assert.assertEquals(true, ((TermRangeQuery) query).includesLower()); Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper()); Assert.assertEquals(0.5f, query.getBoost(), 0); }
@Test public void testInetV6() { Map<String, ColumnMapper> map = new HashMap<>(); map.put("name", new ColumnMapperInet()); Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName()); RangeCondition rangeCondition = range("name").boost(0.5f) .lower("2001:DB8:2de::e13") .upper("2001:DB8:02de::e23") .includeLower(true) .includeUpper(true) .build(); Query query = rangeCondition.query(mappers); Assert.assertNotNull(query); Assert.assertEquals(TermRangeQuery.class, query.getClass()); Assert.assertEquals("name", ((TermRangeQuery) query).getField()); Assert.assertEquals("2001:db8:2de:0:0:0:0:e13", ((TermRangeQuery) query).getLowerTerm().utf8ToString()); Assert.assertEquals("2001:db8:2de:0:0:0:0:e23", ((TermRangeQuery) query).getUpperTerm().utf8ToString()); Assert.assertEquals(true, ((TermRangeQuery) query).includesLower()); Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper()); Assert.assertEquals(0.5f, query.getBoost(), 0); }
@Test public void test5() throws ParseException, IOException { parser = new SuperParser(LUCENE_VERSION, getFieldManager(new WhitespaceAnalyzer(LUCENE_VERSION)), true, null, ScoreType.SUPER, new Term("_primedoc_")); Query query = parser.parse("<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<> -<g.c:c g.d:d>"); BooleanQuery booleanQuery1 = new BooleanQuery(); booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD); booleanQuery1.add(new TermRangeQuery("a.d", new BytesRef("e"), new BytesRef("f"), false, false), Occur.SHOULD); booleanQuery1.add(new TermQuery(new Term("a.b", "b")), Occur.SHOULD); // std analyzer took the "<" out booleanQuery1.add(new TermQuery(new Term("a.test", "hello<")), Occur.SHOULD); BooleanQuery booleanQuery2 = new BooleanQuery(); booleanQuery2.add(new TermQuery(new Term("g.c", "c")), Occur.SHOULD); booleanQuery2.add(new TermQuery(new Term("g.d", "d")), Occur.SHOULD); SuperQuery superQuery1 = new SuperQuery(booleanQuery1, ScoreType.SUPER, new Term("_primedoc_")); SuperQuery superQuery2 = new SuperQuery(booleanQuery2, ScoreType.SUPER, new Term("_primedoc_")); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(superQuery1, Occur.SHOULD); booleanQuery.add(superQuery2, Occur.MUST_NOT); assertQuery(booleanQuery, query); }
@Override public void learnVocab() throws IOException { super.learnVocab(); final String field = ((LuceneIndexConfig)config).getField(); final Terms terms = MultiFields.getTerms(reader, field); final BytesRef maxTerm = terms.getMax(); final BytesRef minTerm = terms.getMin(); Query q = new TermRangeQuery(field, minTerm, maxTerm, true, true); IndexSearcher searcher = new IndexSearcher(reader); topDocs = searcher.search(q, Integer.MAX_VALUE); TermsEnum termsEnum = null; termsEnum = terms.iterator(termsEnum); termsEnum.seekCeil(new BytesRef()); BytesRef term = termsEnum.term(); while(term != null){ int p = addWordToVocab(term.utf8ToString()); vocab[p].setCn((int)termsEnum.totalTermFreq()); term = termsEnum.next(); } }
@Test public void testTermRangeQueryBigger() { JsonObject json = Json.createObjectBuilder() .add("query", Json.createObjectBuilder() .add("type", "RangeQuery") .add("rangeType", "String") .add("field", "field") .add("lowerTerm", "value") .add("upperTerm", JsonValue.NULL) .add("includeLower", JsonValue.FALSE) .add("includeUpper", JsonValue.FALSE)) .build(); QueryData q = new QueryData(new StringReader(json.toString()), queryConverter); TermRangeQuery query = TermRangeQuery.newStringRange("field", "value", null, false, false); assertEquals(query, q.query); }
@Test public void testTermRangeQueryLower() { JsonObject json = Json.createObjectBuilder() .add("query", Json.createObjectBuilder() .add("type", "RangeQuery") .add("rangeType", "String") .add("field", "field") .add("lowerTerm", JsonValue.NULL) .add("upperTerm", "value") .add("includeLower", JsonValue.TRUE) .add("includeUpper", JsonValue.TRUE)) .build(); QueryData q = new QueryData(new StringReader(json.toString()), queryConverter); TermRangeQuery query = TermRangeQuery.newStringRange("field", null, "value", true, true); assertEquals(query, q.query); }
public Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { TermRangeQuery query = (TermRangeQuery) super.getRangeQuery(field, part1, part2, inclusive); if ("price".equals(field)) { return NumericRangeQuery.newDoubleRange( "price", Double.parseDouble( query.getLowerTerm()), Double.parseDouble( query.getUpperTerm()), query.includesLower(), query.includesUpper()); } else { return query; } }
public Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException { TermRangeQuery query = (TermRangeQuery) super.getRangeQuery(field, part1, part2, inclusive); if ("pubmonth".equals(field)) { return NumericRangeQuery.newIntRange( "pubmonth", Integer.parseInt(query.getLowerTerm()), Integer.parseInt(query.getUpperTerm()), query.includesLower(), query.includesUpper()); } else { return query; } }
public void testExtractQueryMetadata_unsupportedQuery() { TermRangeQuery termRangeQuery = new TermRangeQuery("_field", null, null, true, false); UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(termRangeQuery)); assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery)); TermQuery termQuery1 = new TermQuery(new Term("_field", "_term")); BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(termQuery1, BooleanClause.Occur.SHOULD); builder.add(termRangeQuery, BooleanClause.Occur.SHOULD); BooleanQuery bq = builder.build(); e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq)); assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery)); }
public void testExtractQueryMetadata_unsupportedQueryInBoolQueryWithMustClauses() { TermRangeQuery unsupportedQuery = new TermRangeQuery("_field", null, null, true, false); TermQuery termQuery1 = new TermQuery(new Term("_field", "_term")); BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(termQuery1, BooleanClause.Occur.MUST); builder.add(unsupportedQuery, BooleanClause.Occur.MUST); BooleanQuery bq1 = builder.build(); Result result = analyze(bq1); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, termQuery1.getTerm()); TermQuery termQuery2 = new TermQuery(new Term("_field", "_longer_term")); builder = new BooleanQuery.Builder(); builder.add(termQuery1, BooleanClause.Occur.MUST); builder.add(termQuery2, BooleanClause.Occur.MUST); builder.add(unsupportedQuery, BooleanClause.Occur.MUST); bq1 = builder.build(); result = analyze(bq1); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, termQuery2.getTerm()); builder = new BooleanQuery.Builder(); builder.add(unsupportedQuery, BooleanClause.Occur.MUST); builder.add(unsupportedQuery, BooleanClause.Occur.MUST); BooleanQuery bq2 = builder.build(); UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq2)); assertThat(e.getUnsupportedQuery(), sameInstance(unsupportedQuery)); }
public void testExtractTermsAndRanges_failed() throws Exception { addQueryMapping(); TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true); DocumentMapper documentMapper = mapperService.documentMapper(typeName); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null); fieldMapper.processQuery(query, parseContext); ParseContext.Document document = parseContext.doc(); PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); assertThat(document.getFields().size(), equalTo(1)); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED)); }
@Override public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { failIfNotIndexed(); return new TermRangeQuery(name(), lowerTerm == null ? null : indexedValueForSearch(lowerTerm), upperTerm == null ? null : indexedValueForSearch(upperTerm), includeLower, includeUpper); }
private static Query GE_lat(String field, double value) { if (value == 90.0) { return EQ_lat(field, value); } else { return new TermRangeQuery (field, encodeLatitude(value),encodeLatitude(90.0),true,true); } }
private static Query LE_lat(String field, double value) { if (value == -90.0) { return EQ_lat(field, value); } else { return new TermRangeQuery(field, encodeLatitude(-90.0),encodeLatitude(value),true,true); } }
private static Query GE_lon(String field, double value) { if (value == 180.0) { return EQ_lon(field, value); } else { return new TermRangeQuery (field, encodeLongitude(value), encodeLongitude(180.0),true,true); } }
private static Query LE_lon(String field, double value) { if (value == -180.0) { return EQ_lon(field, value); } else { return new TermRangeQuery (field,encodeLongitude(-180.0),encodeLongitude(value),true,true); } }
/** * Converts a FreeTextDateQuery to a BooleanClause */ private BooleanClause convertDate(FreeTextDateQuery query) { TermRangeQuery termQuery = new TermRangeQuery(query.getField(), convertDate(query.getStart(), query), convertDate(query.getEnd(), query), query.isIncludeStart(), query.isIncludeEnd()); termQuery.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); return new BooleanClause(termQuery, Occur.SHOULD); }
@Override public TermRangeQuery build(QueryNode queryNode) throws QueryNodeException { TermRangeQueryNode rangeNode = (TermRangeQueryNode) queryNode; FieldQueryNode upper = rangeNode.getUpperBound(); FieldQueryNode lower = rangeNode.getLowerBound(); String field = StringUtils.toString(rangeNode.getField()); String lowerText = lower.getTextAsString(); String upperText = upper.getTextAsString(); if (lowerText.length() == 0) { lowerText = null; } if (upperText.length() == 0) { upperText = null; } TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(field, lowerText, upperText, rangeNode .isLowerInclusive(), rangeNode.isUpperInclusive()); MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod) queryNode .getTag(MultiTermRewriteMethodProcessor.TAG_ID); if (method != null) { rangeQuery.setRewriteMethod(method); } return rangeQuery; }
@Override protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) { if (isPass2ResolvingPhrases) { // Must use old-style RangeQuery in order to produce a BooleanQuery // that can be turned into SpanOr clause TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(field, part1, part2, startInclusive, endInclusive); rangeQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); return rangeQuery; } return super.newRangeQuery(field, part1, part2, startInclusive, endInclusive); }
private Query toRangeQuery(RangeTerm range) { return TermRangeQuery.newStringRange( range.getField(), range.hasMin() ? range.getMin() : null, range.hasMax() ? range.getMax() : null, range.getMinInclusive(), range.getMaxInclusive()); }
public void testFarsiRangeFilterCollating(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(analyzer)); Document doc = new Document(); doc.add(new TextField("content", "\u0633\u0627\u0628", Field.Store.YES)); doc.add(new StringField("body", "body", Field.Store.YES)); writer.addDocument(doc); writer.close(); IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermQuery(new Term("body", "body")); // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi // orders the U+0698 character before the U+0633 character, so the single // index Term below should NOT be returned by a TermRangeFilter with a Farsi // Collator (or an Arabic one for the case when Farsi searcher not // supported). BooleanQuery bq = new BooleanQuery(); bq.add(query, BooleanClause.Occur.MUST); bq.add(new TermRangeQuery("content", firstBeg, firstEnd, true, true), BooleanClause.Occur.FILTER); ScoreDoc[] result = searcher.search(bq, 1).scoreDocs; assertEquals("The index Term should not be included.", 0, result.length); bq = new BooleanQuery(); bq.add(query, BooleanClause.Occur.MUST); bq.add(new TermRangeQuery("content", secondBeg, secondEnd, true, true), BooleanClause.Occur.FILTER); result = searcher.search(bq, 1).scoreDocs; assertEquals("The index Term should be included.", 1, result.length); reader.close(); dir.close(); }