Spring Boot + Spring Data JPA + Spring Data ElasticSearch:elastic 没有 return 任何结果

Spring Boot + Spring Data JPA + Spring Data ElasticSearch: elastic doesn't return any results

我不熟悉在我的 Spring 启动应用程序中设置嵌入式 Elasticsearch,其中 Spring Data JPA 是使用 Postgres 数据库设置的。

现在我还添加了对 Elastic Search Spring 数据存储库的支持(或者我是这么认为的)。问题是 ES 搜索没有 return 任何东西(JSON 数组是空的),而 JPA 搜索可以正常工作。

我读到人们需要一个不时运行的索引工具,但我在 Spring Data Elastic Search 文档中找不到与此相关的任何内容。

我的理解是否正确,您需要不断地为数据库中的搜索建立索引?本题提供的答案是否是唯一的解决方案:

这是应用程序 class:

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

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

个人实体 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));
    }
}

个人控制器 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);
    }
}

答案很简单。

我必须使用

从数据库查询当前实体并将它们保存到 Elastic Search 中执行批处理作业
personSearchRepository.save(person);