From 8378b346e9396ee6d078624036d0ee9c8f5eacc4 Mon Sep 17 00:00:00 2001 From: jendib Date: Sat, 17 Aug 2013 15:18:42 +0200 Subject: [PATCH] Null check on Lucene documents, fix documents search query --- .../docs/core/dao/jpa/DocumentDao.java | 3 +-- .../docs/core/dao/lucene/LuceneDao.java | 24 +++++++++++++++---- .../core/event/RebuildIndexAsyncListener.java | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/DocumentDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/DocumentDao.java index 426bd778..2b27faa8 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/DocumentDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/DocumentDao.java @@ -145,10 +145,9 @@ public class DocumentDao { Map parameterMap = new HashMap(); List criteriaList = new ArrayList(); - StringBuilder sb = new StringBuilder("select d.DOC_ID_C c0, d.DOC_TITLE_C c1, d.DOC_DESCRIPTION_C c2, d.DOC_CREATEDATE_D c3, d.DOC_LANGUAGE_C c4, s.SHA_ID_C is not null c5 "); + StringBuilder sb = new StringBuilder("select distinct d.DOC_ID_C c0, d.DOC_TITLE_C c1, d.DOC_DESCRIPTION_C c2, d.DOC_CREATEDATE_D c3, d.DOC_LANGUAGE_C c4, s.SHA_ID_C is not null c5 "); sb.append(" from T_DOCUMENT d "); sb.append(" left join T_SHARE s on s.SHA_IDDOCUMENT_C = d.DOC_ID_C and s.SHA_DELETEDATE_D is null "); - sb.append(" left join T_FILE f on f.FIL_IDDOC_C = d.DOC_ID_C and f.FIL_DELETEDATE_D is null "); // Adds search criteria if (criteria.getUserId() != null) { diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/lucene/LuceneDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/lucene/LuceneDao.java index 4aa5039e..7add3341 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/lucene/LuceneDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/lucene/LuceneDao.java @@ -23,6 +23,8 @@ import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.util.Version; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.sismics.docs.core.model.context.AppContext; import com.sismics.docs.core.model.jpa.Document; @@ -36,6 +38,10 @@ import com.sismics.docs.core.util.LuceneUtil.LuceneRunnable; * @author bgamard */ public class LuceneDao { + /** + * Logger. + */ + private static final Logger log = LoggerFactory.getLogger(LuceneDao.class); /** * Destroy and rebuild index. @@ -172,13 +178,17 @@ public class LuceneDao { TermsFilter userFilter = new TermsFilter(terms); // Search + Set documentIdList = new HashSet(); + if (!DirectoryReader.indexExists(AppContext.getInstance().getLuceneDirectory())) { + log.warn("Lucene directory not yet initialized"); + return documentIdList; + } IndexReader reader = DirectoryReader.open(AppContext.getInstance().getLuceneDirectory()); IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(query, userFilter, Integer.MAX_VALUE); ScoreDoc[] docs = topDocs.scoreDocs; // Extract document IDs - Set documentIdList = new HashSet(); for (int i = 0; i < docs.length; i++) { org.apache.lucene.document.Document document = searcher.doc(docs[i].doc); String type = document.get("type"); @@ -205,8 +215,12 @@ public class LuceneDao { luceneDocument.add(new StringField("id", document.getId(), Field.Store.YES)); luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES)); luceneDocument.add(new StringField("type", "document", Field.Store.YES)); - luceneDocument.add(new TextField("title", document.getTitle(), Field.Store.NO)); - luceneDocument.add(new TextField("description", document.getDescription(), Field.Store.NO)); + if (document.getTitle() != null) { + luceneDocument.add(new TextField("title", document.getTitle(), Field.Store.NO)); + } + if (document.getDescription() != null) { + luceneDocument.add(new TextField("description", document.getDescription(), Field.Store.NO)); + } return luceneDocument; } @@ -224,7 +238,9 @@ public class LuceneDao { luceneDocument.add(new StringField("user_id", document.getUserId(), Field.Store.YES)); luceneDocument.add(new StringField("type", "file", Field.Store.YES)); luceneDocument.add(new StringField("document_id", file.getDocumentId(), Field.Store.YES)); - luceneDocument.add(new TextField("content", file.getContent(), Field.Store.NO)); + if (file.getContent() != null) { + luceneDocument.add(new TextField("content", file.getContent(), Field.Store.NO)); + } return luceneDocument; } diff --git a/docs-core/src/main/java/com/sismics/docs/core/event/RebuildIndexAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/event/RebuildIndexAsyncListener.java index 4b1f5274..c68d1924 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/event/RebuildIndexAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/event/RebuildIndexAsyncListener.java @@ -31,7 +31,7 @@ public class RebuildIndexAsyncListener { * @throws Exception */ @Subscribe - public void onArticleCreated(final RebuildIndexAsyncEvent rebuildIndexAsyncEvent) throws Exception { + public void on(final RebuildIndexAsyncEvent rebuildIndexAsyncEvent) throws Exception { if (log.isInfoEnabled()) { log.info("Rebuild index event: " + rebuildIndexAsyncEvent.toString()); }