@Override public boolean equals(final Object o) { if (this == o) return true; if (!super.equals(o)) return false; if (o instanceof NodeConstantScoreQuery) { final NodeConstantScoreQuery other = (NodeConstantScoreQuery) o; return ((this.filter == null) ? other.filter == null : this.filter.equals(other.filter)) && ((this.query == null) ? other.query == null : this.query.equals(other.query)) && this.levelConstraint == other.levelConstraint && this.lowerBound == other.lowerBound && this.upperBound == other.upperBound && StringUtils.equals(this.datatype, other.datatype); } return false; }
@Override public Query rewrite(final IndexReader reader) throws IOException { if (terms.isEmpty()) { final NodeBooleanQuery bq = new NodeBooleanQuery(); bq.setBoost(this.getBoost()); return bq; } else if (terms.size() == 1) { final NodeTermQuery tq = new NodeTermQuery(terms.get(0)); tq.setBoost(this.getBoost()); return tq; } else { return super.rewrite(reader); } }
@Override protected void setAncestorPointer(final NodeQuery ancestor) { super.setAncestorPointer(ancestor); // keep encapsulated query synchronised query.setAncestorPointer(ancestor); }
/** * Sets the given datatype on each descendant of the {@link DatatypeQueryNode} */ private void setDatatype(final QueryNode node, final String datatype) { final Query query = (Query) node.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID); if (query instanceof NodePrimitiveQuery) { ((NodePrimitiveQuery) query).setDatatype(datatype); } if (node.isLeaf()) { return; } for (final QueryNode child : node.getChildren()) { this.setDatatype(child, datatype); } }
@Override public void setNodeConstraint(final int lowerBound, final int upperBound) { super.setNodeConstraint(lowerBound, upperBound); // keep encapsulated query synchronised query.setNodeConstraint(lowerBound, upperBound); }
@Override public void setLevelConstraint(final int levelConstraint) { super.setLevelConstraint(levelConstraint); // keep encapsulated query synchronised query.setLevelConstraint(levelConstraint); }
@Override protected void addClause(final NodeBooleanQuery topLevel, final Term term, final int docFreq, final float boost, final TermContext states) { final NodePrimitiveQuery q = new NodeConstantScoreQuery(new NodeTermQuery(term, states)); q.setBoost(boost); topLevel.add(q, NodeBooleanClause.Occur.SHOULD); } }
openLeft = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, null, 10, true, true); openLeft.setDatatype(datatype); openRight = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 10, null, true, true); openRight.setDatatype(datatype); } else if (datatype.equals("float4")) { openLeft = NodeNumericRangeQuery .newFloatRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, null, 10f, true, true); openLeft.setDatatype(datatype); openRight = NodeNumericRangeQuery .newFloatRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 10f, null, true, true); openRight.setDatatype(datatype); } else if (datatype.equals("long4")) { openLeft = NodeNumericRangeQuery .newLongRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, null, 10l, true, true); openLeft.setDatatype(datatype); openRight = NodeNumericRangeQuery .newLongRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 10l, null, true, true); openRight.setDatatype(datatype); } else { openLeft = NodeNumericRangeQuery .newDoubleRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, null, 10d, true, true); openLeft.setDatatype(datatype); openRight = NodeNumericRangeQuery .newDoubleRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 10d, null, true, true); openRight.setDatatype(datatype);
/** * Test for float. * <p> * Numeric ranges get processed with {@link NodeNumericRangeQueryNodeProcessor}. * Single numeric values are processed with {@link NodeNumericQueryNodeProcessor}. */ @Test public void testNumericQuery4() throws Exception { final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("float", new FloatNumericAnalyzer(4)); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, datatypes); final NodePrimitiveQuery q = NodeNumericRangeQuery .newFloatRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 50.5f, 1000.34f, true, true); q.setDatatype("float"); this._assertSirenQuery(config, new LuceneProxyNodeQuery(q), "float([50.5 TO 1000.34])"); }
/** * Boolean of ranges. * <p> * Numeric ranges get processed with {@link NodeNumericRangeQueryNodeProcessor}. * Single numeric values are processed with {@link NodeNumericQueryNodeProcessor}. */ @Test public void testNumericQuery3() throws Exception { final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("int", new IntNumericAnalyzer(4)); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, datatypes); config.put(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR); final NodePrimitiveQuery r1 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 50, 100, true, true); r1.setDatatype("int"); final NodePrimitiveQuery r2 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 100, 500, true, true); r2.setDatatype("int"); final BooleanQuery bq = new BooleanQuery(); bq.add(new LuceneProxyNodeQuery(r1), BooleanClause.Occur.SHOULD); bq.add(new LuceneProxyNodeQuery(r2), BooleanClause.Occur.SHOULD); this._assertSirenQuery(config, bq, "int([50 TO 100] OR [100 TO 500])"); }
@Test public void testDatatypes4() throws Exception { final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); // Set the default datatypes final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("xsd:int", new IntNumericAnalyzer(4)); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, datatypes); // Test for custom datatypes final BooleanQuery bq1 = new BooleanQuery(); final NodePrimitiveQuery range1 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 1, 1, true, true); range1.setDatatype("xsd:int"); bq1.add(new LuceneProxyNodeQuery(range1), BooleanClause.Occur.MUST); final NodePrimitiveQuery range2 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 2, 2, true, true); range2.setDatatype("xsd:int"); bq1.add(new LuceneProxyNodeQuery(range2), BooleanClause.Occur.MUST_NOT); this._assertSirenQuery(config, bq1, "+xsd:int(1) -xsd:int(2)"); this._assertSirenQuery(config, bq1, "xsd:int(+1 -2)"); }
@Test public void testDatatypes1() throws Exception { final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); // Set the default datatypes final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("xsd:int", new IntNumericAnalyzer(4)); final Analyzer dateAnalyser = new Analyzer() { @Override protected TokenStreamComponents createComponents(final String fieldName, final Reader reader) { final WhitespaceTokenizer t = new WhitespaceTokenizer(LuceneTestCase.TEST_VERSION_CURRENT, reader); final TokenStream ts = new LowerCaseFilter(LuceneTestCase.TEST_VERSION_CURRENT, t); return new TokenStreamComponents(t, ts); } }; datatypes.put("xsd:date", dateAnalyser); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, datatypes); // Test for custom datatypes final BooleanQuery bq1 = new BooleanQuery(); final NodePrimitiveQuery range1 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 12, 21, true, true); range1.setDatatype("xsd:int"); bq1.add(new LuceneProxyNodeQuery(range1), BooleanClause.Occur.MUST); final Query npq = npq("12", "oct", "2012").setDatatype("xsd:date") .getLuceneProxyQuery(); bq1.add(npq, BooleanClause.Occur.MUST); this._assertSirenQuery(config, bq1, "xsd:int([12 TO 21]) xsd:date(\"12 Oct 2012\")"); }
final NodePrimitiveQuery range1To10 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 1, 10, true, true); range1To10.setDatatype("xsd:int"); final NodePrimitiveQuery range20To40 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 20, 40, true, true); range20To40.setDatatype("xsd:int"); bq1.add(new LuceneProxyNodeQuery(range1To10), BooleanClause.Occur.SHOULD); bq1.add(new LuceneProxyNodeQuery(range20To40), BooleanClause.Occur.SHOULD); final NodePrimitiveQuery range10To15 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 10, 15, true, true); range10To15.setDatatype("xsd:int"); final NodePrimitiveQuery range50To55 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 50, 55, true, true); range50To55.setDatatype("xsd:int"); bq2.add(new LuceneProxyNodeQuery(range10To15), BooleanClause.Occur.SHOULD); bq2.add(new LuceneProxyNodeQuery(range50To55), BooleanClause.Occur.SHOULD);
final NodePrimitiveQuery range1 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 12, 21, true, true); range1.setDatatype("xsd:int"); bq2.add(new LuceneProxyNodeQuery(range1), BooleanClause.Occur.MUST); final NodePrimitiveQuery range2 = NodeNumericRangeQuery .newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 4, 12, 12, true, true); range2.setDatatype("xsd:int"); bq2.add(new LuceneProxyNodeQuery(range2), BooleanClause.Occur.MUST);