Elasticsearch快速入门

 

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添加数据有两种主要方式:

  1. json over HTTP
  2. 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的索引也不太可能被破坏。

 

更多资料

  1. 查看以下有关Elasticsearch的书籍。
  2. 了解有关Elasticsearch基本概念的更多信息

 

转载来源:http://www.elasticsearchtutorial.com/elasticsearch-in-5-minutes.html

Elasticsearch是一款非常强大的开源搜索引擎,它可以帮助我们从海量数据中快速找到需要的内容。它是elastic stack(ELK)的核心组件之一,结合kibana、Logstash、Beats等工具,被广泛应用在日志数据分析、实时监控等领域。\[1\] 在Elasticsearch中,为了提高搜索的性能,它使用了倒排索引的概念。倒排索引是指将文档中的每个词都建立索引,然后通过这些索引来快速定位到包含该词的文档。当我们向Elasticsearch插入一条记录时,它会将记录中的字段建立倒排索引,以便能够快速搜索和检索数据。\[2\] 为了能够快速找到某个词,Elasticsearch使用了Term Dictionary。Term Dictionary将所有的词进行排序,并使用二分法查找词,类似于我们在学校时老师教我们使用字典的方式。这种查询方式类似于传统关系型数据库的B-Tree索引方式,但并不是Elasticsearch快速的原因。\[3\] 以上是elasticsearch快速入门介绍。 #### 引用[.reference_title] - *1* [Elasticsearch快速入门](https://blog.csdn.net/ks_1998/article/details/121237513)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【ElasticSearchElasticSearch 快速入门(精讲)](https://blog.csdn.net/m0_45067620/article/details/120377816)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值