public void dispose() { indexEngine.dispose(); }
@Test public void testSearch() { final FieldFactory factory = new SimpleFieldFactory(); final MetaModelStore store = new InMemoryMetaModelStore(); final LuceneSetup setup = new RAMLuceneSetup(); final MetaIndexEngine engine = new LuceneIndexEngine(store, setup, factory); for (int i = 0; i < 50; i++) { final int index = i; engine.index( new KObject() { @Override public String getId() { return "unique.id.here." + index; } @Override public MetaType getType() { return new MetaType() { @Override public String getName() { return "Path"; } }; } @Override public String getClusterId() { return "cluster.id.here." + index % 2; } @Override public String getSegmentId() { return "/"; } @Override public String getKey() { return "some.key.here." + index; } @Override public Iterable<KProperty<?>> getProperties() { return new HashSet<KProperty<?>>() { { add( new KProperty<String>() { @Override public String getName() { return "dcore.author"; } @Override public String getValue() { return "Some Author name Here" + index; } @Override public boolean isSearchable() { return true; } }); add( new KProperty<String>() { @Override public String getName() { return "filename"; } @Override public String getValue() { if (index % 2 == 0) { return "File" + index + ".dtxt"; } return "file" + index + ".txt"; } @Override public boolean isSearchable() { return true; } }); add( new KProperty<String>() { @Override public String getName() { return "dcore.comment"; } @Override public String getValue() { return "My comment here that has some content that is important to my users." + index; } @Override public boolean isSearchable() { return true; } }); add( new KProperty<Integer>() { @Override public String getName() { return "dcore.review"; } @Override public Integer getValue() { return 10 + index; } @Override public boolean isSearchable() { return true; } }); add( new KProperty<Date>() { @Override public String getName() { return "dcore.lastModifiedTime"; } @Override public Date getValue() { return new Date(); } @Override public boolean isSearchable() { return true; } }); } }; } }); } final LuceneSearchIndex searchEngine = new LuceneSearchIndex(setup); assertEquals(1, searchEngine.fullTextSearchHits("Here49")); assertEquals(50, searchEngine.fullTextSearchHits("comment")); assertEquals(0, searchEngine.fullTextSearchHits("file.txt")); assertEquals(1, searchEngine.fullTextSearchHits("file10")); assertEquals(26, searchEngine.fullTextSearchHits("file10.txt")); assertEquals( 25, searchEngine.searchByAttrsHits( new HashMap<String, Object>() { { put("filename", "*.txt"); } })); assertEquals( 25, searchEngine.searchByAttrsHits( new HashMap<String, Object>() { { put("filename", "*.dtxt"); } })); assertEquals( 1, searchEngine.searchByAttrsHits( new HashMap<String, Object>() { { put("filename", "file1.*txt"); } })); assertEquals( 1, searchEngine.searchByAttrsHits( new HashMap<String, Object>() { { put("filename", "File0.dtxt"); } })); assertEquals( 0, searchEngine.fullTextSearchHits( "here49", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.0"; } @Override public String[] segmentIds() { return new String[] {"/"}; } })); assertEquals( 1, searchEngine.fullTextSearchHits( "here49", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.1"; } @Override public String[] segmentIds() { return new String[] {"/"}; } })); assertEquals( 25, searchEngine.fullTextSearchHits( "comment", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.1"; } @Override public String[] segmentIds() { return new String[] {"/"}; } })); assertEquals( 25, searchEngine.fullTextSearchHits( "comment", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.1"; } @Override public String[] segmentIds() { return new String[0]; } })); assertEquals( 25, searchEngine.fullTextSearchHits( "comment", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.0"; } @Override public String[] segmentIds() { return new String[] {"/"}; } })); assertEquals( 25, searchEngine.fullTextSearchHits( "comment", new ClusterSegment() { @Override public String getClusterId() { return "cluster.id.here.0"; } @Override public String[] segmentIds() { return new String[0]; } })); }
public void run(final Path root, final Runnable callback) { try { if (root == null) { return; } final KCluster cluster = toKCluster(root.getFileSystem()); indexEngine.startBatch(cluster); walkFileTree( checkNotNull("root", root), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (indexDisposed.get()) { return FileVisitResult.TERMINATE; } try { checkNotNull("file", file); checkNotNull("attrs", attrs); if (!file.getFileName().toString().startsWith(".")) { // Default indexing for (final Class<? extends FileAttributeView> view : views) { ioService.getFileAttributeView(file, view); } final FileAttribute<?>[] allAttrs = ioService.convert(ioService.readAttributes(file)); if (!indexDisposed.get()) { indexEngine.index(KObjectUtil.toKObject(file, allAttrs)); } else { return FileVisitResult.TERMINATE; } // Additional indexing for (Indexer indexer : additionalIndexers) { if (indexer.supportsPath(file)) { final KObject kObject = indexer.toKObject(file); if (kObject != null) { if (!indexDisposed.get()) { indexEngine.index(kObject); } else { return FileVisitResult.TERMINATE; } } } } } } catch (final Exception ex) { if (indexDisposed.get()) { LOG.warn("Batch index couldn't finish. [@" + root.toUri().toString() + "]"); return FileVisitResult.TERMINATE; } else { LOG.error("Index fails. [@" + file.toString() + "]", ex); } } if (indexDisposed.get()) { return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } }); if (!indexDisposed.get()) { indexEngine.commit(cluster); if (callback != null) { callback.run(); } } else { LOG.warn("Batch index couldn't finish. [@" + root.toUri().toString() + "]"); } } catch (final IllegalStateException ex) { if (indexDisposed.get()) { LOG.warn("Batch index couldn't finish. [@" + root.toUri().toString() + "]"); } else { LOG.error( "Index fails - Index has an invalid state. [@" + root.toUri().toString() + "]", ex); } } catch (final Exception ex) { if (indexDisposed.get()) { LOG.warn("Batch index couldn't finish. [@" + root.toUri().toString() + "]"); } else { LOG.error("Index fails. [@" + root.toUri().toString() + "]", ex); } } }