/** * Test for incorrect numeric values at query time. * <p> * Numeric ranges get processed with {@link NodeNumericRangeQueryNodeProcessor}. * Single numeric values are processed with {@link NodeNumericQueryNodeProcessor}. */ @Test(expected=QueryNodeException.class) public void testNumericQuery1() 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); parse(config, "int([10 TO bla])"); }
@Test public void testSirenFieldDatatypeAnalyzer() throws Exception { final IndexSchema schema = h.getCore().getSchema(); final SchemaField ntriple = schema.getField(JSON_FIELD); final FieldType tmp = ntriple.getType(); TokenizerChain ts = (TokenizerChain) tmp.getAnalyzer(); assertTrue(ts.getTokenFilterFactories()[0] instanceof DatatypeAnalyzerFilterFactory); final DatatypeAnalyzerFilterFactory f = (DatatypeAnalyzerFilterFactory) ts.getTokenFilterFactories()[0]; assertNotNull(f.getDatatypeAnalyzers()); assertEquals(9, f.getDatatypeAnalyzers().size()); assertNotNull(f.getDatatypeAnalyzers().get("http://json.org/field")); ts = (TokenizerChain) f.getDatatypeAnalyzers().get("http://json.org/field"); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof WhitespaceTokenizerFactory); assertNotNull(f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#string")); ts = (TokenizerChain) f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#string"); assertNotNull(ts.getTokenizerFactory()); assertTrue(ts.getTokenizerFactory() instanceof UAX29URLEmailTokenizerFactory); assertNotNull(f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#int")); assertTrue(f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#int") instanceof IntNumericAnalyzer); final IntNumericAnalyzer a = (IntNumericAnalyzer) f.getDatatypeAnalyzers().get("http://www.w3.org/2001/XMLSchema#int"); assertEquals(8, a.getPrecisionStep()); assertEquals(32, a.getNumericParser().getValueSize()); assertEquals(NumericType.INT, a.getNumericParser().getNumericType()); }
/** * Initialise the numeric analyzers. The query analyzer is just used as a * container of the numeric configuration, i.e., type and precision step. */ private void initAnalyzers() { switch (type) { case INTEGER: queryAnalyzer = analyzer = new IntNumericAnalyzer(precisionStep); break; case LONG: queryAnalyzer = analyzer = new LongNumericAnalyzer(precisionStep); break; case FLOAT: queryAnalyzer = analyzer = new FloatNumericAnalyzer(precisionStep); break; case DOUBLE: queryAnalyzer = analyzer = new DoubleNumericAnalyzer(precisionStep); break; case DATE: queryAnalyzer = analyzer = new DateNumericAnalyzer(precisionStep); break; default: throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown " + "type for trie datatype"); } }
/** * Test for wildcard bounds. * <p> * Numeric ranges get processed with {@link NodeNumericRangeQueryNodeProcessor}. * Single numeric values are processed with {@link NodeNumericQueryNodeProcessor}. */ @Test public void testNumericQuery2() throws Exception { final HashMap<ConfigurationKey, Object> config = new HashMap<ConfigurationKey, Object>(); final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("int4", new IntNumericAnalyzer(4)); datatypes.put("float4", new FloatNumericAnalyzer(4)); datatypes.put("long4", new LongNumericAnalyzer(4)); datatypes.put("double4", new DoubleNumericAnalyzer(4)); config.put(KeywordConfigurationKeys.DATATYPES_ANALYZERS, datatypes); // Integer assertOpenRange(config, "int4"); // Float assertOpenRange(config, "float4"); // Long assertOpenRange(config, "long4"); // Double assertOpenRange(config, "double4"); }
private static void init(final Index index) throws IOException { final RandomSirenCodec codec = new RandomSirenCodec(random(), PostingsFormatType.RANDOM); final TupleAnalyzer tupleAnalyzer = (TupleAnalyzer) SirenTestCase.newTupleAnalyzer(); final AnyURIAnalyzer uriAnalyzer = new AnyURIAnalyzer(TEST_VERSION_CURRENT); tupleAnalyzer.registerDatatype(XSDDatatype.XSD_ANY_URI.toCharArray(), uriAnalyzer); // Set the SIREn fields codec.addSirenFields("field8", "field4", "field2", "field" + Integer.MAX_VALUE, "ascfield8", "ascfield4", "ascfield2", "float8", "float4", "float2"); // Set the datatype analyzers tupleAnalyzer.registerDatatype((XSDDatatype.XSD_INT+"8").toCharArray(), new IntNumericAnalyzer(8)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_INT+"4").toCharArray(), new IntNumericAnalyzer(4)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_INT+"2").toCharArray(), new IntNumericAnalyzer(2)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_FLOAT+"8").toCharArray(), new FloatNumericAnalyzer(8)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_FLOAT+"4").toCharArray(), new FloatNumericAnalyzer(4)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_FLOAT+"2").toCharArray(), new FloatNumericAnalyzer(2)); tupleAnalyzer.registerDatatype((XSDDatatype.XSD_INT+Integer.MAX_VALUE).toCharArray(), new IntNumericAnalyzer(Integer.MAX_VALUE)); index.directory = newDirectory(); index.writer = newRandomIndexWriter(index.directory, tupleAnalyzer, codec); }
@Test(expected=AssertionError.class) public void testNumericDatatypeWrongPrecision() throws Exception { final KeywordQueryParser parser = new KeywordQueryParser(); // Set the default datatypes final Map<String, Analyzer> datatypes = new HashMap<String, Analyzer>(); datatypes.put("int", new IntNumericAnalyzer(4)); parser.setDatatypeAnalyzers(datatypes); final NodeQuery rangeWrong = NodeNumericRangeQuery.newIntRange(SirenTestCase.DEFAULT_TEST_FIELD, 42, 12, 21, true, true); assertEquals(new LuceneProxyNodeQuery(rangeWrong), parser.parse("int([12 TO 21])", SirenTestCase.DEFAULT_TEST_FIELD)); }
/** * 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 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\")"); }
@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)"); }
datatypes.put("xsd:int", new IntNumericAnalyzer(4));
datatypes.put("xsd:int", new IntNumericAnalyzer(4));