TextIndexLuceneAssembler extends AssemblerBase { @SuppressWarnings("resource") @Override public TextIndex open(Assembler a, Resource root, Mode mode) { try { if ( !GraphUtils.exactlyOneProperty(root, pDirectory) ) throw new TextIndexException("No 'text:directory' property on " + root) ; Directory directory ; RDFNode n = root.getProperty(pDirectory).getObject() ; if ( n.isLiteral() ) { String literalValue = n.asLiteral().getLexicalForm() ; if (literalValue.equals("mem")) { directory = new ByteBuffersDirectory() ; } else { File dir = new File(literalValue) ; directory = FSDirectory.open(dir.toPath()) ; } } else { Resource x = n.asResource() ; String path = IRILib.IRIToFilename(x.getURI()) ; File dir = new File(path) ; directory = FSDirectory.open(dir.toPath()) ; } String queryParser = null; Statement queryParserStatement = root.getProperty(pQueryParser); if (null != queryParserStatement) { RDFNode qpNode = queryParserStatement.getObject(); if (! qpNode.isResource()) { throw new TextIndexException("Text query parser property is not a resource : " + qpNode); } Resource parserResource = (Resource) qpNode; queryParser = parserResource.getLocalName(); } boolean isMultilingualSupport = false; Statement mlSupportStatement = root.getProperty(pMultilingualSupport); if (null != mlSupportStatement) { RDFNode mlsNode = mlSupportStatement.getObject(); if (! mlsNode.isLiteral()) { throw new TextIndexException("text:multilingualSupport property must be a boolean : " + mlsNode); } isMultilingualSupport = mlsNode.asLiteral().getBoolean(); } int maxBasicQueries = 1024; Statement maxBasicQueriesStatement = root.getProperty(pMaxBasicQueries); if (null != maxBasicQueriesStatement) { RDFNode mbqNode = maxBasicQueriesStatement.getObject(); if (! mbqNode.isLiteral()) { throw new TextIndexException("text:maxBasicQueries property must be a int : " + mbqNode); } try { maxBasicQueries = mbqNode.asLiteral().getInt(); } catch (RuntimeException ex) { throw new TextIndexException("text:maxBasicQueries property must be a int : " + mbqNode+ "("+ex.getMessage()+")"); } } Statement propListsStmt = root.getProperty(pPropLists); if (null != propListsStmt) { RDFNode aNode = propListsStmt.getObject(); if (! aNode.isResource()) { throw new TextIndexException("text:propLists property is not a resource (list) : " + aNode); } PropListsAssembler.open(a, (Resource) aNode); } Statement defAnalyzersStatement = root.getProperty(pDefAnalyzers); if (null != defAnalyzersStatement) { RDFNode aNode = defAnalyzersStatement.getObject(); if (! aNode.isResource()) { throw new TextIndexException("text:defineAnalyzers property is not a resource (list) : " + aNode); } DefineFiltersAssembler.open(a, (Resource) aNode); DefineTokenizersAssembler.open(a, (Resource) aNode); boolean addedLangs = DefineAnalyzersAssembler.open(a, (Resource) aNode); if (addedLangs) { if (!isMultilingualSupport) { Log.warn(this, "Multilingual support implicitly enabled by text:defineAnalyzers"); } isMultilingualSupport = true; } } Analyzer analyzer = null; Statement analyzerStatement = root.getProperty(pAnalyzer); if (null != analyzerStatement) { RDFNode aNode = analyzerStatement.getObject(); if (! aNode.isResource()) { throw new TextIndexException("Text analyzer property is not a resource : " + aNode); } Resource analyzerResource = (Resource) aNode; analyzer = (Analyzer) a.open(analyzerResource); } Analyzer queryAnalyzer = null; Statement queryAnalyzerStatement = root.getProperty(pQueryAnalyzer); if (null != queryAnalyzerStatement) { RDFNode qaNode = queryAnalyzerStatement.getObject(); if (! qaNode.isResource()) { throw new TextIndexException("Text query analyzer property is not a resource : " + qaNode); } Resource analyzerResource = (Resource) qaNode; queryAnalyzer = (Analyzer) a.open(analyzerResource); } boolean storeValues = false; Statement storeValuesStatement = root.getProperty(pStoreValues); if (null != storeValuesStatement) { RDFNode svNode = storeValuesStatement.getObject(); if (! svNode.isLiteral()) { throw new TextIndexException("text:storeValues property must be a boolean : " + svNode); } storeValues = svNode.asLiteral().getBoolean(); } boolean ignoreIndexErrs = false; Statement ignoreIndexErrsStatement = root.getProperty(pIgnoreIndexErrors); if (null != ignoreIndexErrsStatement) { RDFNode iieNode = ignoreIndexErrsStatement.getObject(); if (! iieNode.isLiteral()) { throw new TextIndexException("text:ignoreIndexErrors property must be a boolean : " + iieNode); } ignoreIndexErrs = iieNode.asLiteral().getBoolean(); } boolean cacheQueries = true; Statement cacheQueriesStatement = root.getProperty(pCacheQueries); if (null != cacheQueriesStatement) { RDFNode cqNode = cacheQueriesStatement.getObject(); if (! cqNode.isLiteral()) { throw new TextIndexException("text:cacheQueries property must be a boolean : " + cqNode); } cacheQueries = cqNode.asLiteral().getBoolean(); } Resource r = GraphUtils.getResourceValue(root, pEntityMap) ; EntityDefinition docDef = (EntityDefinition)a.open(r) ; TextIndexConfig config = new TextIndexConfig(docDef); config.setAnalyzer(analyzer); config.setQueryAnalyzer(queryAnalyzer); config.setQueryParser(queryParser); config.setMultilingualSupport(isMultilingualSupport); config.setMaxBasicQueries(maxBasicQueries); config.setValueStored(storeValues); config.setIgnoreIndexErrors(ignoreIndexErrs); docDef.setCacheQueries(cacheQueries); return TextDatasetFactory.createLuceneIndex(directory, config) ; } catch (IOException e) { IO.exception(e) ; return null ; } }  @SuppressWarnings("resource") @Override TextIndex open(Assembler a, Resource root, Mode mode);  }
@Test public void testIndexHasEntityMap() { TextIndexLucene index = (TextIndexLucene) Assembler.general.open(SIMPLE_INDEX_SPEC); try { assertEquals(RDFS.label.asNode(), index.getDocDef().getPrimaryPredicate()); } finally { index.close(); } }
@Test public void testLiteralDirectory() { TextIndexLuceneAssembler assembler = new TextIndexLuceneAssembler(); Resource root = SIMPLE_INDEX_SPEC_LITERAL_DIR; Assembler a = Assembler.general; TextIndexLucene index = (TextIndexLucene)assembler.open(a, root, null); try { assertNotNull(index); } finally { index.close(); } }
@Test public void testResourceDirectory() { TextIndexLuceneAssembler assembler = new TextIndexLuceneAssembler(); Resource root = SIMPLE_INDEX_SPEC2; Assembler a = Assembler.general; TextIndexLucene index = (TextIndexLucene) assembler.open(a, root, null); try { assertFalse(index.getDirectory() instanceof ByteBuffersDirectory); assertNotNull(index.getQueryAnalyzer()); } finally { index.close(); } }
@Test public void testMemDirectory() { TextIndexLuceneAssembler assembler = new TextIndexLuceneAssembler(); Resource root = SIMPLE_INDEX_SPEC_MEM_DIR; Assembler a = Assembler.general; TextIndexLucene index = (TextIndexLucene) assembler.open(a, root, null); try { assertTrue(index.getDirectory() instanceof ByteBuffersDirectory); } finally { index.close(); } }
@Test public void testQueryAnalyzer() { TextIndexLucene index = (TextIndexLucene) Assembler.general.open(SIMPLE_INDEX_SPEC_QUERY_ANALYZER); try { assertTrue(index.getQueryAnalyzer() instanceof KeywordAnalyzer); } finally { index.close(); } }