LuceneSearch extends SearchService { @Override public List<Map<String, String>> query(String queryStr) { if (null == indexDirectory) { throw new IllegalStateException( "Something went wrong on instance creation time, index dir is null"); } List<Map<String, String>> result = Collections.emptyList(); try (IndexReader indexReader = DirectoryReader.open(indexDirectory)) { IndexSearcher indexSearcher = new IndexSearcher(indexReader); Analyzer analyzer = new StandardAnalyzer(); MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {SEARCH_FIELD_TEXT, SEARCH_FIELD_TITLE}, analyzer); Query query = parser.parse(queryStr); logger.debug("Searching for: " + query.toString(SEARCH_FIELD_TEXT)); SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(); Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query)); result = doSearch(indexSearcher, query, analyzer, highlighter); } catch (IOException e) { logger.error("Failed to open index dir {}, make sure indexing finished OK", indexDirectory, e); } catch (ParseException e) { logger.error("Failed to parse query " + queryStr, e); } return result; } @Inject  LuceneSearch(ZeppelinConfiguration conf); @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(String noteId); @Override void deleteIndexDoc(String noteId, Paragraph p); @Override void close(); @Override void startRebuildIndex(Stream<Note> notes);  }
@Test public void canIndexAndQueryByNotebookName() throws IOException, InterruptedException { Note note1 = newNoteWithParagraph("Notebook1", "test"); Note note2 = newNoteWithParagraphs("Notebook2", "not test", "not test at all"); noteSearchService.drainEvents(); List<Map<String, String>> results = noteSearchService.query("Notebook1"); assertThat(results).isNotEmpty(); assertThat(results.size()).isEqualTo(1); assertThat(results.get(0)).containsEntry("id", note1.getId()); }
@Test public void canIndexAndQueryByParagraphTitle() throws IOException, InterruptedException { Note note1 = newNoteWithParagraph("Notebook1", "test", "testingTitleSearch"); Note note2 = newNoteWithParagraph("Notebook2", "not test", "notTestingTitleSearch"); noteSearchService.drainEvents(); List<Map<String, String>> results = noteSearchService.query("testingTitleSearch"); assertThat(results).isNotEmpty(); assertThat(results.size()).isAtLeast(1); int TitleHits = 0; for (Map<String, String> res : results) { if (res.get("header").contains("testingTitleSearch")) { TitleHits++; } } assertThat(TitleHits).isAtLeast(1); }
@Test public void canNotSearchBeforeIndexing() { List<Map<String, String>> result = noteSearchService.query("anything"); assertThat(result).isEmpty(); }