SpringBoot整合Elasticsearch

1.下载es软件

2.在application.properties

#elasticsearch配置
#该名称需要和安装的es配置文件里面的保持一致
spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#连接超时的时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

3.在启动文件加入以下配置

 @PostConstruct
  void init() {
    System.setProperty("es.set.netty.runtime.available.processors", "false");
  }

4.创建实体类

package com.xiao.system.bean;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.Data;

/**
 * elasticsearch使用测试类
 * 
 * @author xiaoss
 *
 */
@Document(indexName = "bolg", type = "bolg") // 文档
@Data
public class EsBolg implements Serializable {

  @Id // 文档主键,唯一标识
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private String id;

  private String title;// 标题

  private String summary;// 摘要

  private String content;// 正文

  public EsBolg() {

  }

  public EsBolg(String title, String summary, String content) {
    this.title = title;
    this.summary = summary;
    this.content = content;
  }

  @Override
  public String toString() {
    return String.format("EsBolg[id='%s',title='%s',summary='%s',content='%s']", id, title, summary, content);
  }

}
5.创建dao

public interface EsBolgRepository extends ElasticsearchRepository<EsBolg, String> {

  /**
   * 分页查询博客
   * 
   * @param title
   * @param summary
   * @param content
   * @param pageable
   * @return
   */
  Page<EsBolg> findDistinctEsBolgByTitleContainingOrSummaryContainingOrContentContaining(String title, String summary,
      String content, Pageable pageable);

  List<EsBolg> findByTitle(String title);
}

6.测试dao

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppConfiguration.class)
public class EsBolgRepositoryTest {

  @Resource
  private EsBolgRepository esBolgRepository;

  @Before
  public void initReposutoryData() {
    // 清除所有数据
    /*
     * esBolgRepository.deleteAll(); esBolgRepository.save(new EsBolg("登黄鹤楼",
     * "王之涣的登黄鹤楼", "白日依山尽,黄鹤入海流。欲穷千里目,更上一层楼。")); esBolgRepository.save(new
     * EsBolg("相思", "王维的相思", "红豆生南国,春来发几支。愿君多采颉,此物最相思"));
     * esBolgRepository.save(new EsBolg("静夜思", "李白的静夜思",
     * "床前明月光,疑是地上霜。举头望明月,低头思故乡。"));
     */
  }

  @Test
  public void testFindDistinctEsBolgByTitleContainingOrSummaryContainingOrContentContaining() {
    Pageable pageable = new PageRequest(0, 20);
    String title = "思";
    String summary = "相思";
    String content = "相思";
    Page<EsBolg> page = esBolgRepository
        .findDistinctEsBolgByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);
    System.out.println("--------start");
    for (EsBolg esBolg : page.getContent()) {
      System.out.println(esBolg.toString());
    }
    System.out.println("--------end");
  }

 

测试结果:

SpringBoot整合Elasticsearch

SpringBoot整合Elasticsearch

 

上一篇:tensorboard可视化图和损失曲线


下一篇:使用torchsummary打印torch模型结构,包括每层名字以及形状