Last active
January 4, 2016 14:42
-
-
Save geraldhumphries/852ea8460c68d23d7654 to your computer and use it in GitHub Desktop.
Elasticsearch Reindexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package [package].web.rest; | |
import com.codahale.metrics.annotation.Timed; | |
import [package].security.AuthoritiesConstants; | |
import [package].security.SecurityUtils; | |
import [package].service.ElasticsearchIndexService; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.security.access.prepost.PreAuthorize; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.inject.Inject; | |
import java.net.URISyntaxException; | |
/** | |
* REST controller for managing the Elasticsearch index. | |
*/ | |
@RestController | |
@RequestMapping("/api") | |
public class ElasticsearchIndexResource { | |
private final Logger log = LoggerFactory.getLogger(ElasticsearchIndexResource.class); | |
@Inject | |
private ElasticsearchIndexService elasticsearchIndexService; | |
/** | |
* POST /elasticsearch/index -> Reindex all Elasticsearch documents | |
*/ | |
@RequestMapping(value = "/elasticsearch/index", | |
method = RequestMethod.POST, | |
produces = MediaType.TEXT_PLAIN_VALUE) | |
@Timed | |
@PreAuthorize("hasAuthority('" + AuthoritiesConstants.ADMIN + "')") | |
public ResponseEntity<String> reindexAll() throws URISyntaxException { | |
log.info("REST request to reindex Elasticsearch by user : {}", SecurityUtils.getCurrentUserLogin()); | |
elasticsearchIndexService.reindexAll(); | |
return new ResponseEntity<>("Request accepted, performing full Elasticsearch reindexing.", HttpStatus.ACCEPTED); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package [package].service; | |
import com.codahale.metrics.annotation.Timed; | |
import [package].domain.*; | |
import [package].repository.*; | |
import [package].repository.search.*; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import javax.inject.Inject; | |
@Service | |
@Transactional | |
public class ElasticsearchIndexService { | |
private final Logger log = LoggerFactory.getLogger(ElasticsearchIndexService.class); | |
@Inject | |
private AddressRepository addressRepository; | |
@Inject | |
private AddressSearchRepository addressSearchRepository; | |
@Inject | |
private ElasticsearchTemplate elasticsearchTemplate; | |
@Async | |
@Timed | |
public void reindexAll() { | |
elasticsearchTemplate.deleteIndex(Address.class); | |
if (addressRepository.count() > 0) { | |
addressSearchRepository.save(addressRepository.findAll()); | |
} | |
log.info("Elasticsearch: Indexed all addresses"); | |
// repeat for all entities | |
log.info("Elasticsearch: Successfully performed reindexing"); | |
} | |
} |
You're right, I've updated it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, it works! I think the
@Inject private MeetingSearchRepository meetingSearchRepository;
is missing.