通过 Spring 数据 elasticsearch 在 elasticsearch 中搜索特定字段

searching a specific field in elasticsearch through Spring data elasticsearch

我正在使用 Spring 数据 Elasticsearch 来解析 ELasticseach 中的数据。我已经有一个索引元素 (elastalert) 包含 alert_sent 属性。所以我想做的是返回发送给管理员的所有警报。我尝试在存储库 List<Alert> findByAlert_sentTrue() 中定义一个方法,但似乎下划线是个问题(如文档 http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.query-methods.query-property-expressions 中所述)。

这里'索引元素的映射:

{
  "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 获取所有已发送的警报? 谢谢。

我找到了一个解决方案,我首先创建了一个存储库,它扩展了 ElasticsearchRepository 并添加了我的个性化查询

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

希望对大家有所帮助:)