Searcher { private List<Result> search(Query query, Optional<Sort> sort) { IndexSearcher searcher = null; try { searcher = new IndexSearcher(DirectoryReader.open(directory)); List<Result> result = new ArrayList<>(); TopDocs topDocs = searcher.search(query, 10, sort.or(Sort.RELEVANCE)); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); result.add(asResult(doc, query)); } return result; } catch (IOException ex) { throw new IllegalStateException(ex); } finally { Utils.close(searcher); } } Searcher(Directory directory); List<Result> search(String query); List<Result> searchSortedByDate(String query); static void main(String[] args);  }
@Test public void searchAuthor() throws ParseException { Talk talk1 = newAuthorTalk("Florian Hopf"); Talk talk2 = newAuthorTalk("Florian"); indexer.index(talk1, talk2); List<Result> documents = searcher.search("speaker:Florian"); assertEquals(2, documents.size()); documents = searcher.search("speaker:\"Florian Hopf\""); assertEquals(1, documents.size()); }
@Test public void searchWithHighlighter() throws ParseException { StringBuilder content = new StringBuilder("Hier taucht das Wort auf "); for (int i = 0; i < 10; i++) { content.append(" und dazwischen ist ganz viel Text der das nicht enthält, "); } content.append("aber jetzt kommt das Wort nochmal."); Talk talk = new Talk("", "", Collections.<String>emptyList(), new Date(), content.toString(), Collections.<String>emptyList(), ""); indexer.index(talk); List<Result> result = searcher.search("Wort"); assertEquals(1, result.size()); assertTrue(result.get(0).getExcerpt(), result.get(0).getExcerpt().contains("<B>Wort</B>")); assertTrue(result.get(0).getExcerpt(), result.get(0).getExcerpt().contains("...")); }
@Test public void phraseQueryOnStemmedTitle() throws ParseException { Talk talk = new Talk("", "Verteiltes Suchen mit Elasticsearch", Collections.<String>emptyList(), new Date(), "", Collections.<String>emptyList(), ""); indexer.index(talk); List<Result> result = searcher.search("title:\"verteilte suche\""); assertEquals(1, result.size()); }