Skip to content

Commit

Permalink
minor refactor index
Browse files Browse the repository at this point in the history
  • Loading branch information
searsia committed Apr 8, 2016
1 parent 82197c0 commit af7e690
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
15 changes: 9 additions & 6 deletions src/main/java/org/searsia/index/HitsSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class HitsSearcher {
private File hitsDir;
private IndexReader reader;
private IndexSearcher searcher;
private int requests;
private int nrRequests;

/**
* Opens the local index "indexName" at "path".
Expand All @@ -62,13 +62,13 @@ public HitsSearcher(String path, String indexName) throws IOException {
open();
}

public void open() throws IOException {
reader = DirectoryReader.open(FSDirectory.open(hitsDir));
searcher = new IndexSearcher(reader);
private void open() throws IOException {
this.reader = DirectoryReader.open(FSDirectory.open(this.hitsDir));
this.searcher = new IndexSearcher(this.reader);
//searcher.setSimilarity(new BM25Similarity(1.2f, 0.75f)); // k1, b
//searcher.setSimilarity(new LMDirichletSimilarity(200f)); // mu
//searcher.setSimilarity(new LMJelinekMercerSimilarity(0.5f)); // lambda
requests = 0;
nrRequests = 0;
}

public void close() throws IOException {
Expand Down Expand Up @@ -107,7 +107,10 @@ public SearchResult search (String queryString, int hitsPerPage) throws IOExcept
hit.put("score", doc.score);
result.addHit(hit);
}
if (requests++ > 10) close(); // close index every 10 searches, to see updates
if (nrRequests++ > 10) {
nrRequests = 0;
close(); // close index every 10 searches, to see updates
}
return result;
}

Expand Down
37 changes: 7 additions & 30 deletions src/main/java/org/searsia/index/HitsWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

Expand All @@ -43,31 +42,22 @@
*
* @author Djoerd Hiemstra and Dolf Trieschnigg
*/
public class HitsWriter implements Runnable {
public class HitsWriter {

public static final Hit SEARSIA_HIT =
new Hit("Searsia", "Search for noobs", "http://searsia.org", "http://searsia.org/images/searsia.png");
private final static Logger LOGGER = Logger.getLogger(HitsWriter.class.getName());

private ArrayBlockingQueue<SearchResult> queue;
private int limit;
private int interval;

private final Version version = Version.LUCENE_4_10_4;
private Directory indexDir;
private StandardAnalyzer indexAnalyzer;
private IndexWriterConfig indexConfig;
private IndexWriter indexWriter;

public HitsWriter(String path, String indexName, ArrayBlockingQueue<SearchResult> queue) throws IOException {
this(path, indexName, queue, 6);
}

public HitsWriter(String path, String indexName, ArrayBlockingQueue<SearchResult> queue, int interval) throws IOException {
this.initIndex(path, indexName);
initIndex(path, indexName);
this.queue = queue;
this.limit = ((queue.remainingCapacity() + queue.size()) / 2) - 1; // half of the capacity
this.interval = interval;
}

private void initIndex(String path, String indexName) throws IOException {
Expand All @@ -79,11 +69,10 @@ private void initIndex(String path, String indexName) throws IOException {
if (!hitsDir.exists()) {
hitsDir.mkdir();
}
this.indexDir = FSDirectory.open(hitsDir);
this.indexAnalyzer = new StandardAnalyzer();
this.indexConfig = new IndexWriterConfig(version, indexAnalyzer);
this.indexConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
this.indexWriter = new IndexWriter(this.indexDir, this.indexConfig);
StandardAnalyzer indexAnalyzer = new StandardAnalyzer();
IndexWriterConfig indexConfig = new IndexWriterConfig(version, indexAnalyzer);
indexConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
this.indexWriter = new IndexWriter(FSDirectory.open(hitsDir), indexConfig);
addSearchResult(new SearchResult(SEARSIA_HIT));
this.indexWriter.commit();
}
Expand Down Expand Up @@ -119,16 +108,4 @@ public boolean check() throws IOException {
return full;
}

@Override // so, it can be spawned as a thread, not used currently...
public void run() {
try {
while(true) {
check();
Thread.sleep(interval * 1000);
}
} catch (InterruptedException e) {
} catch (IOException e) {
}
}

}

0 comments on commit af7e690

Please sign in to comment.