通过Spring数据elasticsearch在elasticsearch中搜索特定字段 [英] searching a specific field in elasticsearch through Spring data elasticsearch

查看:108
本文介绍了通过Spring数据elasticsearch在elasticsearch中搜索特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring data Elasticsearch 来解析 ELasticseach 中的数据.我已经有一个包含index_sent属性的索引元素( elastalert ).所以我想做的是返回所有发送给管理员的警报.我尝试在存储库 List< Alert>中定义一种方法.findByAlert_sentTrue(),但下划线似乎是一个问题(如文档

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

这是索引元素的映射:

{
  "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
...

使用Curl将会是

curl -XPOST 'localhost:9200/elastalert_status/elastalert/_search?pretty' -d '
{
  "query": { "match": { "alert_sent": true } }
}'

那么我该如何使用Spring Data Elasticsearch获得所有发送的警报?谢谢.

So how can I get all those sent alerts using Spring Data Elasticsearch ? Thanks.

推荐答案

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

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

这篇关于通过Spring数据elasticsearch在elasticsearch中搜索特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆