Skip to content

Instantly share code, notes, and snippets.

@jobliz
Created July 28, 2018 15:02
Show Gist options
  • Save jobliz/4d32b0513103f1b75d515f9acadfedd3 to your computer and use it in GitHub Desktop.
Save jobliz/4d32b0513103f1b75d515f9acadfedd3 to your computer and use it in GitHub Desktop.
Tag Search with ElasticSearch 6

Tag Search with ElasticSearch 6

This is a modified version of this tutorial, the queries have been modified so that they work with ES6. Many backwards incompatible changes happened between previous verions and 6, so many how-to's on the internet are outdated. If you're just starting learning ElasticSearch with version 6 then you should read these links and keep a mental note of them.

  • CURL syntax with ElasticSearch: CURL calls need to be done in a certain manner for them to work correctly. There are situations in which ElasticSearch does not complain but the search or query is not processed correctly. Check your CURL call if it ever complains about globbing.

  • Removal of the string type: Per the blog: The string field has split into two new types: text, which should be used for full-text search, and keyword, which should be used for keyword search.

  • Removal of mapping types: The developers have good technical arguments, for this, so if you're just learning with ElasticSearch 6 there's nothing you can do but adapt.

  • Keyword type and not_analyzed: The "keyword" type does not seem to need the "not_analyzed" parameter.

  • 'no query registered for filtered' error The filtered query has been deprecated and removed in ES 5.0. You should now use the bool/must/filter query instead.

Now with all this and the Query DSL in mind, let's create the index:

curl -H 'Content-Type: application/json' -X PUT 'http://localhost:9200/movies/' -d '
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "movie": {
            "properties": {
                "director": {
                    "type": "text"
                },
                "genres": {
                    "type": "keyword"
                },
                "title": {
                    "type": "text"
                },
                "year": {
                    "type": "long"
                }
            }
        }
    }
}'

You can delete the index with:

curl -XDELETE 'http://localhost:9200/movies/'

Posting documents to the index:

curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/movies/movie' -d '
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War", "Foo"]
}'
curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/movies/movie' -d '
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War", "Foo", "Bar"]
}'
curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/movies/movie' -d '
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "Bar"]
}'

Let's see what got in. This should produce the three items we just posted.

curl -XGET 'localhost:9200/movies/movie/_search?pretty' '{}'

The first tag search:

GET movies/movie/_search

curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/movies/movie/_search?pretty' -d '
{
    "query": {
        "bool": {
            "filter": {
                "terms": {
                    "genres": ["War", "Foo"]
                }
            }
        }
    }
}'

The second tag search, excluding one tag:

curl -H 'Content-Type: application/json' -XPOST 'localhost:9200/movies/movie/_search?pretty' -d '
{
    "query": {
        "bool": {
            "must": [
                { "match": { "genres": "War" } },
                { "match": { "genres": "Foo" } },
                { "match": { "genres": "Drama" } }
            ],
            "must_not": [
                { "match": { "genres": "Bar" } }
            ]
        }
    }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment