Elasticsearch学习

1:下载安装运行

[jifeng@jifeng04 ~]$ wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.tar.gz
--2014-09-24 11:41:30--  https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.1.1.tar.gz
正在解析主机 download.elasticsearch.org... 54.243.77.158, 54.225.133.195, 107.22.222.16, ...
Connecting to download.elasticsearch.org|54.243.77.158|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:19872305 (19M) [application/x-tar]
Saving to: `elasticsearch-1.1.1.tar.gz'

100%[===========================================================================>] 19,872,305   209K/s   in 2m 52s  

2014-09-24 11:44:25 (113 KB/s) - `elasticsearch-1.1.1.tar.gz' saved [19872305/19872305]

[jifeng@jifeng04 ~]$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
[jifeng@jifeng04 ~]$ tar zxf elasticsearch-1.1.1.tar.gz    
[jifeng@jifeng04 ~]$ ls
apache-tomcat-7.0.55.tar.gz  elasticsearch-1.1.1         hadoop       mongodb-linux-x86_64-2.6.4
Desktop                      elasticsearch-1.1.1.tar.gz  jdk1.7.0_45  mongodb-linux-x86_64-2.6.4.tgz
[jifeng@jifeng04 ~]$ cd elasticsearch-1.1.1
[jifeng@jifeng04 elasticsearch-1.1.1]$ ls
bin  config  lib  LICENSE.txt  NOTICE.txt  README.textile

运行:

[jifeng@jifeng04 elasticsearch-1.1.1]$ ./bin/elasticsearch
[2014-09-24 12:55:27,190][INFO ][node                     ] [Calypso] version[1.1.1], pid[11538], build[f1585f0/2014-04-16T14:27:12Z]
[2014-09-24 12:55:27,191][INFO ][node                     ] [Calypso] initializing ...
[2014-09-24 12:55:27,208][INFO ][plugins                  ] [Calypso] loaded [], sites []
[2014-09-24 12:55:29,943][INFO ][node                     ] [Calypso] initialized
[2014-09-24 12:55:29,943][INFO ][node                     ] [Calypso] starting ...


2:测试

[jifeng@jifeng03 ~]$ curl 'http://jifeng04:9200/?pretty'
{
  "status" : 200,
  "name" : "Calypso",
  "version" : {
    "number" : "1.1.1",
    "build_hash" : "f1585f096d3f3985e73456debdc1a0745f512bbc",
    "build_timestamp" : "2014-04-16T14:27:12Z",
    "build_snapshot" : false,
    "lucene_version" : "4.7"
  },
  "tagline" : "You Know, for Search"
}


3:计算集群中文件的数量

curl -XGET 'http://jifeng04:9200/_count?pretty' -d '
{  <5>
    "query": {
        "match_all": {}
    }
}'
执行:

[jifeng@jifeng03 ~]$ curl -XGET 'jifeng04:9200/_count?pretty' -d ' 
{
    "query": {
        "match_all": {}
    }
}'
{
  "count" : 3,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  }
}


[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/_count?pretty' -d '
> {  <5>
>     "query": {
>         "match_all": {}
>     }
> }
> '
{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}


4:提交
curl -XPUT 'http://jifeng04:9200/megacorp/employee/1' -d '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'

执行:
[jifeng@jifeng03 ~]$ curl -XPUT 'http://jifeng04:9200/megacorp/employee/1' -d '
> {
>     "first_name" : "John",
>     "last_name" :  "Smith",
>     "age" :        25,
>     "about" :      "I love to go rock climbing",
>     "interests": [ "sports", "music" ]
> }'
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"created":true}
[jifeng@jifeng03 ~]$ 
在提交2个员工的信息
curl -XPUT 'http://jifeng04:9200/megacorp/employee/2' -d '
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}'

curl -XPUT 'http://jifeng04:9200/megacorp/employee/3' -d '
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}'

5:检索
ID检索:
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/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" ]
}}[jifeng@jifeng03 ~]$ 

搜索全部员工:
curl -XGET 'http://jifeng04:9200/megacorp/employee/_search'
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search'
{"took":20,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"megacorp","_type":"employee","_id":"3","_score":1.0, "_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.0, "_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.0, "_source" : 
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}}]}}[jifeng@jifeng03 ~]$ 

条件搜索
搜索一下哪些员工的姓氏中包含Smith
curl -XGET 'http://jifeng04:9200/megacorp/employee/_search?q=last_name:Smith'
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search?q=last_name:Smith'
{"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.30685282, "_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":0.30685282, "_source" : 
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}}]}}[jifeng@jifeng03 ~]$ 

6:使用Query DSL搜索

搜索一下哪些员工的姓氏中包含Smith
curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}'
执行
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
> {
>     "query" : {
>         "match" : {
>             "last_name" : "Smith"
>         }
>     }
> }'
{"took":9,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.30685282, "_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":0.30685282, "_source" : 
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}}]}}[jifeng@jifeng03 ~]$ 

更加复杂的搜索:
curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "Smith" 
                }
            }
        }
    }
}‘
执行:
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
> {
>     "query" : {
>         "filtered" : {
>             "filter" : {
>                 "range" : {
>                     "age" : { "gt" : 30 } 
>                 }
>             },
>             "query" : {
>                 "match" : {
>                     "last_name" : "Smith" 
>                 }
>             }
>         }
>     }
> }
> '
{"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":0.30685282, "_source" : 
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}}]}}[jifeng@jifeng03 ~]$ 

全文搜索

上面的搜索都很简单:名字搜索、通过年龄过滤。接下来我们来学习一下更加复杂的搜索,全文搜索——一项在传统数据库很难实现的功能。 我们将会搜索所有喜欢 rock climbing 的员工:
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search?pretty' -d '
> {
>     "query" : {
>         "match" : {
>             "about" : "rock climbing"
>         }
>     }
> }'
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.16273327,
    "hits" : [ {
      "_index" : "megacorp",
      "_type" : "employee",
      "_id" : "1",
      "_score" : 0.16273327, "_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" : 0.016878016, "_source" : 
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
    } ]
  }
}
[jifeng@jifeng03 ~]$ 

pretty' 对输出结果格式化

段落搜索

能够找出每个字段中的独立单词最然很好,但是有的时候你可能还需要去匹配精确的短语或者 段落 。例如,我们只需要查询到 about 字段只包含 rock climbing 的短语的员工。
match查询变为match_phrase查询:
[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
> {
>     "query" : {
>         "match_phrase" : {
>             "about" : "rock climbing"
>         }
>     }
> }'
{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.23013961, "_source" : 
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}}]}}[jifeng@jifeng03 ~]$ 

高亮搜索

很多程序希望能在搜索结果中高亮匹配到的关键字来告诉用户这个文档是如何匹配他们的搜索的。在Elasticsearch中找到高亮片段是非常容易的。

让我们回到之前的查询,但是添加一个highlight参数:

[jifeng@jifeng03 ~]$ curl -XGET 'http://jifeng04:9200/megacorp/employee/_search' -d '
> {
>     "query" : {
>         "match_phrase" : {
>             "about" : "rock climbing"
>         }
>     },
>     "highlight": {
>         "fields" : {
>             "about" : {}
>         }
>     }
> }'
{"took":34,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_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 <em>rock</em> <em>climbing</em>"]}}]}}
[jifeng@jifeng03 ~]$ 

当我们运行这个查询后,相同的命中结果会被返回,但是我们会得到一个新的名叫 highlight 的部分。在这里包含了 about 字段中的匹配单词,并且会被 <em></em> HTML字符包裹住:

你可能感兴趣的:(elasticsearch,Lucene)