快速掌握通过Spring Data框架操作Elasticsearch

一、通过原生的Java代码操作ES

(1)运行环境:idea、maven3.5、spring

(2)pom.xml 文件中需要配置的工具包

    <maven.compiler.source>1.8

    <maven.compiler.target>1.8

        <!--测试类架包-->

            <groupId>junit

            <artifactId>junit

            <version>4.12

            <scope>test

        <!--ES 相关包-->

            <groupId>org.elasticsearch

            <artifactId>elasticsearch

            <version>5.6.1

            <groupId>org.elasticsearch.client

            <artifactId>transport

            <version>5.6.1

        <!--日志相关包-->

            <groupId>org.apache.logging.log4j

            <artifactId>log4j-to-slf4j

            <version>2.10.0

            <groupId>org.apache.logging.log4j

            <artifactId>log4j-core

            <version>2.9.0

            <groupId>org.slf4j

            <artifactId>slf4j-api

            <version>1.7.25

            <groupId>org.slf4j

            <artifactId>slf4j-simple

            <version>1.7.21

            <groupId>log4j

            <artifactId>log4j

            <version>1.2.12

        <!--多导入一个包,否则报日志错误-->

            <groupId>org.apache.logging.log4j

            <artifactId>log4j-api

            <version>2.9.1

        <!--json数据相关包-->

            <groupId>com.fasterxml.jackson.core

            <artifactId>jackson-core

            <version>2.8.1

            <groupId>com.fasterxml.jackson.core

            <artifactId>jackson-databind

            <version>2.8.1

            <groupId>com.fasterxml.jackson.core

            <artifactId>jackson-annotations

            <version>2.8.1

        <!--spring Data框架相关包-->

            <groupId>org.springframework.data

            <artifactId>spring-data-elasticsearch

            <version>3.0.5.RELEASE

                    <groupId>org.elasticsearch.plugin

                    <artifactId>transport-netty4-client

            <groupId>org.springframework

            <artifactId>spring-test

            <version>5.0.5.RELEASE

</project>


(3)在resources文件夹创建applicationContext.xml配置文件

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

         http://www.springframework.org/schema/beans/spring-beans.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context.xsd

   http://www.springframework.org/schema/data/elasticsearch

   http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">

                                   cluster-nodes="127.0.0.1:9301,127.0.0.1:9302,127.0.0.1:9303"/>

</beans>

(4)创建一个实体(构建 :无参构造、set、get方法)

@Document(indexName ="es_blog",type = "article")

public class Article {

   @Id

   @Field(type = FieldType.Long,store=true)

   private long id;

   @Field(type = FieldType.text,store=true,analyzer="ik_smart")

   private String title;

(5)创建一个接口

package com.xcy.repositories;

import com.xcy.pojo.Article;

importorg.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleRepository extendsElasticsearchRepository {

}

(6)测试

1.创建一个索引库

2.向索引库中插入数据

结果:

3.删除库中的数据

4.查询


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容