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 37c0bed1b33c6c4d3bc56ff3f88b5f7148548b88 Author: Yannick Martel <martel@©odelutin.com> Date: Wed Dec 31 10:51:30 2014 +0100 use question indexation in webservice --- .../indexation/QuestionsIndexationService.java | 84 +++++++++++----------- .../coselmar/services/v1/QuestionsWebService.java | 46 ++++++++++++ .../indexation/QuestionsIndexationServiceTest.java | 3 +- 3 files changed, 90 insertions(+), 43 deletions(-) diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationService.java index 178c9b4..a1f1773 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationService.java @@ -45,21 +45,54 @@ public class QuestionsIndexationService extends CoselmarSimpleServiceSupport { public void indexQuestion(QuestionBean question) throws IOException { - Document doc = new Document(); - doc.add(new StringField(QUESTION_ID_INDEX_PROPERTY, question.getId(), Field.Store.YES)); + // First : try to find if already exist to update it + DirectoryReader ireader = DirectoryReader.open(getLuceneUtils().getIndexWriter(), false); + IndexSearcher isearcher = new IndexSearcher(ireader); - doc.add(new TextField(QUESTION_TITLE_INDEX_PROPERTY, question.getTitle(), Field.Store.YES)); - doc.add(new TextField(QUESTION_SUMMARY_INDEX_PROPERTY, question.getSummary(), Field.Store.YES)); - doc.add(new Field("type", DOCUMENT_TYPE, TextField.TYPE_STORED)); + // Retrieve document + BooleanQuery query = new BooleanQuery(); + query.add(new TermQuery(new Term(QUESTION_ID_INDEX_PROPERTY, question.getId())), BooleanClause.Occur.MUST); + query.add(new TermQuery(new Term("type", DOCUMENT_TYPE)), BooleanClause.Occur.MUST); - Set<String> themes = question.getThemes(); - for (String theme : themes) { - doc.add(new Field(QUESTION_THEME_INDEX_PROPERTY, theme, TextField.TYPE_STORED)); - } + ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; + if (hits.length > 0) { + Document doc = isearcher.doc(hits[0].doc); + + doc.removeField(QUESTION_TITLE_INDEX_PROPERTY); + doc.add(new TextField(QUESTION_TITLE_INDEX_PROPERTY, question.getTitle(), Field.Store.YES)); + doc.removeField(QUESTION_SUMMARY_INDEX_PROPERTY); + doc.add(new TextField(QUESTION_SUMMARY_INDEX_PROPERTY, question.getSummary(), Field.Store.YES)); + + doc.removeFields(QUESTION_THEME_INDEX_PROPERTY); + Set<String> themes = question.getThemes(); + for (String theme : themes) { + doc.add(new Field(QUESTION_THEME_INDEX_PROPERTY, theme, TextField.TYPE_STORED)); + } + + getLuceneUtils().getIndexWriter().updateDocument(new Term(QUESTION_ID_INDEX_PROPERTY, question.getId()), doc); + + } else { + // Not exist yet : add it to index + + Document doc = new Document(); + doc.add(new StringField(QUESTION_ID_INDEX_PROPERTY, question.getId(), Field.Store.YES)); + + doc.add(new TextField(QUESTION_TITLE_INDEX_PROPERTY, question.getTitle(), Field.Store.YES)); + doc.add(new TextField(QUESTION_SUMMARY_INDEX_PROPERTY, question.getSummary(), Field.Store.YES)); + doc.add(new Field("type", DOCUMENT_TYPE, TextField.TYPE_STORED)); + + Set<String> themes = question.getThemes(); + for (String theme : themes) { + doc.add(new Field(QUESTION_THEME_INDEX_PROPERTY, theme, TextField.TYPE_STORED)); + } + getLuceneUtils().getIndexWriter().addDocument(doc); - getLuceneUtils().getIndexWriter().addDocument(doc); + } + + // Commit, close reader. getLuceneUtils().getIndexWriter().commit(); + ireader.close(); } @@ -107,37 +140,6 @@ public class QuestionsIndexationService extends CoselmarSimpleServiceSupport { return documentIds; } - public void updateQuestion(QuestionBean question) throws IOException { - DirectoryReader ireader = DirectoryReader.open(getLuceneUtils().getIndexWriter(), false); - IndexSearcher isearcher = new IndexSearcher(ireader); - - // Retrieve document - BooleanQuery query = new BooleanQuery(); - query.add(new TermQuery(new Term(QUESTION_ID_INDEX_PROPERTY, question.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(QUESTION_TITLE_INDEX_PROPERTY); - doc.add(new TextField(QUESTION_TITLE_INDEX_PROPERTY, question.getTitle(), Field.Store.YES)); - doc.removeField(QUESTION_SUMMARY_INDEX_PROPERTY); - doc.add(new TextField(QUESTION_SUMMARY_INDEX_PROPERTY, question.getSummary(), Field.Store.YES)); - - doc.removeFields(QUESTION_THEME_INDEX_PROPERTY); - Set<String> themes = question.getThemes(); - for (String theme : themes) { - doc.add(new Field(QUESTION_THEME_INDEX_PROPERTY, theme, TextField.TYPE_STORED)); - } - - getLuceneUtils().getIndexWriter().updateDocument(new Term(QUESTION_ID_INDEX_PROPERTY, question.getId()), doc); - getLuceneUtils().getIndexWriter().commit(); - } - - ireader.close(); - } - public void deleteQuestion(String documentId) throws IOException { // Retrieve document diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java index ed57270..2434279 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/QuestionsWebService.java @@ -24,6 +24,7 @@ package fr.ifremer.coselmar.services.v1; * #L% */ +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -49,6 +50,7 @@ import fr.ifremer.coselmar.persistence.entity.Status; import fr.ifremer.coselmar.services.CoselmarWebServiceSupport; import fr.ifremer.coselmar.services.errors.InvalidCredentialException; import fr.ifremer.coselmar.services.errors.UnauthorizedException; +import fr.ifremer.coselmar.services.indexation.QuestionsIndexationService; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -199,6 +201,21 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { } commit(); + + QuestionBean result = BeanEntityConverter.toBean(getPersistenceContext().getTopiaIdFactory(), questionEntity); + + QuestionsIndexationService questionsIndexationService = getServicesContext().newService(QuestionsIndexationService.class); + try { + questionsIndexationService.indexQuestion(result); + if (log.isDebugEnabled()) { + String message = String.format("Question '%s' added to index", result.getTitle()); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to index new question", e); + } + } } public List<QuestionBean> getQuestions() throws InvalidCredentialException, UnauthorizedException { @@ -305,6 +322,20 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { getPersistenceContext().getCoselmarUserGroupDao().delete(participantGroup); commit(); + + // Remove it from index too + QuestionsIndexationService questionsIndexationService = getServicesContext().newService(QuestionsIndexationService.class); + try { + questionsIndexationService.deleteQuestion(questionId); + if (log.isDebugEnabled()) { + String message = String.format("Question '%s' deleted from index", questionId); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to remove question from index", e); + } + } } public QuestionBean getQuestion(String questionId) throws InvalidCredentialException, UnauthorizedException { @@ -661,6 +692,21 @@ public class QuestionsWebService extends CoselmarWebServiceSupport { } commit(); + + QuestionBean result = BeanEntityConverter.toBean(getPersistenceContext().getTopiaIdFactory(), questionEntity); + + QuestionsIndexationService questionsIndexationService = getServicesContext().newService(QuestionsIndexationService.class); + try { + questionsIndexationService.indexQuestion(result); + if (log.isDebugEnabled()) { + String message = String.format("Question '%s' added to index", result.getTitle()); + log.debug(message); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Unable to index new question", e); + } + } } public List<String> getThemes() throws InvalidCredentialException, UnauthorizedException { diff --git a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationServiceTest.java b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationServiceTest.java index fff64be..d6175e8 100644 --- a/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationServiceTest.java +++ b/coselmar-rest/src/test/java/fr/ifremer/coselmar/services/indexation/QuestionsIndexationServiceTest.java @@ -168,7 +168,7 @@ public class QuestionsIndexationServiceTest extends AbstractCoselmarServiceTest questionsIndexationService.indexQuestion(questionTwo); questionOne.setTitle("How old is the doctor who ?"); - questionsIndexationService.updateQuestion(questionOne); + questionsIndexationService.indexQuestion(questionOne); // Ok, let's search now ! List<String> questionMatchingTitleIds = questionsIndexationService.searchQuestion("awesome"); @@ -256,7 +256,6 @@ public class QuestionsIndexationServiceTest extends AbstractCoselmarServiceTest Assert.assertEquals(1, documentMatchingPartOfSummaryIds.size()); Assert.assertEquals(questionTwoId, documentMatchingPartOfSummaryIds.get(0)); - } @Before -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.