Spring Boot + Spring Data JPA + Spring Data ElasticSearch:elastic 不返回任何结果



我是在我的Spring Boot应用程序中设置嵌入式Elasticsearch的新手,其中Spring Data JPA是使用Postgres数据库设置的。

现在,我还添加了对弹性搜索 Spring 数据存储库的支持(至少我是这么认为的)。问题是 ES 搜索不返回任何内容(JSON 数组为空),而 JPA 搜索工作正常。

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

我是否正确理解您需要不断索引数据库中的搜索?本主题中提供的答案 通过 Spring Data ElasticSearch 将 Spring Data JPA 条目批量索引到 Elastic 是唯一的解决方案:

下面是应用程序类:

@SpringBootApplication
@EnableJpaRepositories(basePackages = "eu.deniss.repository")
@ComponentScan
public class SpringDataElasticsearchDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataElasticsearchDemoApplication.class, args);
}
}

个人实体类:

@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类:

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

PersonServiceImplclass:

@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));
}
}

人员控制器类:

@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);
}
}

答案很简单。

我必须执行从数据库中查询当前实体并将其保存到弹性搜索的批处理工作

personSearchRepository.save(person);

最新更新