gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:32:39 25 4
gpt4 key购买 nike

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

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

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

我是否正确理解您需要不断地为数据库中的搜索建立索引?本题提供的答案是Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch唯一的解决办法:

这是Application类:

@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> {
}

PersonServiceImpl 类:

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

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

关于java - Spring Boot + Spring Data JPA + Spring Data ElasticSearch : elastic doesn't return any results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com