设为首页 收藏本站
查看: 1233|回复: 0

[经验分享] Elasticsearch 安装使用

[复制链接]

尚未签到

发表于 2019-1-28 14:26:45 | 显示全部楼层 |阅读模式
  Elasticsearch是一个全文搜索引擎。
  安装Elasticsearch时需要先安装Java
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.zip
unzip elasticsearch-1.4.2.zip
cd elasticsearch-1.4.2
./bin/elasticsearch
  

  安装Marvel
  Marvel是一个管理和监控Elasticsearch的工具。它提供一个叫Sense的交互式接口方便通过浏览器与Elasticsearch交互。

  

  bin/plugin -i elasticsearch/marvel/latest
  

  如果不想使用Marvel监控本地集群,可以使用如下方式关闭Marvel监控

echo 'marvel.agent.enabled: false' >> ./config/elasticsearch.yml  可以通过前台的方式启动Elasticsearch

  bin]$ sudo ./elasticsearch
  

  使用-d参数可以将Elasticsearch放到后台运行

  bin]$ sudo ./elasticsearch -d
  

  查看Elasticsearch中的数据

$ curl "http://localhost:9200/?pretty"
{
  "status" : 200,
  "name" : "xxx",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.2",
    "build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
    "build_timestamp" : "2014-12-16T14:11:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}  

  通过config/elasticsearch.yml设置cluster.name和node.name
  

  

  可以通过以下方式关闭Elasticsearch
curl -XPOST 'http://localhost:9200/_shutdown'  

  Talking to Elasticsearch
  根据是否使用Java语言,与Elasticsearch交互有几种方法,如果是Java API参见文档

  http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
  如果使用其他语言,则使用Elasticsearch提供的RESTFUL API,或者可以直接使用linux命令curl访问
  

curl -X '://:/?' -d ''  

  VER         HTTP请求方式,GET,POST,PUT,HEAD或DELETE

  PROTOCOL      使用HTTP或者HTTPS

  HOST        Elasticsearch集群中的任意一个node的主机名,如果是在node本机就直接使用localhost

  PORT        Elasticsearch运行HTTP服务的端口,默认是9200

  QUERY_STRING   查询参数
  BODY        JSON格式的请求数据
$ curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'
{
  "count" : 22692,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  }
}$ curl -i -XGET 'localhost:9200/'
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 334
{
  "status" : 200,
  "name" : "jidong",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "1.4.2",
    "build_hash" : "927caff6f05403e936c20bf4529f144f0c89fd8c",
    "build_timestamp" : "2014-12-16T14:11:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.2"
  },
  "tagline" : "You Know, for Search"
}  

  

Relational DB   Databases    Tables   Rows     Columns
Elasticsearch   Indices    Types   Documents  Fields  

  通过Marvel的Sense接口访问Elasticsearch
  http://xxxx.com:9200/_plugin/marvel/sense/index.html
  以下直接使用GET或PUT的简略形式,都是直接使用Marvel的Sense接口访问Elasticsearch,可以点击“Copy as cURL”查看对应的curl命令写法
  

DSC0000.png

  

PUT /megacorp/employee/1
{
   
"first_name" : "John",
   
"last_name" :  "Smith",
   
"age" :        25,
   
"about" :      "I love to go rock climbing",
   
"interests": [ "sports", "music" ]
}/megacorp/employee/1  这个路径包含三个信息
  megacorp 索引名称,类似关系型数据库的数据库名称

  employee 类型名称,类似关系型数据库的表名称
  1     特定employee的ID
  

  

PUT /megacorp/employee/2
{
   
"first_name" :  "Jane",
   
"last_name" :   "Smith",
   
"age" :         32,
   
"about" :       "I like to collect rock albums",
   
"interests":  [ "music" ]
}
PUT
/megacorp/employee/3
{
   
"first_name" :  "Douglas",
   
"last_name" :   "Fir",
   
"age" :         35,
   
"about":        "I like to build cabinets",
   
"interests":  [ "forestry" ]
}  

  在Sense中输入
GET /megacorp/employee/1  显示结果
{
  
"_index" :   "megacorp",
  
"_type" :    "employee",
  
"_id" :      "1",
  
"_version" : 1,
  
"found" :    true,
  
"_source" :  {
      
"first_name" :  "John",
      
"last_name" :   "Smith",
      
"age" :         25,
      
"about" :       "I love to go rock climbing",
      
"interests":  [ "sports", "music" ]
  
}
}  

  

GET /megacorp/employee/_search  

{
   
"took":      6,
   
"timed_out": false,
   
"_shards": { ... },
   
"hits": {
      
"total":      3,
      
"max_score":  1,
      
"hits": [
         
{
            
"_index":         "megacorp",
            
"_type":          "employee",
            
"_id":            "3",
            
"_score":         1,
            
"_source": {
               
"first_name":  "Douglas",
               
"last_name":   "Fir",
               
"age":         35,
               
"about":       "I like to build cabinets",
               
"interests": [ "forestry" ]
            
}
         
},
         
{
            
"_index":         "megacorp",
            
"_type":          "employee",
            
"_id":            "1",
            
"_score":         1,
            
"_source": {
               
"first_name":  "John",
               
"last_name":   "Smith",
               
"age":         25,
               
"about":       "I love to go rock climbing",
               
"interests": [ "sports", "music" ]
            
}
         
},
         
{
            
"_index":         "megacorp",
            
"_type":          "employee",
            
"_id":            "2",
            
"_score":         1,
            
"_source": {
               
"first_name":  "Jane",
               
"last_name":   "Smith",
               
"age":         32,
               
"about":       "I like to collect rock albums",
               
"interests": [ "music" ]
            
}
         
}
      
]
   
}
}  

GET /megacorp/employee/_search?q=last_name:Smith{
   
...
   
"hits": {
      
"total":      2,
      
"max_score":  0.30685282,
      
"hits": [
         
{
            
...
            
"_source": {
               
"first_name":  "John",
               
"last_name":   "Smith",
               
"age":         25,
               
"about":       "I love to go rock climbing",
               
"interests": [ "sports", "music" ]
            
}
         
},
         
{
            
...
            
"_source": {
               
"first_name":  "Jane",
               
"last_name":   "Smith",
               
"age":         32,
               
"about":       "I like to collect rock albums",
               
"interests": [ "music" ]
            
}
         
}
      
]
   
}
}  

  

  Elasticsearch提供了一个丰富的,灵活的查询语言,叫做DSL.Domain-specific language(DSL)使用特定的JSON请求。
GET /megacorp/employee/_search
{
   
"query" : {
        
"match" : {
            
"last_name" : "Smith"
        
}
   
}
}  这里没有使用查询参数,使用match匹配查询条件。输出结果和上个例子相同。

  

  

  查找所有last name为Smith,年龄大于30的员工
[object Object][object Object]  

{
   
...
   
"hits": {
      
"total":      1,
      
"max_score":  0.30685282,
      
"hits": [
         
{
            
...
            
"_source": {
               
"first_name":  "Jane",
               
"last_name":   "Smith",
               
"age":         32,
               
"about":       "I like to collect rock albums",
               
"interests": [ "music" ]
            
}
         
}
      
]
   
}
}  

  Full-text search 全文搜索

  

  搜索所有喜欢rock climbing的员工
  在Sense中输入
GET /megacorp/employee/_search
{
   
"query" : {
        
"match" : {
            
"about" : "rock climbing"
        
}
   
}
}  

  查看查询结果
[object Object][object Object]  默认情况下,Elasticsearch根据匹配结果的relevance score进行排序,表示匹配程度。可以看到第二个匹配结果只包含rock也被显示出来。
  

  如果想要完全匹配查询条件,可以使用短语搜索phrase search
  使用match_phrase进行条件匹配
GET /megacorp/employee/_search
{
   
"query" : {
        
"match_phrase" : {
            
"about" : "rock climbing"
        
}
   
}
}  
{
   
...
   
"hits": {
      
"total":      1,
      
"max_score":  0.23013961,
      
"hits": [
         
{
            
...
            
"_score":         0.23013961,
            
"_source": {
               
"first_name":  "John",
               
"last_name":   "Smith",
               
"age":         25,
               
"about":       "I love to go rock climbing",
               
"interests": [ "sports", "music" ]
            
}
         
}
      
]
   
}
}  现在就只有一条搜索结果
  

  

  Highlight our searches高亮显示查询结果
GET /megacorp/employee/_search
{
   
"query" : {
        
"match_phrase" : {
            
"about" : "rock climbing"
        
}
   
},
   
"highlight": {
        
"fields" : {
            
"about" : {}
        
}
   
}
}{
   
...
   
"hits": {
      
"total":      1,
      
"max_score":  0.23013961,
      
"hits": [
         
{
            
...
            
"_score":         0.23013961,
            
"_source": {
               
"first_name":  "John",
               
"last_name":   "Smith",
               
"age":         25,
               
"about":       "I love to go rock climbing",
               
"interests": [ "sports", "music" ]
            
},
            
"highlight": {
               
"about": [
                  
"I love to go rock climbing"
               
]
            
}
         
}
      
]
   
}
}  

  

  使用Elasticsearch的聚合函数可以对数据进行复杂的分析。类似SQL语言的GROUP BY语句。
GET /megacorp/employee/_search
{
  
"aggs": {
   
"all_interests": {
      
"terms": { "field": "interests" }
   
}
  
}
}{
   
...
   
"hits": { ... },
   
"aggregations": {
      
"all_interests": {
         
"buckets": [
            
{
               
"key":       "music",
               
"doc_count": 2
            
},
            
{
               
"key":       "forestry",
               
"doc_count": 1
            
},
            
{
               
"key":       "sports",
               
"doc_count": 1
            
}
         
]
      
}
   
}
}  

GET /megacorp/employee/_search
{
  
"query": {
   
"match": {
      
"last_name": "smith"
   
}
  
},
  
"aggs": {
   
"all_interests": {
      
"terms": {
        
"field": "interests"
      
}
   
}
  
}
}  ...
  
"all_interests": {
     
"buckets": [
        
{
           
"key": "music",
           
"doc_count": 2
        
},
        
{
           
"key": "sports",
           
"doc_count": 1
        
}
     
]
  
}  

  

  Elasticsearch可以横向扩展到几百台服务器,处理PB以上的数据。
  

  

  

  

  参考文档
  http://www.elasticsearch.com/guide/en/elasticsearch/guide/current/index.html
  http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index.html
  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-668806-1-1.html 上篇帖子: elasticsearch 的使用 下篇帖子: elasticSearch服务安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表