외부 쿼리에 값을 전달하는 방법

외부 쿼리에 값을 전달하는 방법

아래에 비슷한 로그가 있습니다.

나는 더미를 만들고 아래와 같이 index만들었습니다 .mappingdev-tools

PUT new
{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type":   "date",
        "format": "yyyy-MM-dd HH:mm:ss.SSS"
      }
    }
  }
}

그리고 indexed아래와 같은 데이터,

PUT /new/_doc/1
{
  "@timestamp": "2021-11-05 08:12:14.534",
  "level": "INFO",
  "id": "1",
  "text": "website is accessed",
  "status": "clicked"
}

PUT /new/_doc/2
{
  "@timestamp": "2021-10-14 09:11:14.534",
  "level": "INFO",
  "id": "3",
  "text": "website is accessed",
  "status": "clicked"
}

PUT /new/_doc/3
{
  "@timestamp": "2021-09-09 02:08:20.534",
  "level": "INFO",
  "id": "4",
  "text": "website is accessed",
  "status": "clicked"
}

아래 쿼리를 사용하여 총 개수를 가져올 수 있습니다 request.

GET new/_search
{
  "aggs": {},
  "size": 0,
  "fields": [],
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "text": "website is accessed"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2021-10-01",
              "lte": "2021-10-30"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}

아래와 같이 얻고 response,

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

보시다시피 hardcode특정 date항목에 대한 값을 가져와야 합니다. 즉, 한 달 month동안 동일한 정보를 가져오려면 컬 요청에서 아래와 같이 sept수정해야 합니다 .date time range

"range": {
  "@timestamp": {
    "gte": "2021-09-01",
    "lte": "2021-09-30"
    }
    }

아래는 curl call request.

curl -u elastic:xxx  -XGET "http://10.10.10.10:9200/new/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "aggs": {},
  "size": 0,
  "fields": [],
  "query": {
    "bool": {
      "must": [],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "text": "website is accessed"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "2021-10-01",
              "lte": "2021-10-30"
            }
          }
        }
      ],
      "should": [],
      "must_not": []
    }
  }
}'

특정 에 대한 정보를 가져오는 컬 요청에 동적으로(즉, 실제로 요청 자체를 하드코딩하지 않고) year전달 하려면 어떻게 해야 합니까 ?monthmonthyear


업데이트 -

아래를 사용하여 지난 달(11월) 또는 지난 2개월(10월) 등의 결과를 얻을 수 있습니다.

지난 달 - Nov-

"gte": "now-M",
"lt": "now/M"

2 개월 -Oct

"gte": "now-2M/M",
"lte": "now-2M/M"

year그러나 원하는 것을 제공 하고 month결과를 검색할 수 있는 방법이 있습니까 ?

감사해요,

답변1

당신이 사용할 수있는데이트 수학완전히 정의된 날짜:

"range": {
  "@timestamp": {
    "gte": "2021-10-01",
    "lte": "2021-10-01||+1M/d"
  }
}

관련 정보