Spring Boot + Spring Data JPA + Spring Data Elasti

2019-07-25 17:07发布

问题:

I am new to setting up embedded Elasticsearch into my Spring Boot application where Spring Data JPA is setup with Postgres database.

Now I've also added support for Elastic Search Spring Data repositories (or so I thought). The problem is that ES searches do not return anything (JSON array is empty), whilst JPA ones work correctly.

I read that people need an index tool that runs every now and then, but I couldn't find anything related to this in the Spring Data Elastic Search documents.

Do I understand correctly that you need to index the searches from the database constantly? Is the answer provided in this topic Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch the only solution:

Here is the Application class:

@SpringBootApplication
@EnableJpaRepositories(basePackages = "eu.deniss.repository")
@ComponentScan
public class SpringDataElasticsearchDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataElasticsearchDemoApplication.class, args);
    }
}

Person Entity class:

@Entity
@Document(indexName = "person", type = "person")
public class Person {
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    private String gender;
    private String ipAddress;

    @Id
    @org.springframework.data.annotation.Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
...
}

PersonSearchRepository class:

public interface PersonSearchRepository extends ElasticsearchRepository<Person, Long> {
}

PersonServiceImpl class:

@Service
public class PersonServiceImpl implements PersonService {

    private final PersonRepository personRepository;
    private final PersonSearchRepository personSearchRepository;
    private final PersonMapper personMapper;

    private static final Logger log = Logger.getLogger(PersonServiceImpl.class);

    public PersonServiceImpl(PersonRepository personRepository, PersonSearchRepository personSearchRepository, PersonMapper personMapper) {
        this.personRepository = personRepository;
        this.personSearchRepository = personSearchRepository;
        this.personMapper = personMapper;
    }

...

    @Override
    @Transactional(readOnly = true)
    public Page<PersonDTO> search(String query, Pageable pageable) {
        log.info("Request to search for a page of People with a query " + query);
        Page<Person> result = personSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(person -> personMapper.personToPersonDTO(person));
    }
}

PersonController class:

@RestController()
@RequestMapping("/api")
public class PersonController {

    private final PersonService personService;
    private final Logger log = LoggerFactory.getLogger(PersonController.class);
    private static final String ENTITY_NAME = "person";

    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    @GetMapping("/_search/people")
    public ResponseEntity<List<PersonDTO>> searchPeople(@RequestParam String query, Pageable pageable) throws URISyntaxException {
        log.info("REST request to search for a page of Appointments for query {} ", query);
        Page<PersonDTO> page = personService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/people");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }
}

回答1:

The answer was quite simple.

I had to do a batch job of querying current entities from a database and saving them to Elastic Search by using

personSearchRepository.save(person);