V
V
Vayladion Gognazdiak2022-02-05 12:53:30
elasticsearch
Vayladion Gognazdiak, 2022-02-05 12:53:30

How to filter field values ​​in output results in ElasticSeatch?

Good day.

A few explanations.
There is an index containing the fields first_name, last_name, working_shifts - first name, last name and dates of work shifts, respectively.
Each entry contains a unique first_name + last_name pair

There is an entry in the index

{"first_name": "Ivan", "last_name": "Ivanov", "workings_shifts": ["2022-02-01", "2022-02-04", "2022-02-05"]}


How can I get working_shifts according to Ivanov Ivan, for a certain period?
For example for "gt": "2022-01-20", "lt": "2022-02-04" ?

Index:
{
  "employees": {
    "mappings": {
      "properties": {
        "first_name": { "type": "keyword" },
        "last_name": { "type": "keyword" },
        "working_shifts": { "type": "date" }
      }
    }
  }
}


I get a record for Ivan Ivanov
"size": 1,
  "body": {
    "query": {
      "bool": {
        "must": [
          { "match": { "first_name": { "query": "Ivan" } } },
          { "match": { "last_name": { "query": "Ivanov" } } }
        ]
      }
    },
    "fields": [
      {
        "field": "working_shifts",
      }
    ],
    "_source": false
  }


And how to apply the range indicated above to this single result? So that only: Reading the documentation in the Docvalue fields / Stored fields part did not give any results ;( post_filter removes/leaves the entire record,
{"gt": "2022-01-20", "lt": "2022-02-04"}
{"working_shifts": ["2022-02-01",]}

"post_filter": {
     "range":{
        "working_shifts":{
            "gte": "2022-01-01",
            "lte": "2022-02-03"
        }
      }
}


Thanks in advance for your reply.

UPD:
Actually I did it like this, but for sure there is an easier way
"script_fields": {
      "Days": {
        "script": {
          "inline": """
          def fromTime = 1645218000;
          def days = [];
          for ( int i = 0; i < params['_source'].working_shifts.size(); i++ ) {
            if (params['_source'].working_shifts[i] > fromTime) {
              Date date = new java.util.Date(params['_source'].working_shifts[i]*1000L);
              SimpleDateFormat sdf = new java.text.SimpleDateFormat('yyyy-MM-dd');
              String formattedDate = sdf.format(date);
              days.add(formattedDate);
            }
          }
          return days;
          """
        }
      }
    }

Dates are specifically given to unixtime, because it seems impossible to compare dates inside elastic:
Cannot apply [>] operation to types [java.util.Date] and [java.util.Date]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question