Hitesh Dholaria
Hitesh Dholaria

Reputation: 157

How to filter ElasticSearch results basis the field value?

Below is my example search response with 4 results retrieved.

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0.41753215,
        "hits": [
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/shared/Report_google_Shared",
                "_score": 0.41753215,
                "fields": {
                    "lastModified": [
                        1461738428007
                    ],
                    "dir": [
                        "/shared"
                    ],
                    "filename": [
                        "Report_google_Shared"
                    ]
                },
                "highlight": {
                    "filename": [
                        "Report_google_Shared"
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/shared/Report_Gmail_Shared",
                "_score": 0.41753215,
                "fields": {
                    "lastModified": [
                        1461738618676
                    ],
                    "dir": [
                        "/shared"
                    ],
                    "filename": [
                        "Report_Gmail_Shared"
                    ]
                },
                "highlight": {
                    "filename": [
                        "Report_Gmail_Shared"
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/private/hitesh/Report_Gmail_Private",
                "_score": 0.1883173,
                "fields": {
                    "lastModified": [
                        1461738629888
                    ],
                    "dir": [
                        "/private/hitesh"
                    ],
                    "filename": [
                        "Report_Gmail_Private"
                    ]
                },
                "highlight": {
                    "filename": [
                        "Report_Gmail_Private"
                    ]
                }
            },
            {
                "_index": "google_a804f89b-d32e-426a-a79a-ea83d65c98ea",
                "_type": "viz_dashlet",
                "_id": "/private/dholaria/Report_google_Private",
                "_score": 0.1883173,
                "fields": {
                    "lastModified": [
                        1461738451720
                    ],
                    "dir": [
                        "/private/dholaria"
                    ],
                    "filename": [
                        "Report_google_Private"
                    ]
                },
                "highlight": {
                    "filename": [
                        "Report_google_Private"
                    ]
                }
            }
        ]
    }
}

Now, I want to filter the above search results basis the specific "dir" field value as per the below criteria.

Include the search result in the response if and only if:

How can I achieve the above functionality in ElasticSearch?

PS: Below is my example mapping.


    {
        "google_a804f89b-d32e-426a-a79a-ea83d65c98ea": {
            "mappings": {
                "viz_dashlet": {
                    "properties": {
                        "charts": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "columnLabels": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "columnNames": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "creator": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "dimensions": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "dir": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "expressions": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "filename": {
                            "type": "string",
                            "index_analyzer": "whitespace_index",
                            "search_analyzer": "whitespace_search",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "lastModified": {
                            "type": "date",
                            "format": "date_hour_minute_second"
                        },
                        "measures": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        },
                        "promptFilters": {
                            "type": "string",
                            "index_analyzer": "report_index_analyzer",
                            "search_analyzer": "report_search_analyzer",
                            "fields": {
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                            }
                        }
                    }
                }
            }
        }
    }

Upvotes: 2

Views: 392

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Try this query:

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "dir.raw": {
              "value": "/shared"
            }
          }
        },
        {
          "term": {
            "dir.raw": {
              "value": "/private/hitesh"
            }
          }
        },
        {
          "match_phrase_prefix": {
            "dir.raw": "/shared"
          }
        },
        {
          "match_phrase_prefix": {
            "dir.raw": "/private/hitesh"
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions