Null check on Lucene documents, fix documents search query

This commit is contained in:
jendib 2013-08-17 15:18:42 +02:00
parent 7ed976b27a
commit 8378b346e9
3 changed files with 22 additions and 7 deletions

View File

@ -145,10 +145,9 @@ public class DocumentDao {
Map<String, Object> parameterMap = new HashMap<String, Object>();
List<String> criteriaList = new ArrayList<String>();
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) {

View File

@ -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<String> documentIdList = new HashSet<String>();
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<String> documentIdList = new HashSet<String>();
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));
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));
if (file.getContent() != null) {
luceneDocument.add(new TextField("content", file.getContent(), Field.Store.NO));
}
return luceneDocument;
}

View File

@ -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());
}