美文网首页
elk--笔记5-Springboot集成ElasticSear

elk--笔记5-Springboot集成ElasticSear

作者: 牵手生活 | 来源:发表于2020-02-16 10:38 被阅读0次

idea新建一个module

idea/file/new/module/Spring Initializr

module配置 module配置2

springboot 集成 Elasticsearch7.2.0 ,head 插件

pom.xml导入包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>starter-data-elasticsearch</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

项目结构


完成es测试结构
module的applicaiton.properties配置
spring.data.elasticsearch.cluster-nodes=192.168.1.116:9300
# 该配置和Elasticsearch的elasticsearch.yml中的配置信息有关
spring.data.elasticsearch.cluster-name=czg-es-springboot
创建EsCustomer实体

@Data
//useServerConfiguration 表示使用线上的index map和index配置
//createIndex 是否自动插件document实体对应的index
//设置elasticsearch的实体映射 http://www.luyixian.cn/news_show_103399.aspx
//注意之前设置为doc,在调用save时报错,后来改为_do
@Document(indexName = "cztest",type = "_doc",createIndex = false)  //useServerConfiguration = true
public class EsCustomer {

    @Field(type = FieldType.Long)
    @Id
    Long id;
    @Field(type = FieldType.Text,analyzer = "")  //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
    private String firstName;  //first_name
    @Field(type = FieldType.Text,analyzer = "")  //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
    private String lastName;  //last_name
    @Field(type = FieldType.Integer)
    private int age;
    @Field(type = FieldType.Text,analyzer = "")  //如果涉及中文需要使用analyzer = "ik_max_word",主要es需要安装ik_max_word插件
    private String about;
/*    @Field(type = FieldType.Text)
    private String[] interests;*/

}
创建接口EsCustomerRepository继承ElasticsearchRepository(提供类似jpa的接口实现)
public interface EsCustomerRepository extends ElasticsearchRepository<EsCustomer,Integer>{
}
编写单元测试用例EsCustomerRepositoryTest

@RunWith(SpringRunner.class)
@SpringBootTest
public class EsCustomerRepositoryTest {
/*    @Autowired
    ElasticsearchTemplate template;*/
    @Autowired
    EsCustomerRepository esCustomerRepository;

    //node {#transport#-1}{dReBn_47QuuIGRuK4n0EpA}{192.168.1.116}{192.168.1.116:9300} not part of the cluster Cluster [Hq5OeP5TQmO6OVP3puv7RQ], ignoring...
    //no such index [springbootCustomer]  https://blog.csdn.net/chen_2890/article/details/83895646
    @Test
    public void testEs_all(){
        Iterable<EsCustomer> all = esCustomerRepository.findAll();
        Iterator<EsCustomer> iterator = all.iterator();
        EsCustomer esCustomer = iterator.next();
        if (null !=esCustomer){
            System.out.println("------"+esCustomer.getAbout());
        }


    }
    @Test
    public void testEs_sava(){
        EsCustomer esCustomer = new EsCustomer();
        esCustomer.setId(3L);
        esCustomer.setFirstName("cai");
        esCustomer.setLastName("haiyun");
        esCustomer.setAbout("喜欢打球");
        esCustomer.setAge(33);
        esCustomerRepository.save(esCustomer);
        System.out.println("ok");


    }


 /*   POST cztest/_search
    {
        "query": {
        "bool": {
            "should": [
            {"match": {
                "last_name": "haibin"
            }},
            {"match": {
                "last_name": "yueliang"
            }}
      ]
        }
    }
    }*/

    @Test
    public void testEs_search(){
        String keyword = "hai";
        String keyword2 = "haibin";

        HashMap<String,Object> map = new HashMap<>();
        StopWatch watch = new StopWatch();
        watch.start();//开始计时


        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.should(QueryBuilders.matchPhraseQuery("last_name",keyword));
        builder.should(QueryBuilders.matchPhraseQuery("last_name",keyword2));
        String s = builder.toString();
        /*Iterable<EsCustomer> search = esCustomerRepository.search(builder);*/
        Page<EsCustomer> search = (Page<EsCustomer>) esCustomerRepository.search(builder);
        List<EsCustomer> content = search.getContent();
        map.put("list",content);

        watch.stop();//结束计时

        long totalMillis = watch.totalTime().getMillis();
        map.put("duration",totalMillis);

    }

}

测试效果
es单元测试效果

相关文章

网友评论

      本文标题:elk--笔记5-Springboot集成ElasticSear

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