LuceneSearch implements SearchService { @Override public void addIndexDocs(Collection<Note> collection) { int docsIndexed = 0; long start = System.nanoTime(); try { for (Note note : collection) { addIndexDocAsync(note); docsIndexed++; } } catch (IOException e) { LOG.error("Failed to index all Notebooks", e); } finally { try { writer.commit(); } catch (IOException e) { LOG.error("Failed to save index", e); } long end = System.nanoTime(); LOG.info("Indexing {} notebooks took {}ms", docsIndexed, TimeUnit.NANOSECONDS.toMillis(end - start)); } } LuceneSearch(); @Override List<Map<String, String>> query(String queryStr); @Override void updateIndexDoc(Note note); @Override void addIndexDocs(Collection<Note> collection); @Override void addIndexDoc(Note note); @Override void deleteIndexDocs(Note note); @Override void deleteIndexDoc(Note note, Paragraph p); @Override void close();  }
@Test public void canIndexNotebook() { Note note1 = newNoteWithParagraph("Notebook1", "test"); Note note2 = newNoteWithParagraph("Notebook2", "not test"); List<Note> notebook = Arrays.asList(note1, note2); notebookIndex.addIndexDocs(notebook); }
@Test public void indexParagraphUpdatedOnNoteSave() throws IOException { Note note1 = newNoteWithParagraph("Notebook1", "test"); Note note2 = newNoteWithParagraphs("Notebook2", "not test", "not test at all"); notebookIndex.addIndexDocs(Arrays.asList(note1, note2)); assertThat(resultForQuery("test").size()).isEqualTo(3); Paragraph p1 = note1.getLastParagraph(); p1.setText("no no no"); note1.persist(null); assertThat(resultForQuery("Notebook1").size()).isEqualTo(1); List<Map<String, String>> results = resultForQuery("test"); assertThat(results).isNotEmpty(); assertThat(results.size()).isEqualTo(2); for (Map<String, String> result: results) { assertThat(result.get("id").startsWith(note1.getId())).isFalse();; } }
@Test public void indexNoteNameUpdatedOnNoteSave() throws IOException { Note note1 = newNoteWithParagraph("Notebook1", "test"); Note note2 = newNoteWithParagraphs("Notebook2", "not test", "not test at all"); notebookIndex.addIndexDocs(Arrays.asList(note1, note2)); assertThat(resultForQuery("test").size()).isEqualTo(3); note1.setName("NotebookN"); note1.persist(null); assertThat(resultForQuery("Notebook1")).isEmpty(); assertThat(resultForQuery("NotebookN")).isNotEmpty(); assertThat(resultForQuery("NotebookN").size()).isEqualTo(1); }