Skip to content

Commit

Permalink
adds getDocument plus unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
searsia committed Apr 8, 2016
1 parent a83e37e commit 82197c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
25 changes: 22 additions & 3 deletions src/main/java/org/searsia/index/HitsSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
20 changes: 14 additions & 6 deletions src/test/java/org/searsia/index/TestHitsSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -31,15 +31,16 @@ public class TestHitsSearcher {

@BeforeClass
public static void setUp() throws Exception {
Logger.getLogger("").setLevel(Level.WARNING);
LOGGER.removeAllAppenders();
LOGGER.addAppender(new NullAppender());
queue = new ArrayBlockingQueue<SearchResult>(2);
writer = new HitsWriter(PATH, INDEX, queue);
SearchResult result = readFile("exampleSearchResult.json");
queue.offer(result);
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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 82197c0

Please sign in to comment.