Elasticsearch 101

Preview:

DESCRIPTION

Elasticsearch is a highly scalable open-source full-text search and analytics engine. A cluster is a collection of one or more nodes (servers) that together holds your entire data and provides federated indexing and search capabilities across all nodes. A node is a single server that is part of your cluster, stores your data, and participates in the cluster’s indexing and search capabilities. An index is a collection of documents that have somewhat similar characteristics. A document is a basic unit of information that can be indexed. A type is a logical category/partition of your index whose semantics is completely up to you. Elasticsearch provides the ability to subdivide your index into multiple pieces called shards. Elasticsearch allows you to make one or more copies of your index’s shards into what are called replica shards, or replicas for short. installation Java http://www.elasticsearch.org/download/ http://localhost:9200/ { "ok": true, "status": 200, "name": "Red Lotus", "version": { "number": "0.90.13", "build_hash": "249c9c5e06765c9e929e92b1d235e1ba4dc679fa", "build_timestamp": "2014-03-25T15:27:12Z", "build_snapshot": false, "lucene_version": "4.6" }, "tagline": "You Know, for Search" } http://localhost:9200/_plugin/head/ Create an index named 'movies_1.0.0' curl -XPUT 'http://localhost:9200/movies_1.0.0' a concise list of all indices and their aliases in your cluster curl http://localhost:9200/_aliases Add an index alias curl -XPOST 'http://localhost:9200/_aliases' -d ' { "actions" : [ { "add" : { "index" : "movies_1.0.0", "alias" : "movies" } } ] }' Create a type called 'movie' curl -XPUT 'http://localhost:9200/movies/movie/_mapping' -d '{ "movie":{ "properties":{ "director":{ "type":"string" }, "genres":{ "type":"string" }, "title":{ "type":"string" }, "year":{ "type":"long" } } } }' get mapping of a type curl -XGET 'http://localhost:9200/movies/movie/_mapping' Create a document curl -XPUT "http://localhost:9200/movies/movie/1" -d' { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972, "genres": ["Adventure"] }' Retrieve the document curl -XGET "http://localhost:9200/movies/movie/1" more data... curl -XPUT "http://localhost:9200/movies/movie/1" -d' { "title": "The Godfather", "director": "Francis Ford Coppola", "year": 1972, "genres": ["Crime", "Drama"] }' curl -XPUT "http://localhost:9200/movies/movie/2" -d' { "title": "Lawrence of Arabia", "director": "David Lean", "year": 1962, "genres": ["Adventure", "Biography", "Drama"] }' curl -XPUT "http://localhost:9200/movies/movie/3" -d' { "title": "To Kill a Mockingbi

Citation preview

(the VERY basics of it)

Elasticsearch is a highly scalable open-source full-text search and analytics engine.

● A cluster is a collection of one or more nodes (servers) that together holds your entire data and provides federated indexing and search capabilities across all nodes.

● A node is a single server that is part of your cluster, stores your data, and participates in the cluster’s indexing and search capabilities.

● An index is a collection of documents that have somewhat similar characteristics.

● A document is a basic unit of information that can be indexed.

● A type is a logical category/partition of your index whose semantics is completely up to you.

cluster

node

index

type

type

document

document

document

document

● Elasticsearch provides the ability to subdivide your index into multiple pieces called shards.

clusternode

Index: shard 1type

document

document

Index: shard 2type

document

document

● Elasticsearch allows you to make one or more copies of your index’s shards into what are called replica shards, or replicas for short.

clusternode

Index: shard 1type

document

document

Index: shard 2type

document

document

Index: shard 1: replicatype

document

document

Index: shard 2: replicatype

document

document

installation

● Java

● http://www.elasticsearch.org/download/

http://localhost:9200/{

"ok": true,

"status": 200,

"name": "Red Lotus",

"version": {

"number": "0.90.13",

"build_hash": "249c9c5e06765c9e929e92b1d235e1ba4dc679fa",

"build_timestamp": "2014-03-25T15:27:12Z",

"build_snapshot": false,

"lucene_version": "4.6"

},

"tagline": "You Know, for Search"

}

http://localhost:9200/_plugin/head/

Create an index named 'movies_1.0.0'

curl ­XPUT 'http://localhost:9200/movies_1.0.0'

a concise list of all indices and their aliases in your cluster

curl http://localhost:9200/_aliases

Add an index alias

curl ­XPOST 'http://localhost:9200/_aliases' ­d '

{

    "actions" : [

        {

           "add" : {

              "index" : "movies_1.0.0", 

              "alias" : "movies" 

           } 

        }

    ]

}'

Create a type called 'movie'curl -XPUT 'http://localhost:9200/movies/movie/_mapping' -d '{

"movie":{

"properties":{

"director":{

"type":"string"

},

"genres":{

"type":"string"

},

"title":{

"type":"string"

},

"year":{

"type":"long"

}

}

}

}'

get mapping of a type

curl ­XGET 'http://localhost:9200/movies/movie/_mapping'

Create a document

curl ­XPUT "http://localhost:9200/movies/movie/1" ­d'

{

    "title": "The Godfather",

    "director": "Francis Ford Coppola",

    "year": 1972,

    "genres": ["Adventure"]

}'

Retrieve the document

curl ­XGET "http://localhost:9200/movies/movie/1" 

more data...curl ­XPUT "http://localhost:9200/movies/movie/1" ­d'

{

    "title": "The Godfather",

    "director": "Francis Ford Coppola",

    "year": 1972,

    "genres": ["Crime", "Drama"]

}'

curl ­XPUT "http://localhost:9200/movies/movie/2" ­d'

{

    "title": "Lawrence of Arabia",

    "director": "David Lean",

    "year": 1962,

    "genres": ["Adventure", "Biography", "Drama"]

}'

curl ­XPUT "http://localhost:9200/movies/movie/3" ­d'

{

    "title": "To Kill a Mockingbird",

    "director": "Robert Mulligan",

    "year": 1962,

    "genres": ["Crime", "Drama", "Mystery"]

}'

curl ­XPUT "http://localhost:9200/movies/movie/4" ­d'

{

    "title": "Apocalypse Now",

    "director": "Francis Ford Coppola",

    "year": 1979,

    "genres": ["Drama", "War"]

}'

curl ­XPUT "http://localhost:9200/movies/movie/5" ­d'

{

    "title": "Kill Bill: Vol. 1",

    "director": "Quentin Tarantino",

    "year": 2003,

    "genres": ["Action", "Crime", "Thriller"]

}'

curl ­XPUT "http://localhost:9200/movies/movie/6" ­d'

{

    "title": "The Assassination of Jesse James by the Coward Robert Ford",

    "director": "Andrew Dominik",

    "year": 2007,

    "genres": ["Biography", "Crime", "Drama"]

}'

Querying

Show all

 curl ­XGET 'http://localhost:9200/movies/_search' ­d '{

    "query": {

        "match_all": {}

    }

}'

Search request bodycurl ­XPOST "http://localhost:9200/_search" ­d'

{

    "query": {

        "query_string": {

            "query": "kill"         

        }

    }

}'

Specifying fields to search in

curl ­XPOST "http://localhost:9200/_search" ­d'

{

    "query": {

        "query_string": {

            "query": "ford",

            "fields": ["title"]

        }

    }

}'

Filtering

Filtering sample 1curl ­XPOST "http://localhost:9200/_search" ­d'

{

    "query": {

        "filtered": {

            "query": {

                "match_all": {

                }

            },

            "filter": {

                "term": { "year": 1962 }

            }

        }

    }

}'

Filtering sample 2curl ­XPOST "http://localhost:9200/_search" ­d'

{

    "query": {

        "filtered": {

            "query": {

                "query_string": {

                    "query": "drama"

                }

            },

            "filter": {

                "term": { "year": 1962 }

            }

        }

    }

}'

references

http://joelabrahamsson.com/elasticsearch­101/

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_basic_concepts.

html