Elasticsearch Query Date Range, Фильтр и Сортировка - PullRequest
0 голосов
/ 12 февраля 2020

Я не могу получить желаемый результат, помогите, пожалуйста, My Mapping следующим образом

  "mappings": {

    },
            "properties": {
                "id":    { "type": "keyword"  },
                "summary_date":  {"type":   "date" },
                "trans_date":  {"type":   "date" },
                "trans_hour":  {"type":   "date" }
            }
    }
}'

Я хотел бы найти диапазон дат для набора идентификаторов, и Result должен получить мне trans_date с последним trans_hour.

что я могу получить, это

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "id" : "123",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "06",
        }
      },
      {
        "_source" : {
          "id" : "123",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "04",
        }
      },
      {
        "_source" : {
          "id" : "567",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "06",
        }
      },
      {
        "_source" : {
          "id" : "567",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "04",
        }
      }
    ]
  }

Мне нужно следующее, только один документ на определенную дату с последним trans_hour

  {
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "id" : "123",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "06",
        }
      },
      {
        "_source" : {
          "id" : "567",
          "summary_date" : "2019-09-11",
          "trans_date" : "20190911",
          "trans_hour" : "06",
        }
      },

    ]
  }

  I am able to get all the documents based on date range and id filter 

  { 
   "query": {
    "bool": {
      "must": [
        {
          "range": {
            "summary_date": {
              "gte": "2019-09-11",
              "lte": "2019-09-15"
            }
          }
        }
      ],
            "filter" : [{ "terms": {"msisdn_numb_id": ["123", "567"]} }]

    }
  }
 }'

 However, I am not able to form the query to get the desired result, I tried the following

 {
   "query": {
    "bool": {
      "must": [
        {
          "range": {
            "summary_date": {
              "gte": "2019-09-11",
              "lte": "2019-09-11"
            }
          }
        }
      ]
    }
  },
"aggs": {
    "field1_unique": {
      "terms": {
        "field": "trans_date"
      },
      "aggs": {
        "first_one": {
          "top_hits": {
            "size": 1,
            "sort": [{"trans_hour": {"order":"desc"}}]
          }
        }
      }
    }
  }
}'

Пожалуйста, помогите

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...