简体   繁体   English

Spring Boot + Spring Data JPA + Spring Data ElasticSearch:弹性不会返回任何结果

[英]Spring Boot + Spring Data JPA + Spring Data ElasticSearch: elastic doesn't return any results

I am new to setting up embedded Elasticsearch into my Spring Boot application where Spring Data JPA is setup with Postgres database. 我是新手,在我的Spring Boot应用程序中设置嵌入式Elasticsearch,其中Spring Data JPA是使用Postgres数据库设置的。

Now I've also added support for Elastic Search Spring Data repositories (or so I thought). 现在我还添加了对弹性搜索Spring Data存储库的支持(或者我认为)。 The problem is that ES searches do not return anything (JSON array is empty), whilst JPA ones work correctly. 问题是ES搜索没有返回任何内容(JSON数组为空),而JPA搜索工作正常。

I read that people need an index tool that runs every now and then, but I couldn't find anything related to this in the Spring Data Elastic Search documents. 我读到人们需要一个不时运行的索引工具,但我在Spring Data Elastic Search文档中找不到与此相关的任何内容。

Do I understand correctly that you need to index the searches from the database constantly? 我是否正确理解您需要不断地从数据库中索引搜索? Is the answer provided in this topic Batch indexing Spring Data JPA entries to Elastic through Spring Data ElasticSearch the only solution: 是否在本主题中提供了答案通过Spring Data ElasticSearchSpring Data JPA条目批量索引到Elastic是唯一的解决方案:

Here is the Application class: 这是Application类:

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

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

Person Entity class: 人实体类:

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

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

PersonServiceImpl class: 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 class: 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);
    }
}

The answer was quite simple. 答案很简单。

I had to do a batch job of querying current entities from a database and saving them to Elastic Search by using 我不得不做一个从数据库查询当前实体并通过使用将它们保存到Elastic Search的批处理工作

personSearchRepository.save(person);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM