0

我是新来的嵌入式Elasticsearch设置成我的春节,启动应用程序,其中春季数据JPA是设置与Postgres数据库。春季启动+弹簧数据JPA +弹簧数据ElasticSearch:弹性不返回任何结果

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

我读,人们需要的是现在,然后运行,每一个指数的工具,但我找不到在春节弹性数据检索的文献与此相关的任何东西。

难道我理解正确的,你需要建立索引的搜索从不断的数据库?本主题Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch唯一的解决方案中提供了答案:

这里是应用类:

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

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

Person实体类:

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

做从数据库中查询当前实体并将其保存到弹性搜索的批处理作业如果你不保存任何弹性什么都不会被索引。在数据库中存储某些内容时,添加@ @ Document不会自动将其保存为弹性。 –

+0

@ M.Deinum,那么我将不得不做一个批处理作业,只读取我所有的数据库数据并将其保存到Elastic Search中? –

回答

1

的答案是很简单的。

我不得不用

personSearchRepository.save(person);