diff --git a/src/main/java/org/searsia/index/HitsSearcher.java b/src/main/java/org/searsia/index/HitsSearcher.java index 09fd65a..e07125c 100644 --- a/src/main/java/org/searsia/index/HitsSearcher.java +++ b/src/main/java/org/searsia/index/HitsSearcher.java @@ -23,12 +23,14 @@ import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.store.FSDirectory; @@ -95,7 +97,7 @@ public SearchResult search (String queryString, int hitsPerPage) throws IOExcept throw new IOException(e); } collector = TopScoreDocCollector.create(hitsPerPage, true); - if (searcher == null) open(); // reopen index every 3 searches, to see updates. + if (searcher == null) open(); // reopen index to see updates. searcher.search(query, collector); docs = collector.topDocs().scoreDocs; for(ScoreDoc doc: docs) { @@ -105,10 +107,27 @@ public SearchResult search (String queryString, int hitsPerPage) throws IOExcept hit.put("score", doc.score); result.addHit(hit); } - if (requests++ > 3) close(); // close index every 3 searches, to see updates + if (requests++ > 10) close(); // close index every 10 searches, to see updates return result; } + + public Hit getDocument(String hitId) throws IOException { + Term term = new Term("id", hitId); + Query query = new TermQuery(term); + TopScoreDocCollector collector = TopScoreDocCollector.create(1, true); + if (searcher == null) open(); + searcher.search(query, collector); + if (collector.getTotalHits() > 0) { + ScoreDoc[] docs = collector.topDocs().scoreDocs; + Document doc = searcher.doc(docs[0].doc); + Hit hit = new Hit(doc.get("result")); + return hit; + } else { + return null; + } + } + /** * Dump the index to standard out * @throws IOException @@ -117,7 +136,7 @@ public void dump() throws IOException { TopScoreDocCollector collector; ScoreDoc[] docs; collector = TopScoreDocCollector.create(999999, true); - if (searcher == null) open(); // reopen index every 3 searches, to see updates. + if (searcher == null) open(); searcher.search(new MatchAllDocsQuery(), collector); docs = collector.topDocs().scoreDocs; for(ScoreDoc doc: docs) { diff --git a/src/test/java/org/searsia/index/TestHitsSearcher.java b/src/test/java/org/searsia/index/TestHitsSearcher.java index a1cd6ed..bc1360b 100644 --- a/src/test/java/org/searsia/index/TestHitsSearcher.java +++ b/src/test/java/org/searsia/index/TestHitsSearcher.java @@ -4,15 +4,14 @@ import java.io.FileReader; import java.io.IOException; import java.util.concurrent.ArrayBlockingQueue; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.apache.log4j.Logger; +import org.apache.log4j.varia.NullAppender; import org.json.JSONArray; import org.json.JSONObject; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; - import org.searsia.Hit; import org.searsia.SearchResult; import org.searsia.index.HitsSearcher; @@ -23,6 +22,7 @@ */ public class TestHitsSearcher { + private static final Logger LOGGER = Logger.getLogger("org.searsia"); private static final String PATH = "target/index-test"; private static final String INDEX = "test"; private static HitsWriter writer; @@ -31,7 +31,8 @@ public class TestHitsSearcher { @BeforeClass public static void setUp() throws Exception { - Logger.getLogger("").setLevel(Level.WARNING); + LOGGER.removeAllAppenders(); + LOGGER.addAppender(new NullAppender()); queue = new ArrayBlockingQueue(2); writer = new HitsWriter(PATH, INDEX, queue); SearchResult result = readFile("exampleSearchResult.json"); @@ -39,7 +40,7 @@ public static void setUp() throws Exception { writer.flush(); searcher = new HitsSearcher(PATH, INDEX); } - + private static SearchResult readFile(String fileString) throws IOException { SearchResult result = new SearchResult(); String s, jsonString = ""; // TODO: Does the following file name work in Windows? @@ -80,8 +81,15 @@ public void testSearch3() throws Exception { SearchResult result = searcher.search("retrieval"); Assert.assertEquals(6, result.getHits().size()); } - + @Test + public void testSearch4() throws Exception { + SearchResult result = readFile("exampleSearchResult.json"); + Hit hit1 = result.getHits().get(0); + Hit hit2 = searcher.getDocument(hit1.getId()); + Assert.assertEquals(hit1.getTitle(), hit2.getTitle()); + } + /** * Can also be used from the command line to test an existing index * @param args query