美文网首页
Spring-data-elasticsearch - 集成

Spring-data-elasticsearch - 集成

作者: saoraozhe3hao | 来源:发表于2018-11-16 14:14 被阅读0次

    官网:https://spring.io/projects/spring-data-elasticsearch

    Spring-data-elasticsearch 应用组成

    1、配置Maven依赖

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>
    

    2、Spring Bean配置

    <context:component-scan base-package="com.hogen.*" />
    <elasticsearch:repositories base-package="com.hogen.repositories" />  <!-- 扫描DAO层 -->
    
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,127.0.0.1:9300"/> <!-- 指定集群,端口为TCP服务的端口,而非HTTP服务对应的9200 -->
    <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>
    

    3、POJO类

    public class Person {
        @Id
        @Field(index=false,store=true,type=FieldType.Integer)
        private Integer id;
        @Field(index=true,analyzer="ik",store=true,searchAnalyzer="ik",type=FieldType.Text)
        private String title;
        @Field(index=true,analyzer="ik",store=true,searchAnalyzer="ik",type=FieldType.Text)
        private String content;
    }
    

    4、DAO接口

    // 会自动生成具体实现
    public interface PersonRepository extends ElasticsearchRepository<Person, Integer> {
        List<Person> findByTitle(String title);   // 模糊查询
       @Query("{'bool': {'must' : {'field' : {'title' : '?0'}}}}")   // 指定查询json
        Page<Person> findByTitle(String title, Pageable pageable);   // 分页查询
    }
    

    5、Service层

    @Service
    public class PersonServiceImpl {
        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;
        @Autowired
        private PersonRepository personRepository;
    
        public void createIndex() {
            elasticsearchTemplate.createIndex(Person.class);   // 创建索引
            elasticsearchTemplate.putMapping(Person.class);   // 设置映射
        }
    
        public void save(Person person){
            personRepository.save(person);
        }
        public void delete(Person article) {
            personRepository.delete(article);
        }
        public Iterable<Person> findAll() {
            return personRepository.findAll();
        }
    
        public List<Person> findByTitle(String title) {
            return personRepository.findByTitle(title);
        }
        public Page<Person> findByTitle(String title, Pageable pageable) {
            return personRepository.findByTitle(title, pageable);
        }
    }
    

    6、使用

    @Autowired
    private PersonServiceImpl personService;
    

    使用场景

    1、在RDS中不是索引的字段,但又要被搜索,可以将这个字段和ID存在ES。查询时从ES中找出这条数据的ID,再拿ID去RDS查出完整数据

    相关文章

      网友评论

          本文标题:Spring-data-elasticsearch - 集成

          本文链接:https://www.haomeiwen.com/subject/iilmfqtx.html