Skip to content

Instantly share code, notes, and snippets.

@cuibonobo
Created July 2, 2014 21:43
Show Gist options
  • Select an option

  • Save cuibonobo/310bbd7d0683335923f0 to your computer and use it in GitHub Desktop.

Select an option

Save cuibonobo/310bbd7d0683335923f0 to your computer and use it in GitHub Desktop.
How to get an exact match for a field in Elasticsearch.

This method is extremely slow, but if you've tokenized your fields in a certain way and don't want to re-index them to include exact matches, this is a good way to go.

{
    "query" : {
        "filtered" : {
            "query" : {
                "match_all": {}
            },
            "filter" : {
                "script" : {
                    "script" : "_source.name == filename",
                    "params" : {
                        "filename" : "exact_match_value.txt"
                    }
                }
            }
        }
    }
}

What's happening here is that we've included a query to match everything, and then we're filtering the query based on the script. The field that I was looking for did't appear in the actual Elasticsearch document, but it was in a field in the _source, which is why we're using that here. The keys in params can be whatever you want, as long as they appear in the value of the script field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment