Answer the question
In order to leave comments, you need to log in
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"]}
{
"employees": {
"mappings": {
"properties": {
"first_name": { "type": "keyword" },
"last_name": { "type": "keyword" },
"working_shifts": { "type": "date" }
}
}
}
}
"size": 1,
"body": {
"query": {
"bool": {
"must": [
{ "match": { "first_name": { "query": "Ivan" } } },
{ "match": { "last_name": { "query": "Ivanov" } } }
]
}
},
"fields": [
{
"field": "working_shifts",
}
],
"_source": false
}
{"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"
}
}
}
"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;
"""
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question