Elasticsearch快速入门
Elasticsearch可以轻松运行功能齐全的搜索服务器。事实上,它很容易,我将在5分钟内告诉你如何!
安装并运行Elasticsearch
出于本教程的目的,我假设您使用的是Linux或Mac环境。
您还应该安装JDK 6或更高版本。
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.tar.gz
tar -zxvf elasticsearch-1.7.2.tar.gz
cd elasticsearch-1.7.2
bin/elasticsearch
你应该在终端看到这样的东西。
[2015-09-14 15:32:52,278][INFO ][node ] [Big Man]version[1.7.2], pid[10907], build[e43676b/2015-09-14T09:49:53Z]
[2015-09-14 15:32:52,279][INFO ][node ] [Big Man] initializing ...
[2015-09-14 15:32:52,376][INFO ][plugins ] [Big Man] loaded [], sites []
[2015-09-14 15:32:52,426][INFO ][env ] [Big Man] using [1]data paths, mounts [[/ (/dev/sdc1)]], net usable_space [8.7gb], net total_space [219.9gb], types [ext3]
Java HotSpot(TM) Server VM warning: You have loaded library /tmp/es/elasticsearch-1.7.2/lib/sigar/libsigar-x86-linux.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
[2015-09-14 15:32:55,294][INFO ][node ] [Big Man] initialized
[2015-09-14 15:32:55,294][INFO ][node ] [Big Man] starting ...
[2015-09-14 15:32:55,411][INFO ][transport ] [Big Man] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/192.168.43.172:9300]}
[2015-09-14 15:32:55,428][INFO ][discovery ] [Big Man] elasticsearch/VKL1HQmyT_KRtmTGznmQyg
[2015-09-14 15:32:59,210][INFO ][cluster.service ] [Big Man] new_master [Big Man][VKL1HQmyT_KRtmTGznmQyg][Happy][inet[/192.168.43.172:9300]], reason: zen-disco-join (elected_as_master)
[2015-09-14 15:32:59,239][INFO ][http ] [Big Man] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/192.168.43.172:9200]}
[2015-09-14 15:32:59,239][INFO ][node ] [Big Man] started
[2015-09-14 15:32:59,284][INFO ][gateway ] [Big Man] recovered [0] indices into cluster_state
Elasticsearch现在正在运行!您可以在Web浏览器上的http://localhost:9200访问它,它返回以下内容:
curl -H "Content-Type: application/json" -XGET http://192.168.0.99:9200
{
"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"
}
索引数据
我们现在要将一些数据索引到我们的Elasticsearch实例。我们将使用博客引擎的示例,其中包含一些帖子和评论。
curl -H "Content-Type: application/json" -XGET http://192.168.0.99:9200/person/data/_search
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/blog/user/dilbert' -d '{ "name" : "Dilbert Brown" }'
curl -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -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"
}'
对于这些请求中的每一个,您应该已收到验证操作成功的响应,例如:
{"ok":true,"_index":"blog","_type":"post","_id":"1","_version":1}
让我们验证所有操作都是成功的。
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/user/dilbert?pretty=true'
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/1?pretty=true'
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/2?pretty=true'
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/3?pretty=true'
请注意,向Elasticsearch添加数据有两种主要方式:
- json over HTTP
- Native client
我们将在后续教程中更详细地探讨这些内容。
搜索
让我们看看我们是否可以检索我们刚刚通过搜索添加的文档。
查找Dilbert发表的所有博客文章:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/_search?q=user:dilbert&pretty=true'
这将返回以下JSON结果:
{
"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"
}
} ]
}
所有不包含术语搜索的帖子:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/_search?q=-title:search&pretty=true'
检索包含搜索且未分发的所有帖子的标题:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/post/_search?q=+title:search%20-title:distributed&pretty=true&fields=title'
postDate上的范围搜索:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/blog/_search?pretty=true' -d '
{
"query" : {
"range" : {
"postDate" : { "from" : "2011-12-10", "to" : "2011-12-12" }
}
}
}'
您将在单独的教程中了解有关各种URL查询参数的更多信息。
通常的Lucene查询语法可以通过JSON查询语言或通过查询解析器获得。
关掉
要关机Elasticsearch,从那里你推出elasticsearch终端,击中按Ctrl + C。这将彻底关闭ElasticSearch。
Elasticsearch非常强大,因此即使在操作系统或磁盘崩溃的情况下,ElasticSearch的索引也不太可能被破坏。
更多资料
- 查看以下有关Elasticsearch的书籍。
- 了解有关Elasticsearch基本概念的更多信息
转载来源:http://www.elasticsearchtutorial.com/elasticsearch-in-5-minutes.html

229

被折叠的 条评论
为什么被折叠?



