CrescentIndexerExecutor { public String indexing(CrescentCollection collection, IndexingRequestForm indexingRequestForm) { logger.info("indexingRequestForm : {}", indexingRequestForm); IndexingType indexingType = IndexingType.valueOf(indexingRequestForm.getIndexingType().toUpperCase()); IndexingCommand indexingCommand = IndexingCommand.valueOf(indexingRequestForm.getCommand().toUpperCase()); String query = indexingRequestForm.getQuery(); logger.info("Indexing type : {} , Indexing command : {} ", indexingType, indexingCommand); logger.info("Query : {}", query); String resultMessage = "Nothing To Execute..."; if(IndexingCommand.ADD == indexingCommand) { List<Document> documentList = LuceneDocumentBuilder.buildDocumentList(indexingRequestForm.getDocumentList(), collection.getCrescentFieldByName()); crescentIndexer.addDocument(documentList, collection.getName()); resultMessage = documentList.size() + "건의 색인이 완료되었습니다."; } else if (IndexingCommand.UPDATE == indexingCommand) { List<Document> documentList = LuceneDocumentBuilder.buildDocumentList(indexingRequestForm.getDocumentList(), collection.getCrescentFieldByName()); if(documentList.size() == 0) { logger.error("업데이트 할 document가 없습니다."); throw new IllegalStateException("업데이트 할 document가 없습니다."); } String[] splitQuery = query.split(":"); if(splitQuery.length != 2) { logger.error("Update 대상 문서를 찾을 Query식이 잘못되었습니다. [{}]", query); throw new IllegalStateException("Update 대상 문서를 찾을 Query식이 잘못되었습니다. ["+query+"]"); } String field = query.split(":")[0]; String value = query.split(":")[1]; logger.info("field : {}, value : {}", field, value); Term updateTerm = new Term(field, value); crescentIndexer.updateDocuments(updateTerm, documentList, collection.getName()); resultMessage = updateTerm.toString() + "에 대한 update가 완료되었습니다."; } else if (IndexingCommand.UPDATE_BY_FIELD_VALUE == indexingCommand) { List<Document> documentList = LuceneDocumentBuilder.buildDocumentList(indexingRequestForm.getDocumentList(), collection.getCrescentFieldByName()); if(documentList.size() == 0) { logger.error("업데이트 할 document가 없습니다."); throw new IllegalStateException("업데이트 할 document가 없습니다."); } String field = query.split(":")[0]; String value = query.split(":")[1]; logger.info("field : {}, value : {}", field, value); for(Document document : documentList) { value = document.get(field); if(value == null || value.length() == 0) { logger.error("Update 대상 문서를 찾을 field지이 잘못되었거나 field : [{}], value가 없습니다. value : [{}]", field, value); throw new IllegalStateException("pdate 대상 문서를 찾을 field지이 잘못되었거나 field : ["+field+"], value가 없습니다. value : ["+value+"]"); } Term updateTerm = new Term(field, value); crescentIndexer.updateDocument(updateTerm, document, collection.getName()); } resultMessage = query + "에 대한 update가 완료되었습니다."; } else if (IndexingCommand.DELETE == indexingCommand) { String[] splitQuery = query.split(":"); if(splitQuery.length != 2) { logger.error("Delete 대상 문서를 찾을 Query식이 잘못되었습니다. [{}]", query); throw new IllegalStateException("Delete 대상 문서를 찾을 Query식이 잘못되었습니다. ["+query+"]"); } String field = query.split(":")[0]; String value = query.split(":")[1]; logger.info("field : {}, value : {}", field, value); Term deleteTerm = new Term(field, value); Query deleteTermQuery = new TermQuery(deleteTerm); crescentIndexer.deleteDocument(deleteTermQuery, collection.getName()); resultMessage = deleteTerm.toString() + "에 대한 delete가 완료되었습니다."; } if(IndexingType.BULK == indexingType) { crescentIndexer.commit(collection.getName()); } return resultMessage; }  String indexing(CrescentCollection collection, IndexingRequestForm indexingRequestForm);  }
@Test public void addDocument() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getAddDocBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("1건의 색인이 완료되었습니다.", returnMessage); }
@Test public void deleteDocument() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getDeleteDocBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:test에 대한 delete가 완료되었습니다.", returnMessage); }
@Test public void updateDocument() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getUpdateDocBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:test에 대한 update가 완료되었습니다.", returnMessage); }
@Test public void updateNewDocument() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getUpdateNewDocBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:testnew에 대한 update가 완료되었습니다.", returnMessage); SearchRequest searchRequest = new SearchRequest(); searchRequest.setKeyword("testnew"); searchRequest.setSearchField("creuser"); searchRequest.setCollectionName("sample"); CrescentSearchRequestWrapper csrw = new CrescentSearchRequestWrapper(searchRequest); SearchResult searchResult = crescentDocSearcher.search(csrw); Assert.assertTrue(searchResult.getResultList().size() == 1); }
@Test public void updateNewDocuments() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getUpdateNewDocListBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:testnew에 대한 update가 완료되었습니다.", returnMessage); SearchRequest searchRequest = new SearchRequest(); searchRequest.setKeyword("testnew"); searchRequest.setSearchField("creuser"); searchRequest.setCollectionName("sample"); CrescentSearchRequestWrapper csrw = new CrescentSearchRequestWrapper(searchRequest); SearchResult searchResult = crescentDocSearcher.search(csrw); Assert.assertTrue(searchResult.getResultList().size() == 2); }
@Test public void updateByFieldValueDocument() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getUpdateByFieldValueDocBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:*에 대한 update가 완료되었습니다.", returnMessage); SearchRequest searchRequest = new SearchRequest(); searchRequest.setKeyword("test"); searchRequest.setSearchField("creuser"); searchRequest.setCollectionName("sample"); CrescentSearchRequestWrapper csrw = new CrescentSearchRequestWrapper(searchRequest); SearchResult searchResult = crescentDocSearcher.search(csrw); Assert.assertTrue(searchResult.getResultList().size() == 1); Assert.assertEquals("제목 입니다0 업데이트...", searchResult.getResultList().get(0).get("title")); }
@Test public void updateByFieldValueNewDocumentList() throws CorruptIndexException, IOException { CrescentCollections crescentCollections = collectionHandler.getCrescentCollections(); Map<String, CrescentCollection> collections = crescentCollections.getCrescentCollectionsMap(); CrescentCollection sampleCollection = collections.get("sample"); Handler handler = new JsonDataHandler(); IndexingRequestForm indexingRequestForm = handler.handledData(FormattedTextBuilder.getUpdateByFieldValueNewDocListBulkJsonForm()); String returnMessage = executor.indexing(sampleCollection, indexingRequestForm); Assert.assertEquals("creuser:*에 대한 update가 완료되었습니다.", returnMessage); SearchRequest searchRequest = new SearchRequest(); searchRequest.setKeyword("testnew"); searchRequest.setSearchField("creuser"); searchRequest.setCollectionName("sample"); CrescentSearchRequestWrapper csrw = new CrescentSearchRequestWrapper(searchRequest); SearchResult searchResult = crescentDocSearcher.search(csrw); Assert.assertTrue(searchResult.getResultList().size() == 1); Assert.assertEquals("제목 입니다1 업데이트...", searchResult.getResultList().get(0).get("title")); }