【发布时间】:2017-08-29 07:28:57
【问题描述】:
我是新手,将嵌入式 Elasticsearch 设置到我的 Spring Boot 应用程序中,其中 Spring Data JPA 是使用 Postgres 数据库设置的。
现在我还添加了对 Elastic Search Spring Data 存储库的支持(至少我是这么认为的)。问题是 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);
}
}
【问题讨论】:
-
如果您不将任何内容保存到弹性文件中,则任何内容都不会被索引。当您在数据库中存储某些内容时,添加
@Document不会自动将其保存到弹性中。 -
@M.Deinum 那么我将不得不做一个批处理工作,只需读取我所有的数据库数据并将其保存到 Elastic Search 中?
标签: java spring-boot spring-data-jpa spring-data-elasticsearch