springboot整合ES,springboot整合Elasticsearch

一,引入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>


        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-elasticsearchartifactId>
        dependency>

        <dependency>
            <groupId>net.logstash.logbackgroupId>
            <artifactId>logstash-logback-encoderartifactId>
            <version>6.6version>
        dependency>


        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.79version>
        dependency>
    dependencies>

二,配置yml文件

elasticsearch:
  url: 127.0.0.1
  port: 9200

三,在resources目录下添加setting.json文件指定分词器

{
  "analysis": {
    "analyzer": {
      "url_analyzer": {
        "tokenizer": "standard",
        "char_filter": [
          "url_char_filter"
        ]
      }
    },
    "char_filter": {
      "url_char_filter": {
        "mytokenizer": {
          "type": "ngram",
          "min_gram": 1,
          "max_gram": 2,
          "token_chars": [
            "letter",
            "digit",
            "whitespace",
            "punctuation",
            "symbol"
          ]
        }
      }
    }
  }
}

四,配置es客户端

@Data
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {

    @Value("${elasticsearch.url}")
    private String url;

    @Value("${elasticsearch.port}")
    private Integer port;


    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(url, port)));
        return client;
    }
}

五,配置dao层

@Repository
public interface StudentDao extends ElasticsearchRepository<Student,Long> {
    List<Student> findByName(String name);
}

Student类为document实体类,我们自己在dao层定义的方法,可以不用去实现 es会直接根据方法名来获取我们要的结果。
六,Student实体类


@ToString
@Data
@Document(indexName = "students")
@Setting(settingPath = "settings.json")
public class Student {

    @Id
    @Field(type = FieldType.Long)
    private Long id;

    @Field(type = FieldType.Text,fielddata = true,analyzer = "url_analyzer")
    private String name;

    @Field(type = FieldType.Keyword)
    private String sex;

    @Field(type = FieldType.Integer)
    private Integer age;

    @Field(type = FieldType.Text)
    private String address;
}

七,controller层

@RestController
@RequestMapping("/students")
public class StudentController {

    @Resource
    private StudentService studentService;


    @PostMapping("/save")
    public String save(@RequestBody  Student student){
        long id = System.currentTimeMillis();
        student.setId(id);
        studentService.save(student);
        return null;
    }

    @GetMapping("/get")
    public String findAll(){
        List<Student> stus = studentService.findAll();
        return stus.toString();
    }

    @GetMapping("/findByName")
    public String findByName( @RequestParam String name) {
        List<Student> stus = studentService.findByName(name);
        return stus.toString();
    }

八,service实现类内容

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public void save(Student stu) {
        studentDao.save(stu);
    }

    @Override
    public List<Student> findAll() {
        Iterable<Student> all = studentDao.findAll();
        Iterator<Student> iterator = all.iterator();
        List<Student> stus = new ArrayList<>();
        while (iterator.hasNext()){
            Student next = iterator.next();
            stus.add(next);
        }
        return stus;
    }

    @Override
    public List<Student> findByName(String name) {
        List<Student> stus = studentDao.findByName(name);
        return stus;
    }
}

九,service内容

public interface StudentService {
    void save(Student user);

    List<Student> findAll();

    List<Student> findByName(String name);

}

你可能感兴趣的:(elasticsearch,spring,boot,java)