问题
I'm using Spring data Elasticsearch to parse data in ELasticseach. I have already there an indexed element (elastalert) witch contains the alert_sent property. So what i want to do is returning all alerts that were sent to the admin. I tried defining a method in the Repository List<Alert> findByAlert_sentTrue()
but it seems that the underscore is a problem (as mentioned in the documentation http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.query-methods.query-property-expressions).
Here' the mapping of the indexed element:
{
"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"
}
}
}
}
}
}
I created an entity to use that indexed element:
@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
...
With Curl it would be
curl -XPOST 'localhost:9200/elastalert_status/elastalert/_search?pretty' -d '
{
"query": { "match": { "alert_sent": true } }
}'
So how can I get all those sent alerts using Spring Data Elasticsearch ? Thanks.
回答1:
I found a solution for this, I started by creating a Repository which extends the ElasticsearchRepository and added my personnalized query
public interface RuleRepository extends ElasticsearchRepository<Rule,String> {
@Query("{\"bool\": {\"must\": {\"match\": {\"alert_sent\": true}}}}")
List<Rule> findSentAlert();
}
and to visualize those alerts just add this piece of code:
List<Rule> rules = repository.findSentAlert();
System.out.println("Rule list: " + rules);
I hope it could help someone :)
来源:https://stackoverflow.com/questions/37171124/searching-a-specific-field-in-elasticsearch-through-spring-data-elasticsearch