protected <N extends Number> NumericRangeQuery<?> numericRange( Class<N> clazz, String field, @Nullable N min, @Nullable N max, boolean minInc, boolean maxInc) { if (clazz.equals(Integer.class)) { return NumericRangeQuery.newIntRange(field, (Integer) min, (Integer) max, minInc, maxInc); } else if (clazz.equals(Double.class)) { return NumericRangeQuery.newDoubleRange(field, (Double) min, (Double) max, minInc, minInc); } else if (clazz.equals(Float.class)) { return NumericRangeQuery.newFloatRange(field, (Float) min, (Float) max, minInc, minInc); } else if (clazz.equals(Long.class)) { return NumericRangeQuery.newLongRange(field, (Long) min, (Long) max, minInc, minInc); } else if (clazz.equals(Byte.class) || clazz.equals(Short.class)) { return NumericRangeQuery.newIntRange( field, min != null ? min.intValue() : null, max != null ? max.intValue() : null, minInc, maxInc); } else { throw new IllegalArgumentException("Unsupported numeric type " + clazz.getName()); } }
public void testToQueryNumericRangeQuery() throws Exception { assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("12~0.2").defaultField(INT_FIELD_NAME).toQuery(createShardContext()); NumericRangeQuery fuzzyQuery = (NumericRangeQuery) query; assertThat(fuzzyQuery.getMin().longValue(), equalTo(12l)); assertThat(fuzzyQuery.getMax().longValue(), equalTo(12l)); }
private static void planNumericRangeQuery( final StringBuilder builder, final NumericRangeQuery<?> query) { builder.append(query.getMin()); builder.append(" TO "); builder.append(query.getMax()); builder.append(" AS "); builder.append(query.getMin().getClass().getSimpleName()); }
private void assertRange(final Query q, final Class<?> type, final Number min, final Number max) { assertThat(q, is(NumericRangeQuery.class)); final NumericRangeQuery nq = (NumericRangeQuery) q; assertThat(nq.getMin(), is(type)); assertThat(nq.getMax(), is(type)); assertThat(nq.getMin(), is(min)); assertThat(nq.getMax(), is(max)); }
public CObjList searchFiles(String comid, String share, String qstr, Sort srt) { BooleanQuery query = new BooleanQuery(); Term pstterm = new Term(CObj.PARAM_TYPE, CObj.FILE); query.add(new TermQuery(pstterm), BooleanClause.Occur.MUST); Term comterm = new Term(CObj.docString(CObj.COMMUNITYID), comid); query.add(new TermQuery(comterm), BooleanClause.Occur.MUST); NumericRangeQuery<Long> nq = NumericRangeQuery.newLongRange( CObj.docNumber(CObj.NUMBER_HAS), 0L, Long.MAX_VALUE, false, true); query.add(nq, BooleanClause.Occur.MUST); if (share != null) { Term shareterm = new Term(CObj.docString(CObj.SHARE_NAME), share); query.add(new TermQuery(shareterm), BooleanClause.Occur.MUST); } Matcher m = Pattern.compile("\\S+").matcher(qstr); if (m.find()) { StringBuilder sb = new StringBuilder(); sb.append(CObj.docStringText(CObj.NAME)); sb.append(":\""); sb.append(qstr); sb.append("\""); return search(query, sb.toString(), Integer.MAX_VALUE, srt); } else { return search(query, Integer.MAX_VALUE, srt); } }
private Query innerRangeQuery( Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable DateTimeZone timeZone, @Nullable DateMathParser forcedDateParser) { return NumericRangeQuery.newLongRange( names.indexName(), precisionStep, lowerTerm == null ? null : parseToMilliseconds( lowerTerm, !includeLower, timeZone, forcedDateParser == null ? dateMathParser : forcedDateParser), upperTerm == null ? null : parseToMilliseconds( upperTerm, includeUpper, timeZone, forcedDateParser == null ? dateMathParser : forcedDateParser), includeLower, includeUpper); }
public Concept getNextConceptToUpdate() { synchronized (lock) { IndexHits<Node> hits = conceptsForUpdate.query( new QueryContext( NumericRangeQuery.newLongRange( NamespaceConstants.DATE_UPDATED, Long.MIN_VALUE, Long.MAX_VALUE, true, true)) .sort( new Sort( new SortField(NamespaceConstants.DATE_UPDATED, SortField.LONG, false)))); if (hits.hasNext()) { Node job = hits.next(); System.out.println(job.getId()); Concept concept = getByNode(job); removeFromCU(concept); return concept; } } return null; }
@Override public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { short iValue = Short.parseShort(value); short iSim = (short) (minSim * dFuzzyFactor); return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@Override public Query createQuery() { Calendar[] dates = generate(date); /* we dont cache query because of the dates */ return NumericRangeQuery.newLongRange( getLuceneField(), dates[0].getTimeInMillis(), dates[1].getTimeInMillis(), true, true); }
public static Query createMultiIntQuery(String field, int[] ids) { BooleanQuery query = new BooleanQuery(); for (int id : ids) { query.add(NumericRangeQuery.newIntRange(field, id, id, true, true), Occur.SHOULD); } return query; }
@Override public Query fuzzyQuery( String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = Long.parseLong(value); long iSim = (long) (minSim * dFuzzyFactor); return NumericRangeQuery.newLongRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@Override public void addNumericRangeTerm( BooleanQuery booleanQuery, String field, Long startValue, Long endValue) { NumericRangeQuery<?> numericRangeQuery = NumericRangeQuery.newLongRange(field, startValue, endValue, true, true); booleanQuery.add(numericRangeQuery, BooleanClause.Occur.SHOULD); }
public void testDateRangeBoundaries() throws IOException { assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"range\" : {\n" + " \"" + DATE_FIELD_NAME + "\" : {\n" + " \"gte\": \"2014-11-05||/M\",\n" + " \"lte\": \"2014-12-08||/d\"\n" + " }\n" + " }\n" + "}\n"; Query parsedQuery = parseQuery(query).toQuery(createShardContext()).rewrite(null); assertThat(parsedQuery, instanceOf(NumericRangeQuery.class)); NumericRangeQuery rangeQuery = (NumericRangeQuery) parsedQuery; DateTime min = DateTime.parse("2014-11-01T00:00:00.000+00"); assertThat(rangeQuery.getMin().longValue(), is(min.getMillis())); assertTrue(rangeQuery.includesMin()); DateTime max = DateTime.parse("2014-12-08T23:59:59.999+00"); assertThat(rangeQuery.getMax().longValue(), is(max.getMillis())); assertTrue(rangeQuery.includesMax()); query = "{\n" + " \"range\" : {\n" + " \"" + DATE_FIELD_NAME + "\" : {\n" + " \"gt\": \"2014-11-05||/M\",\n" + " \"lt\": \"2014-12-08||/d\"\n" + " }\n" + " }\n" + "}"; parsedQuery = parseQuery(query).toQuery(createShardContext()).rewrite(null); assertThat(parsedQuery, instanceOf(NumericRangeQuery.class)); rangeQuery = (NumericRangeQuery) parsedQuery; min = DateTime.parse("2014-11-30T23:59:59.999+00"); assertThat(rangeQuery.getMin().longValue(), is(min.getMillis())); assertFalse(rangeQuery.includesMin()); max = DateTime.parse("2014-12-08T00:00:00.000+00"); assertThat(rangeQuery.getMax().longValue(), is(max.getMillis())); assertFalse(rangeQuery.includesMax()); }
/** * Returns a number of random songs. * * @param criteria Search criteria. * @return List of random songs. */ public List<MediaFile> getRandomSongs(RandomSearchCriteria criteria) { List<MediaFile> result = new ArrayList<MediaFile>(); IndexReader reader = null; try { reader = createIndexReader(SONG); Searcher searcher = new IndexSearcher(reader); BooleanQuery query = new BooleanQuery(); query.add( new TermQuery(new Term(FIELD_MEDIA_TYPE, MediaFile.MediaType.MUSIC.name().toLowerCase())), BooleanClause.Occur.MUST); if (criteria.getGenre() != null) { String genre = normalizeGenre(criteria.getGenre()); query.add(new TermQuery(new Term(FIELD_GENRE, genre)), BooleanClause.Occur.MUST); } if (criteria.getFromYear() != null || criteria.getToYear() != null) { NumericRangeQuery<Integer> rangeQuery = NumericRangeQuery.newIntRange( FIELD_YEAR, criteria.getFromYear(), criteria.getToYear(), true, true); query.add(rangeQuery, BooleanClause.Occur.MUST); } List<SpanTermQuery> musicFolderQueries = new ArrayList<SpanTermQuery>(); for (MusicFolder musicFolder : criteria.getMusicFolders()) { musicFolderQueries.add( new SpanTermQuery(new Term(FIELD_FOLDER, musicFolder.getPath().getPath()))); } query.add( new SpanOrQuery(musicFolderQueries.toArray(new SpanQuery[musicFolderQueries.size()])), BooleanClause.Occur.MUST); TopDocs topDocs = searcher.search(query, null, Integer.MAX_VALUE); List<ScoreDoc> scoreDocs = Lists.newArrayList(topDocs.scoreDocs); Random random = new Random(System.currentTimeMillis()); while (!scoreDocs.isEmpty() && result.size() < criteria.getCount()) { int index = random.nextInt(scoreDocs.size()); Document doc = searcher.doc(scoreDocs.remove(index).doc); int id = Integer.valueOf(doc.get(FIELD_ID)); try { addIfNotNull(mediaFileService.getMediaFile(id), result); } catch (Exception x) { LOG.warn("Failed to get media file " + id); } } } catch (Throwable x) { LOG.error("Failed to search or random songs.", x); } finally { FileUtil.closeQuietly(reader); } return result; }
@Override public Query rangeQuery( Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newIntRange( name(), numericPrecisionStep(), lowerTerm == null ? null : (int) parseValue(lowerTerm), upperTerm == null ? null : (int) parseValue(upperTerm), includeLower, includeUpper); }
private static JsonObject convertNumericRangeQuery(NumericRangeQuery<?> query) { JsonObject interval = new JsonObject(); if (query.getMin() != null) { interval.addProperty(query.includesMin() ? "gte" : "gt", query.getMin()); } if (query.getMax() != null) { interval.addProperty(query.includesMax() ? "lte" : "lt", query.getMax()); } interval.addProperty("boost", query.getBoost()); JsonObject range = JsonBuilder.object() .add("range", JsonBuilder.object().add(query.getField(), interval)) .build(); return wrapQueryForNestedIfRequired(query.getField(), range); }
@Override public Query fuzzyQuery( Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { byte iValue = parseValue(value); byte iSim = fuzziness.asByte(); return NumericRangeQuery.newIntRange( name(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
@Override public Query fuzzyQuery( String value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { int iValue = Integer.parseInt(value); int iSim = fuzziness.asInt(); return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@Override public Query fuzzyQuery( String value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { double iValue = Double.parseDouble(value); double iSim = fuzziness.asDouble(); return NumericRangeQuery.newDoubleRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@Override public Query fuzzyQuery( String value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { short iValue = Short.parseShort(value); short iSim = fuzziness.asShort(); return NumericRangeQuery.newIntRange( names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true); }
@Override public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { short iValue = Short.parseShort(value); short iSim; try { iSim = Short.parseShort(minSim); } catch (NumberFormatException e) { iSim = (short) Float.parseFloat(minSim); } return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@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 fuzzyQuery( String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = Long.parseLong(value); long iSim; try { iSim = Long.parseLong(minSim); } catch (NumberFormatException e) { iSim = (long) Double.parseDouble(minSim); } return NumericRangeQuery.newLongRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
@Override public Query fuzzyQuery( String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) { byte iValue = Byte.parseByte(value); byte iSim; try { iSim = Byte.parseByte(minSim); } catch (NumberFormatException e) { iSim = (byte) Float.parseFloat(minSim); } return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }
// Could be based on creator and sequence number, but // not enough to worry about. Just send all. public CObjList getCommunities(String creator, long first, long last) { BooleanQuery bq = new BooleanQuery(); Term typterm = new Term(CObj.PARAM_TYPE, CObj.COMMUNITY); bq.add(new TermQuery(typterm), BooleanClause.Occur.MUST); Term memterm = new Term(CObj.docString(CObj.CREATOR), creator); bq.add(new TermQuery(memterm), BooleanClause.Occur.MUST); NumericRangeQuery<Long> nq = NumericRangeQuery.newLongRange(CObj.docNumber(CObj.SEQNUM), first, last, true, true); bq.add(nq, BooleanClause.Occur.MUST); return search(bq, Integer.MAX_VALUE); }
@Test public void testQueryNumeric() { Float boost = 0.7f; Object[] values = new Object[] {1, 2, 3}; Schema schema = schema().mapper("name", integerMapper()).build(); ContainsCondition condition = new ContainsCondition(boost, "name", values); Query query = condition.query(schema); assertNotNull("Query is not built", query); assertEquals("Query type is wrong", BooleanQuery.class, query.getClass()); BooleanQuery booleanQuery = (BooleanQuery) query; assertEquals("Boost is not set", 0.7f, query.getBoost(), 0); BooleanClause[] clauses = booleanQuery.getClauses(); assertEquals("Query is wrong", values.length, clauses.length); for (int i = 0; i < values.length; i++) { NumericRangeQuery<?> numericRangeQuery = (NumericRangeQuery<?>) clauses[i].getQuery(); assertEquals("Query is wrong", values[i], numericRangeQuery.getMin()); assertEquals("Query is wrong", values[i], numericRangeQuery.getMax()); } }
@Override public Query rangeQuery( String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, lowerTerm == null ? null : Integer.parseInt(lowerTerm), upperTerm == null ? null : Integer.parseInt(upperTerm), includeLower, includeUpper); }
@Override public Query rangeQuery( Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, @Nullable QueryParseContext context) { return NumericRangeQuery.newIntRange( names.indexName(), precisionStep, lowerTerm == null ? null : parseValue(lowerTerm), upperTerm == null ? null : parseValue(upperTerm), includeLower, includeUpper); }
public CObjList getUnDecodedMemberships(long lastdec) { BooleanQuery bq = new BooleanQuery(); Term typterm = new Term(CObj.PARAM_TYPE, CObj.MEMBERSHIP); bq.add(new TermQuery(typterm), BooleanClause.Occur.MUST); Term nondec = new Term(CObj.docPrivate(CObj.DECODED), "false"); bq.add(new TermQuery(nondec), BooleanClause.Occur.MUST); NumericRangeQuery<Long> nq = NumericRangeQuery.newLongRange( CObj.docPrivateNumber(CObj.LASTUPDATE), lastdec, Long.MAX_VALUE, true, true); bq.add(nq, BooleanClause.Occur.MUST); return search(bq, Integer.MAX_VALUE); }
@Override public Query fuzzyQuery( String value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { long iValue = ipToLong(value); long iSim; try { iSim = ipToLong(fuzziness.asString()); } catch (ElasticsearchIllegalArgumentException e) { iSim = fuzziness.asLong(); } return NumericRangeQuery.newLongRange( names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true); }