springboot配置整合elasticsearchjava
import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.TransportAddress; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import javax.annotation.Resource; /** * Created by sherlock on 16/9/21. */ @Configuration @PropertySource(value = "classpath:/elasticsearch.properties") @EnableElasticsearchRepositories(basePackages = "com.mikasa.springboot.example.elasticsearch") public class ElasticsearchConfiguration { @Resource private Environment environment; @Bean public Client client() { TransportClient client = new TransportClient(); TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port"))); client.addTransportAddress(address); return client; } @Bean public ElasticsearchOperations elasticsearchTemplate() { return new ElasticsearchTemplate(client()); } }
elasticsearch.propertiesweb
elasticsearch.host=localhost elasticsearch.port=9300
本身封裝倉庫,繼承ElasticsearchRepository<Object, ID>spring
import com.mikasa.springboot.example.domain.Post; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; /** * Created by sherlock on 16/9/21. */ public interface PostRepository extends ElasticsearchRepository<Post, String> { Page<Post> findByTagsName(String name, Pageable pageable); List<Post> findByRatingBetween(Double beginning, Double end); }
經過倉庫編寫邏輯方法springboot
import com.mikasa.springboot.example.domain.Post; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import java.util.List; /** * Created by sherlock on 16/9/21. */ @Service public class PostService { @Autowired PostRepository postRepository; public Post save(Post post){ postRepository.save(post); return post; } public Post findOne(String id) { return postRepository.findOne(id); } public Iterable<Post> findAll() { return postRepository.findAll(); } public Page<Post> findByTagsName(String tagName, PageRequest pageRequest) { return postRepository.findByTagsName(tagName, pageRequest); } List<Post> findByRatingBetween(Double beginning, Double end){ return postRepository.findByRatingBetween(beginning,end); } }
經過API調用測試接口app
import com.mikasa.springboot.example.domain.Post; import com.mikasa.springboot.example.domain.Tag; import com.mikasa.springboot.example.elasticsearch.PostService; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; /** * Created by sherlock on 16/9/21. */ @RestController @RequestMapping("/elasticsearch") public class ElasticsearchController { Logger log = LoggerFactory.getLogger(ElasticsearchController.class); @Autowired private PostService postService; @ApiOperation(value="測試保存elasticsearch", notes="elasticsearch保存Post") @RequestMapping(value = "/save",method = RequestMethod.GET) public Object save() { log.info("測試elasticsearch..."); Tag tag = new Tag(); tag.setId("3"); tag.setName("lol"); Tag tag2 = new Tag(); tag2.setId("4"); tag2.setName("java"); Post post = new Post(); post.setId("2"); post.setTitle("test_elasticsearch"); post.setRating(9.9); post.setTags(Arrays.asList(tag, tag2)); postService.save(post); return post; } @ApiOperation(value="測試獲取elasticsearch", notes="elasticsearch獲取list") @RequestMapping(value = "/list",method = RequestMethod.GET) public Object list() { log.info("測試獲取elasticsearch..."); Iterable<Post> iterable = postService.findAll(); return iterable; } @ApiOperation(value="經過id獲取post", notes="elasticsearch經過id獲取post") @ApiImplicitParam(name = "id", value = "ID", required = true, paramType = "path", dataType = "String") @RequestMapping(value = "/find{id}",method = RequestMethod.GET) public Object find(@PathVariable String id) { log.info("測試獲取elasticsearch...ById"); Post post = postService.findOne(id); return post; } }