Springboot集成ES及JPA
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot集成ES及JPA相关的知识,希望对你有一定的参考价值。
参考技术A springboot maven项目结构package com.example.esboot.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Data
@NoArgsConstructor
@Accessors(chain =true)
@Document(indexName ="person",type ="_doc", shards =1, replicas =0)
public class Person
private Integerid;
private Stringname;
private Stringsex;
public Integer getId() return id;
public void setId(Integer id) this.id = id;
public String getName() return name;
public void setName(String name) this.name = name;
public String getSex() return sex;
public void setSex(String sex) this.sex = sex;
Elasticsearch 的JPA 使用
PersonRepository.java
package com.example.esboot.repository;
import com.example.esboot.entity.Person;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface PersonRepositoryextends ElasticsearchRepository
@Highlight(fields =
@HighlightField(name ="name"),
@HighlightField(name ="sex")
)
List> findByNameOrSex(String text, String sex);
PersonService.java
package com.example.esboot.servcie;
import com.example.esboot.entity.Person;
import java.util.List;
public interface PersonService
public Person findById(String id);
public String save(Person person);
Iterable findAll();
List findByNameOrSex(String text,String sex);
PersonServiceImpl.java
package com.example.esboot.servcie.impl;
import com.example.esboot.entity.Person;
import com.example.esboot.repository.PersonRepository;
import com.example.esboot.servcie.PersonService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class PersonServiceImplimplements PersonService
@Resource
PersonRepositorypersonRepository;
@Override
public List findByNameOrSex(String text, String sex)
List list =new ArrayList<>();
personRepository.findByNameOrSex(text, sex).forEach(personSearchHit ->list.add(personSearchHit.getContent()));
return list;
@Override
public Person findById(String id)
return personRepository.findById(id).get();
@Override
public String save(Person person)
return personRepository.save(person).getName();
@Override
public Iterable findAll()
return personRepository.findAll();
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.connection-timeout=300s
spring.data.elasticsearch.repositories.enabled=true
EsTest.java
package com.example.esboot.es;
import com.example.esboot.entity.Person;
import com.example.esboot.servcie.PersonService;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.junit.Test;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EsTest
@Resource
private ElasticsearchOperationselasticsearchOperations;
@Resource
private PersonServicepersonService;
@Test
public void test()
Person person =new Person();
person.setId(1);
person.setName("baijie");
person.setSex("n");
IndexQuery indexQuery =new IndexQueryBuilder().withId(String.valueOf(person.getId())).withObject(person).build();
String docId =elasticsearchOperations.index(indexQuery, IndexCoordinates.of("person"));
System.out.println(docId);
@Test
public void save()
Person person =new Person();
person.setId(2);
person.setName("中国");
person.setSex("男");
String name =personService.save(person);
System.out.println(name);
@Test
public void findByNameOrSex()
List byNameOrSex =personService.findByNameOrSex("baijie","男");
System.out.println(byNameOrSex);
@Test
public void findAll()
personService.findAll().forEach(person ->
System.out.println(person);
);
@Test
public void queryForObject()
Person person =personService.findById("1");
System.out.println(person);
以上是关于Springboot集成ES及JPA的主要内容,如果未能解决你的问题,请参考以下文章
JasperReports 怎么与 spring boot集成