From af7e690466c168a9a5441b0eee5ff7ff996e5373 Mon Sep 17 00:00:00 2001 From: Searsia Date: Fri, 8 Apr 2016 21:33:25 +0200 Subject: [PATCH] minor refactor index --- .../java/org/searsia/index/HitsSearcher.java | 15 +++++--- .../java/org/searsia/index/HitsWriter.java | 37 ++++--------------- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/searsia/index/HitsSearcher.java b/src/main/java/org/searsia/index/HitsSearcher.java index e07125c..1574d86 100644 --- a/src/main/java/org/searsia/index/HitsSearcher.java +++ b/src/main/java/org/searsia/index/HitsSearcher.java @@ -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". @@ -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 { @@ -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; } diff --git a/src/main/java/org/searsia/index/HitsWriter.java b/src/main/java/org/searsia/index/HitsWriter.java index 2fe59da..d833e0b 100644 --- a/src/main/java/org/searsia/index/HitsWriter.java +++ b/src/main/java/org/searsia/index/HitsWriter.java @@ -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; @@ -43,7 +42,7 @@ * * @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"); @@ -51,23 +50,14 @@ public class HitsWriter implements Runnable { private ArrayBlockingQueue 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 queue) throws IOException { - this(path, indexName, queue, 6); - } - - public HitsWriter(String path, String indexName, ArrayBlockingQueue 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 { @@ -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(); } @@ -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) { - } - } - }