Linux Mac

ElasticSearch za 5 minúť

ElasticSearch uľahčuje prevádzkovať plnohodnotný vyhľadávací server. V skutočnosti je to tak jednoduché, ukážem Vám to za 5 minút!

1. Inštalácia a spustenie ElasticSearch

Na účely tohto tutoriálu, predpokladám že používate prostredie Linux alebo Mac. Tiež by ste mali mat JDK 6 alebo vyššiu verzia.

wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.1.0.tar.gz
tar -zxvf elasticsearch-2.1.0.tar.gz
cd elasticsearch-2.1.0
bin/elasticsearch

Mali by ste vidieť niečo také v termináli.

[2015-12-07 19:09:17,167][INFO ][node ] [Grand Director] version[2.1.0], pid[9069], build[72cd1f1/2015-11-18T22:40:03Z]
[2015-12-07 19:09:17,168][INFO ][node ] [Grand Director] initializing ...
[2015-12-07 19:09:17,283][INFO ][plugins ] [Grand Director] loaded [], sites []
[2015-12-07 19:09:17,328][INFO ][env ] [Grand Director] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [78.2gb], net total_space [225.4gb], spins? [no], types [ext4]
[2015-12-07 19:09:19,751][INFO ][node ] [Grand Director] initialized
[2015-12-07 19:09:19,751][INFO ][node ] [Grand Director] starting ...
[2015-12-07 19:09:19,867][INFO ][transport ] [Grand Director] publish_address {127.0.0.1:9300}, bound_addresses {127.0.0.1:9300}, {[::1]:9300}
[2015-12-07 19:09:19,878][INFO ][discovery ] [Grand Director] elasticsearch/l4AFrTa5Tgi6ntJnZ69_uA
[2015-12-07 19:09:22,959][INFO ][cluster.service ] [Grand Director] new_master {Grand Director}{l4AFrTa5Tgi6ntJnZ69_uA}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2015-12-07 19:09:22,981][INFO ][http ] [Grand Director] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}, {[::1]:9200}
[2015-12-07 19:09:22,982][INFO ][node ] [Grand Director] started
[2015-12-07 19:09:23,032][INFO ][gateway ] [Grand Director] recovered [0] indices into cluster_state
[2015-12-07 19:10:30,582][INFO ][node ] [Grand Director] stopping ...
[2015-12-07 19:10:30,606][INFO ][node ] [Grand Director] stopped
[2015-12-07 19:10:30,607][INFO ][node ] [Grand Director] closing ...
[2015-12-07 19:10:30,618][INFO ][node ] [Grand Director] closed

ElasticSearch teraz beží! Máte k nemu prístup na adrese http://localhost:9200 na svojom webovom prehliadači, ktorý vracia toto:

{
"status" : 200,
"name" : "Big Man",
"cluster_name" : "elasticsearch",
"version" : {
"number" : "1.7.2",
"build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
"build_timestamp" : "2015-09-14T09:49:53Z",
"build_snapshot" : false,
"lucene_version" : "4.10.4"
},
"tagline" : "You Know, for Search"
}

2. Indexovanie dát

Teraz budeme indexovať niektoré údaje na našej inštancie ElasticSearchu.

curl -XPUT 'http://localhost:9200/blog/user/dilbert' -d '{ "name" : "Dilbert Brown" }'

curl -XPUT 'http://localhost:9200/blog/post/1' -d '
{
    "user": "dilbert",
    "postDate": "2011-12-15",
    "body": "Search is hard. Search should be easy." ,
    "title": "On search"
}'

curl -XPUT 'http://localhost:9200/blog/post/2' -d '
{
    "user": "dilbert",
    "postDate": "2011-12-12",
    "body": "Distribution is hard. Distribution should be easy." ,
    "title": "On distributed search"
}'

curl -XPUT 'http://localhost:9200/blog/post/3' -d '
{
    "user": "dilbert",
    "postDate": "2011-12-10",
    "body": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat" ,
    "title": "Lorem ipsum"
}'

Uvidíme, či sa nám podarí získať dokumenty ktoré sme práve pridali.
Poďme overiť, že všetky operácie boli úspešné.

curl -XGET 'http://localhost:9200/blog/user/dilbert?pretty=true'
curl -XGET 'http://localhost:9200/blog/post/1?pretty=true'
curl -XGET 'http://localhost:9200/blog/post/2?pretty=true'
curl -XGET 'http://localhost:9200/blog/post/3?pretty=true'

3. Vyhladávanie

Nájsť všetky príspevky od užívateľa Dilbert:

'http://localhost:9200/blog/post/_search?q=user:dilbert&pretty=true'

JSON výsledok:

{
"took" : 85,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [ {
"_index" : "blog",
"_type" : "post",
"_id" : "1",
"_score" : 1.0, "_source" :
{
"user": "dilbert",
"postDate": "2011-12-15",
"body": "Search is hard. Search should be easy." ,
"title": "On search"
}
}, {
"_index" : "blog",
"_type" : "post",
"_id" : "2",
"_score" : 0.30685282, "_source" :
{
"user": "dilbert",
"postDate": "2011-12-12",
"body": "Distribution is hard. Distribution should be easy." ,
"title": "On distributed search"
}
}, {
"_index" : "blog",
"_type" : "post",
"_id" : "3",
"_score" : 0.30685282, "_source" :
{
"user": "dilbert",
"postDate": "2011-12-10",
"body": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat" ,
"title": "Lorem ipsum"
}
} ]
}

Všetky príspevky, ktoré neobsahujú termín hľadania:

curl 'http://localhost:9200/blog/post/_search?q=-title:search&pretty=true'

Načítať názov všetkých príspevkov ktoré obsahujú vyhľadávanie a nie sú distribuované:

curl 'http://localhost:9200/blog/post/_search?q=+title:search%20-title:distributed&pretty=true&fields=title'

Vyhľadávanie s časovým intervalom:

curl -XGET 'http://localhost:9200/blog/_search?pretty=true' -d '
{
"query" : {
"range" : {
"postDate" : { "from" : "2011-12-10", "to" : "2011-12-12" }
}
}
}'

4. Ako vypnút ElasticSearch
CTRL+D

zdroj: TS FMG.sk

Loading Facebook Comments ...