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