如何使用elasticsearch API

时间:2015-07-16 15:08:22

标签: java api elasticsearch repository

我在gradle中添加了elasticsearch:

compile("org.springframework.boot:spring-boot-starter-data-elasticsearch:1.2.5.RELEASE")

我创建了一个域类:

@Document(indexName = "opendata_gent", type = "details", shards = 1, replicas = 0)
public class Details {

    @Id
    private String id;

    private String type;

    private Date timestamp;

    private String data;

    // getters and setters
}

使用存储库:

public interface DetailsRepository extends ElasticsearchRepository<Details, String> {

    Details findById(String id);

    List<Details> findByType(String type);

    List<Details> findByTimestampBetween(Date beginDate, Date endDate);

    List<Details> findByTypeAndTimestampBetween(String type, Date beginDate, Date endDate);
}

服务:

@Service
public class DetailsService {

    @Autowired
    public DetailsRepository detailsRepository;

    public Details findById(String id) {
        return detailsRepository.findById(id);
    }

    public List<Details> findByType(String type) {
        return detailsRepository.findByType(type);
    }

    public List<Details> findByTimestampBetween(Date beginDate, Date endDate) {
        return detailsRepository.findByTimestampBetween(beginDate, endDate);
    }

    public List<Details> findByTypeAndTimestampBetween(String type, Date beginDate, Date endDate) {
        return detailsRepository.findByTypeAndTimestampBetween(type, beginDate, endDate);
    }

    public void addDetails(Details details) {
        detailsRepository.save(details);
    }
}

我将此添加到我的application.properties:

elasticsearch.host=localhost
elasticsearch.port=9300

我还需要做些什么才能通过浏览器访问elasticsearch并获取数据?

0 个答案:

没有答案