我使用 Spring数据Elasticsearch 来解析 ELasticseach 中的数据。我已经有一个包含alert_sent属性的索引元素( elastalert )。所以我想要做的是返回发送给管理员的所有警报。我尝试在存储库List<Alert> findByAlert_sentTrue()
中定义一个方法,但似乎下划线是一个问题(如文档http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.query-methods.query-property-expressions中所述)。
下面&#39;索引元素的映射:
{
"elastalert_status" : {
"mappings" : {
"elastalert" : {
"properties" : {
"@timestamp" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"aggregate_id" : {
"type" : "string",
"index" : "not_analyzed"
},
"alert_exception" : {
"type" : "string"
},
"alert_info" : {
"properties" : {
"recipients" : {
"type" : "string"
},
"type" : {
"type" : "string"
}
}
},
"alert_sent" : {
"type" : "boolean"
},
"alert_time" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"match_body" : {
"type" : "object",
"enabled" : false
},
"rule_name" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
}
我创建了一个使用该索引元素的实体:
@Document(indexName = "elastalert_status", type = "elastalert")
public class Rule {
@Id
private String id;
private String name;
private String es_host;
private String es_port;
private String index;
private String type;
private String query;
private String TimeStamp;
private String email;
private int runEvery;
private String alertsent;
private String alertTime;
private String matchBody;
...
Getters and Setters
...
使用Curl,它将是
curl -XPOST 'localhost:9200/elastalert_status/elastalert/_search?pretty' -d '
{
"query": { "match": { "alert_sent": true } }
}'
那么如何使用Spring Data Elasticsearch获取所有发送的警报? 感谢。
答案 0 :(得分:0)
我找到了一个解决方案,我开始创建一个扩展ElasticsearchRepository并添加了我的个性化查询的Repository
public interface RuleRepository extends ElasticsearchRepository<Rule,String> {
@Query("{\"bool\": {\"must\": {\"match\": {\"alert_sent\": true}}}}")
List<Rule> findSentAlert();
}
并可视化这些警报只需添加以下代码:
List<Rule> rules = repository.findSentAlert();
System.out.println("Rule list: " + rules);
我希望它可以帮助某人:)