help me to write the correct filter query Here I used nested mapping and I trying to filter metric_id =1 is not working correctly #this is my mapping
"mappings": {
"_doc": {
"properties": {
"metrics": {
"type": "nested"
}
}
}
}
json data
{
'process_date': '2022-03-23T08:16:48.626Z',
'metrics': [
{
'date': '2022-02-23',
'metric_id': 1,
'metric_name': 'Total unique_user',
'timeframe_type': 'D',
'users_count': 2
},
{
'date': '2022-02-25',
'metric_id': 1,
'metric_name': 'Total unique_user',
'timeframe_type': 'D',
'users_count': 1
},
{
"date": "2022-02-01",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "M",
"users_count": 2
},
{
"date": "2022-02-21",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "W",
"users_count": 2
},
{
"date": "2022-01-01",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "Y",
"users_count": 2
}{
'date': '2022-02-23',
'metric_id': 2,
'metric_name': 'Total unique_course',
'timeframe_type': 'D',
'course_count': 1
},
{
'date': '2022-02-24',
'metric_id': 2,
'metric_name': 'Total unique_course',
'timeframe_type': 'D',
'course_count': 0
}
]
}
I'm tried filter queries metric_id =1 but got all the metric_id documents
GET metrics_test/_search
{
"query": {"nested": {
"path": "metrics",
"query": {"bool": {"filter": {"term": {
"metrics.metric_id": 1
}}}}
}}
}
GET metrics_test/_search
{
"query": {"nested": {
"path": "metrics",
"query": {"bool": {"must": [
{"match": {
"metrics.metric_id": 1
}}
]}
}}
}}
timeframe_type filter this is working fine
GET metrics_test/_search
{
"query": {
"nested": {
"path": "metrics",
"query": {
"bool": {
"filter": {"term": {
"metrics.timeframe_type.keyword":"Y"
}}
}
}
}
}
}
Expected Output : 1st filter : metric_id =1
{
'process_date': '2022-03-23T08:16:48.626Z',
'metrics': [
{
'date': '2022-02-23',
'metric_id': 1,
'metric_name': 'Total unique_user',
'timeframe_type': 'D',
'users_count': 2
},
{
'date': '2022-02-25',
'metric_id': 1,
'metric_name': 'Total unique_user',
'timeframe_type': 'D',
'users_count': 1
},
{
"date": "2022-02-01",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "M",
"users_count": 2
},
{
"date": "2022-02-21",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "W",
"users_count": 2
},
{
"date": "2022-01-01",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "Y",
"users_count": 2
}]}
2nd filter : metric_id =1 and timeframe_type='Y'
{'process_date': '2022-03-23T08:16:48.626Z',
'metrics': [{
"date": "2022-01-01",
"metric_id": 1,
"metric_name": "Total unique_user",
"timeframe_type": "Y",
"users_count": 2
}]}
3rd filter : metric_id =1 and timeframe_type='Y' and date range 2022-02-23 to 2022-02-30