This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit b9b1dc936cb43ed7a99f0a495ae8a135793a01af Author: Yannick Martel <martel@©odelutin.com> Date: Tue Dec 30 10:15:47 2014 +0100 add way to update and delete index information for document. Link delete with service --- .../indexation/DocumentsIndexationService.java | 51 ++++- .../coselmar/services/v1/DocumentsWebService.java | 14 ++ .../services/DocumentsIndexationServiceTest.java | 103 ---------- .../indexation/DocumentsIndexationServiceTest.java | 208 +++++++++++++++++++++ 4 files changed, 269 insertions(+), 107 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java index 7c166c7..08a7c51 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationService.java @@ -77,10 +77,6 @@ public class DocumentsIndexationService extends CoselmarSimpleServiceSupport { } - public void updateDocument(DocumentBean document) { - //TODO - } - public List<String> searchDocuments(String text) throws IOException, ParseException { DirectoryReader ireader = DirectoryReader.open(getIndexWriter(), false); IndexSearcher isearcher = new IndexSearcher(ireader); @@ -126,6 +122,53 @@ public class DocumentsIndexationService extends CoselmarSimpleServiceSupport { return documentIds; } + public void updateDocument(DocumentBean document) throws IOException { + DirectoryReader ireader = DirectoryReader.open(getIndexWriter(), false); + IndexSearcher isearcher = new IndexSearcher(ireader); + + // Retrieve document + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(DOCUMENT_ID_INDEX_PROPERTY, document.getId())), BooleanClause.Occur.MUST); + query.add(new TermQuery(new Term("type", DOCUMENT_TYPE)), BooleanClause.Occur.MUST); + + ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; + if (hits.length > 0) { + Document doc = isearcher.doc(hits[0].doc); + + doc.removeField(DOCUMENT_NAME_INDEX_PROPERTY); + doc.add(new TextField(DOCUMENT_NAME_INDEX_PROPERTY, document.getName(), Field.Store.YES)); + doc.removeField(DOCUMENT_AUTHORS_INDEX_PROPERTY); + doc.add(new TextField(DOCUMENT_AUTHORS_INDEX_PROPERTY, document.getAuthors(), Field.Store.YES)); + doc.removeField(DOCUMENT_SUMMARY_INDEX_PROPERTY); + doc.add(new TextField(DOCUMENT_SUMMARY_INDEX_PROPERTY, document.getSummary(), Field.Store.YES)); + + doc.removeFields(DOCUMENT_KEYWORD_INDEX_PROPERTY); + Set<String> keywords = document.getKeywords(); + for (String keyword : keywords) { + doc.add(new Field(DOCUMENT_KEYWORD_INDEX_PROPERTY, keyword, TextField.TYPE_STORED)); + } + + getIndexWriter().updateDocument(new Term(DOCUMENT_ID_INDEX_PROPERTY, document.getId()), doc); + getIndexWriter().commit(); + } + } + + public void deleteDocument(String documentId) throws IOException { + + // Retrieve document + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(DOCUMENT_ID_INDEX_PROPERTY, documentId)), BooleanClause.Occur.MUST); + query.add(new TermQuery(new Term("type", DOCUMENT_TYPE)), BooleanClause.Occur.MUST); + + getIndexWriter().deleteDocuments(query); + getIndexWriter().commit(); + + } + + protected void cleanIndex() throws IOException { + getIndexWriter().deleteAll(); + getIndexWriter().commit(); + } protected static Analyzer getAnalyzer() { if (analyzer == null) { diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java index 0bad993..e16667e 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java @@ -323,6 +323,20 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { getDocumentDao().delete(document); commit(); + + //Remove index entry + DocumentsIndexationService documentsIndexationService = getServicesContext().newService(DocumentsIndexationService.class); + try { + documentsIndexationService.deleteDocument(documentId); + if (log.isDebugEnabled()) { + String message = String.format("Document '%s' removed from index", documentId); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to remove document entry from index", e); + } + } } public List<String> getKeywords() throws InvalidCredentialException, UnauthorizedException { diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/DocumentsIndexationServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/DocumentsIndexationServiceTest.java deleted file mode 100644 index c201e4c..0000000 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/DocumentsIndexationServiceTest.java +++ /dev/null @@ -1,103 +0,0 @@ -package fr.ifremer.coselmar.services; - -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import com.google.common.collect.Lists; -import fr.ifremer.coselmar.beans.DocumentBean; -import fr.ifremer.coselmar.persistence.entity.Privacy; -import fr.ifremer.coselmar.services.indexation.DocumentsIndexationService; -import org.junit.Assert; -import org.junit.Test; - -/** - * @author ymartel <martel@codelutin.com> - */ -public class DocumentsIndexationServiceTest extends AbstractCoselmarServiceTest { - - protected FakeCoselmarServicesContext serviceContext; - - protected FakeCoselmarServicesContext getServiceContext() { - - if (serviceContext == null) { - serviceContext = application.newServiceContext(application.newPersistenceContext(), Locale.FRANCE); - } - - return serviceContext; - } - - @Test - public void testAddDocument() throws Exception { - CoselmarServicesContext serviceContext = getServiceContext(); - DocumentsIndexationService documentsIndexationService = - serviceContext.newService(DocumentsIndexationService.class); - - DocumentBean documentOne = new DocumentBean("document1", - "Ceci n'est pas un document", "John Doe", Privacy.PUBLIC.name(), - new Date(), Lists.newArrayList("document", "test"), "testDocument", - "This is not a fake document used for test", "fr", null, "Jack, Jane", - null, null, false, null, "http://somewhere", "no comment"); - - documentsIndexationService.indexDocument(documentOne); - - } - - @Test - public void testSearchDocument() throws Exception { - populate(); - - CoselmarServicesContext serviceContext = getServiceContext(); - DocumentsIndexationService documentsIndexationService = - serviceContext.newService(DocumentsIndexationService.class); - - List<String> documentMatchingDoctorIds = documentsIndexationService.searchDocuments("doctor"); - Assert.assertEquals(1, documentMatchingDoctorIds.size()); - Assert.assertEquals("document3", documentMatchingDoctorIds.get(0)); - - List<String> documentMatchingAmyIds = documentsIndexationService.searchDocuments("amy"); - Assert.assertEquals(2, documentMatchingAmyIds.size()); - Assert.assertTrue(documentMatchingAmyIds.contains("document2")); - Assert.assertTrue(documentMatchingAmyIds.contains("document3")); - - List<String> documentMatchingTheMasterIds = documentsIndexationService.searchDocuments("the Master"); - Assert.assertTrue(documentMatchingTheMasterIds.isEmpty()); - - List<String> documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is part of"); - Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); - Assert.assertEquals("document3", documentMatchingPartOfSummaryIds.get(0)); - - } - - public void populate() throws Exception { - CoselmarServicesContext serviceContext = getServiceContext(); - DocumentsIndexationService documentsIndexationService = - serviceContext.newService(DocumentsIndexationService.class); - - DocumentBean documentOne = new DocumentBean("document1", - "Ceci n'est pas un document", "John Doe", Privacy.PUBLIC.name(), - new Date(), Lists.newArrayList("document", "test"), "testDocument", - "This is not a fake document used for test", "fr", null, "Jack, Jane", - null, null, false, null, "http://somewhere", "no comment"); - - documentsIndexationService.indexDocument(documentOne); - - DocumentBean documentTwo = new DocumentBean("document2", - "Another document", "Amy Pond", Privacy.PUBLIC.name(), - new Date(), Lists.newArrayList("document", "test", "fish"), "testDocument", - "This is just an other document used for test", "fr", null, "Amy, Rory", - null, null, false, null, "http://somewhere", "no comment"); - - documentsIndexationService.indexDocument(documentTwo); - - DocumentBean documentThree = new DocumentBean("document3", - "Tardis documentation", "The Doctor", Privacy.PUBLIC.name(), - new Date(), Lists.newArrayList("tardis", "documentation", "old", "new", "borrowed", "blue"), "testDocument", - "This is part of documentation about the TARDIS", "fr", null, "The Doctor, Rose, Amy, River, Clara", - null, null, false, null, "http://tardis.wikia.com/wiki/TARDIS", "no comment"); - - documentsIndexationService.indexDocument(documentThree); - - } - -} diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationServiceTest.java new file mode 100644 index 0000000..7b667a3 --- /dev/null +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/DocumentsIndexationServiceTest.java @@ -0,0 +1,208 @@ +package fr.ifremer.coselmar.services.indexation; + +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import com.google.common.collect.Lists; +import fr.ifremer.coselmar.beans.DocumentBean; +import fr.ifremer.coselmar.persistence.entity.Privacy; +import fr.ifremer.coselmar.services.AbstractCoselmarServiceTest; +import fr.ifremer.coselmar.services.CoselmarServicesContext; +import fr.ifremer.coselmar.services.FakeCoselmarServicesContext; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author ymartel <martel@codelutin.com> + */ +public class DocumentsIndexationServiceTest extends AbstractCoselmarServiceTest { + +// protected FakeCoselmarServicesContext serviceContext; + + protected FakeCoselmarServicesContext getServiceContext() { + +// if (serviceContext == null) { + FakeCoselmarServicesContext serviceContext = application.newServiceContext(application.newPersistenceContext(), Locale.FRANCE); +// } + + return serviceContext; + } + + @Test + public void testAddDocument() throws Exception { + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + + DocumentBean documentOne = new DocumentBean("document1", + "Ceci n'est pas un document", "John Doe", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("document", "test"), "testDocument", + "This is not a fake document used for test", "fr", null, "Jack, Jane", + null, null, false, null, "http://somewhere", "no comment"); + + documentsIndexationService.indexDocument(documentOne); + + } + + @Test + public void testSearchDocument() throws Exception { + populate(); + + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + + List<String> documentMatchingDoctorIds = documentsIndexationService.searchDocuments("doctor"); + Assert.assertEquals(1, documentMatchingDoctorIds.size()); + Assert.assertEquals("document3", documentMatchingDoctorIds.get(0)); + + List<String> documentMatchingAmyIds = documentsIndexationService.searchDocuments("amy"); + Assert.assertEquals(2, documentMatchingAmyIds.size()); + Assert.assertTrue(documentMatchingAmyIds.contains("document2")); + Assert.assertTrue(documentMatchingAmyIds.contains("document3")); + + List<String> documentMatchingTheMasterIds = documentsIndexationService.searchDocuments("the Master"); + Assert.assertTrue(documentMatchingTheMasterIds.isEmpty()); + + List<String> documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is part of"); + Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); + Assert.assertEquals("document3", documentMatchingPartOfSummaryIds.get(0)); + + } + + @Test + public void testUpdateDocument() throws Exception { + + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + + DocumentBean documentOne = new DocumentBean("document1", + "Ceci n'est pas un document", "John Doe", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("document", "test"), "testDocument", + "This is not a fake document used for test", "fr", null, "Jack, Jane", + null, null, false, null, "http://somewhere", "no comment"); + + documentsIndexationService.indexDocument(documentOne); + + List<String> documentMatchingDocumentIds = documentsIndexationService.searchDocuments("document"); + Assert.assertEquals(1, documentMatchingDocumentIds.size()); + Assert.assertEquals("document1", documentMatchingDocumentIds.get(0)); + + List<String> documentMatchingJackIds = documentsIndexationService.searchDocuments("jack"); + Assert.assertEquals(1, documentMatchingJackIds.size()); + Assert.assertTrue(documentMatchingJackIds.contains("document1")); + + List<String> documentMatchingTheMasterIds = documentsIndexationService.searchDocuments("James"); + Assert.assertTrue(documentMatchingTheMasterIds.isEmpty()); + + List<String> documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is not a fake document"); + Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); + Assert.assertEquals("document1", documentMatchingPartOfSummaryIds.get(0)); + + documentOne = new DocumentBean("document1", + "Doc Premier", "John Doe", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("test", "update"), "testDocument", + "This is a fake doc updated for test", "fr", null, "James, JJ", + null, null, false, null, "http://somewhere", "no comment"); + + documentsIndexationService.updateDocument(documentOne); + + documentMatchingDocumentIds = documentsIndexationService.searchDocuments("document"); + Assert.assertTrue(documentMatchingDocumentIds.isEmpty()); + + documentMatchingJackIds = documentsIndexationService.searchDocuments("jack"); + Assert.assertTrue(documentMatchingJackIds.isEmpty()); + + documentMatchingTheMasterIds = documentsIndexationService.searchDocuments("James"); + Assert.assertEquals(1, documentMatchingTheMasterIds.size()); + Assert.assertEquals("document1", documentMatchingTheMasterIds.get(0)); + + documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is not a fake document"); + Assert.assertTrue(documentMatchingPartOfSummaryIds.isEmpty()); + + documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is a fake doc"); + Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); + Assert.assertEquals("document1", documentMatchingPartOfSummaryIds.get(0)); + + List<String> documentMatchingKeywordIds = documentsIndexationService.searchDocuments("update"); + Assert.assertEquals(1, documentMatchingKeywordIds.size()); + Assert.assertEquals("document1", documentMatchingKeywordIds.get(0)); + + List<String> documentMatchingPremierIds = documentsIndexationService.searchDocuments("premier"); + Assert.assertEquals(1, documentMatchingPremierIds.size()); + Assert.assertEquals("document1", documentMatchingPremierIds.get(0)); + + } + + @Test + public void testDeleteDocument() throws Exception { + populate(); + + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + + //Delete document2 + documentsIndexationService.deleteDocument("document2"); + + List<String> documentMatchingDoctorIds = documentsIndexationService.searchDocuments("doctor"); + Assert.assertEquals(1, documentMatchingDoctorIds.size()); + Assert.assertEquals("document3", documentMatchingDoctorIds.get(0)); + + List<String> documentMatchingAmyIds = documentsIndexationService.searchDocuments("amy"); + Assert.assertEquals(1, documentMatchingAmyIds.size()); + Assert.assertTrue(documentMatchingAmyIds.contains("document3")); + + List<String> documentMatchingTheMasterIds = documentsIndexationService.searchDocuments("Another"); + Assert.assertTrue(documentMatchingTheMasterIds.isEmpty()); + + List<String> documentMatchingPartOfSummaryIds = documentsIndexationService.searchDocuments("This is part of"); + Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); + Assert.assertEquals("document3", documentMatchingPartOfSummaryIds.get(0)); + + + } + + public void populate() throws Exception { + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + + DocumentBean documentOne = new DocumentBean("document1", + "Ceci n'est pas un document", "John Doe", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("document", "test"), "testDocument", + "This is not a fake document used for test", "fr", null, "Jack, Jane", + null, null, false, null, "http://somewhere", "no comment"); + + documentsIndexationService.indexDocument(documentOne); + + DocumentBean documentTwo = new DocumentBean("document2", + "Another document", "Amy Pond", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("document", "test", "fish"), "testDocument", + "This is just an other document used for test", "fr", null, "Amy, Rory", + null, null, false, null, "http://somewhere", "no comment"); + + documentsIndexationService.indexDocument(documentTwo); + + DocumentBean documentThree = new DocumentBean("document3", + "Tardis documentation", "The Doctor", Privacy.PUBLIC.name(), + new Date(), Lists.newArrayList("tardis", "documentation", "old", "new", "borrowed", "blue"), "testDocument", + "This is part of documentation about the TARDIS", "fr", null, "The Doctor, Rose, Amy, River, Clara", + null, null, false, null, "http://tardis.wikia.com/wiki/TARDIS", "no comment"); + + documentsIndexationService.indexDocument(documentThree); + + } + + @After + public void cleanUp() throws Exception { + CoselmarServicesContext serviceContext = getServiceContext(); + DocumentsIndexationService documentsIndexationService = + serviceContext.newService(DocumentsIndexationService.class); + documentsIndexationService.cleanIndex(); + + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.