LuceneSearch implements SearchService { @Override public List<Map<String, String>> query(String queryStr) { if (null == ramDirectory) { 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(ramDirectory)) { 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); LOG.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); indexReader.close(); } catch (IOException e) { LOG.error("Failed to open index dir {}, make sure indexing finished OK", ramDirectory, e); } catch (ParseException e) { LOG.error("Failed to parse query " + queryStr, e); } return result; } 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 canNotSearchBeforeIndexing() { List<Map<String, String>> result = notebookIndex.query("anything"); assertThat(result).isEmpty(); }