Java教程

Springboot web+Sping Data Elastcsearch

本文主要是介绍Springboot web+Sping Data Elastcsearch,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Springboot 2.3.7.RELEASE

application.yml

spring:
  application:
    name: es-application
  elasticsearch:
    rest:
      uris: http://localhost:9200

pojo

package com.example.estest03.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "users")
public class User {
    @Id
    private Integer id;

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

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

    @Field(type = FieldType.Double)
    private Double height;
}

interface

package com.example.estest03.dao;

import com.example.estest03.pojo.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface UserRepository extends ElasticsearchRepository<User, Integer> {
    /**
     * 根据name查询
     * @param name
     * @return
     */
    List<User> findByName(String name);

    /**
     * 根据age查询
     * @param age
     * @return
     */
    List<User> findByAge(Integer age);

    /**
     * 根据height查询
     * @param height
     * @return
     */
    List<User> findByHeight(Double height);

    /**
     * 根据name和aga查询
     * @param name
     * @param age
     * @return
     */
    List<User> findByNameAndAge(String name, Integer age);

    /**
     * 分页查询,需要指定Pageable参数
     * 这里name可以有多个,或关系
     * @param pageable
     * @param names
     * @return
     */
    List<User> findByNameIn(Pageable pageable, String...names);
}

service

package com.example.estest03.service;

import com.example.estest03.pojo.User;
import org.springframework.data.domain.Pageable;

import java.util.Iterator;
import java.util.List;

public interface IElasticService {
    /**
     *  向索引中添加一个Document
     * @return
     */
    User saveDoc(User user);

    /**
     * 向索引中添加多个Document
     * @return
     */
    Iterable<User> saveDocAll(List<User> users);

    /**
     * 根据ID删除Doc
     * @return
     */
    void deletDoc(Integer id);

    /**
     * 查询所有Doc
     * @return
     */
    Iterator<User> FinAll();

    /**
     * 根据ID查询Doc
     * @return
     */
    User FinByID(Integer id);

    /**
     * 查询所有Doc后根据字段排序--升序
     * @return
     */
    Iterable<User> FindAllSortAsc(String field);

    /**
     * 查询所有Doc后根据字段排序--降序
     * @return
     */
    Iterable<User> FindAllSortDesc(String field);

    /**
     * 根据name查询
     * @param name
     * @return
     */
    List<User> findByName(String name);

    /**
     * 根据age查询
     * @param age
     * @return
     */
    List<User> findByAge(Integer age);

    /**
     * 根据height查询
     * @param height
     * @return
     */
    List<User> findByHeight(Double height);

    /**
     * 根据name和aga查询
     * @param name
     * @param age
     * @return
     */
    List<User> findByNameAndAge(String name, Integer age);

    /**
     * 分页查询,需要指定Pageable参数
     * 这里name可以有多个,或关系
     * @param pageable
     * @param names
     * @return
     */
    List<User> findByNameIn(Pageable pageable, String...names);
}

impl

package com.example.estest03.impl;

import com.example.estest03.dao.UserRepository;
import com.example.estest03.pojo.User;
import com.example.estest03.service.IElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service("elasticService")
public class ElasticServiceImpl implements IElasticService {
    @Autowired
    UserRepository userRepository;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public User saveDoc(User user) {
        return userRepository.save(user);
    }

    @Override
    public Iterable<User> saveDocAll(List<User> users) {
        return userRepository.saveAll(users);
    }

    @Override
    public void deletDoc(Integer id) {
        userRepository.deleteById(id);
    }

    @Override
    public Iterator<User>  FinAll() {
        return userRepository.findAll().iterator();
    }

    @Override
    public User FinByID(Integer id) {
        return userRepository.findById(id).get();
    }

    @Override
    public Iterable<User> FindAllSortAsc(String field) {
        return userRepository.findAll(Sort.by(Sort.Order.asc(field)));
    }

    @Override
    public Iterable<User> FindAllSortDesc(String field) {
        return userRepository.findAll(Sort.by(Sort.Order.desc(field)));
    }

    @Override
    public List<User> findByName(String name) {
        return userRepository.findByName(name);
    }

    @Override
    public List<User> findByAge(Integer age) {
        return userRepository.findByAge(age);
    }

    @Override
    public List<User> findByHeight(Double height) {
        return userRepository.findByHeight(height);
    }

    @Override
    public List<User> findByNameAndAge(String name, Integer age) {
        return userRepository.findByNameAndAge(name, age);
    }

    @Override
    public List<User> findByNameIn(Pageable pageable, String... names) {
        return userRepository.findByNameIn(pageable,names);
    }
}

controller

package com.example.estest03.controller;

import com.example.estest03.pojo.User;
import com.example.estest03.service.IElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;

@RestController
@RequestMapping("/elastic")
public class ElasticController {

    @Autowired
    private IElasticService elasticService;

    @GetMapping("/all")
    public Iterator<User> all(){
        return elasticService.FinAll();
    }
}

原始查询

   /**
     * 原始查询
     */
    @Test
    void query(){
        //构建查询条件
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery("zhangsan和lisi"))//指定使用的查询方式
                .withSort(SortBuilders.fieldSort("age").order(SortOrder.DESC))//排序--降序
                .withPageable(PageRequest.of(0,10))//分页
                .build();

        //执行查询方法
        SearchHits<User> searchHits = elasticsearchRestTemplate.search(searchQuery, User.class);
        // for (SearchHit<User> searchHit : searchHits) {
        //     System.out.println(searchHit.getContent());
        // }

        searchHits.forEach(searchHit -> System.out.println(searchHit.getContent()));
    }
这篇关于Springboot web+Sping Data Elastcsearch的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!