第一次看elasticsearch,建议大家看官方文档,我这个就是官方抄来的鹅已。。。。
参考文档:Elasticsearch 权威指南
基本概念对比关系图:
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices -> Types -> Documents -> Fields举例
| Relational DB | Databases | Tables | Rows | Columns |
| Elasticsearch | Indices | Types | Documents | Fileds |
| Elasticsearch | Alibaba Crop | employee | {"name":"Jack Ma", "age":50} | name, age |
索引就是存数据:
PUT /megacorp/employee/1有个数据库叫:阿里巴巴公司 Alibaba Crop (索引名)
有一张表叫:员工表 employee (类型名)
数据有一位员工(行):{ "name":"Jack Ma", "age":50}
表中的字段(列):name, age
id:1
不需要做别的,可以直接索引文档(一条数据)。
「索引」含义的区分:
索引(名词)
(index) 就像数据库,index的复数是indices 或indexes(类似多个数据库)。
索引(动词)
把一个文档(一条数据)存储到索引(数据库)中,就是动词的索引,类似插入数据 insert。
查询不叫索引,叫检索,查询。如果文档已经存在,新的文档将覆盖旧的文档。
倒排索引
传统数据库为特定列增加一个索引,例如B-Tree索引来加速检索。
Elasticsearch和Lucene使用一种叫做倒排索引(inverted index)的数据结构来达到相同目的。
默认情况下,文档中的所有字段都会被索引(拥有一个倒排索引),只有这样他们才是可被搜索的。
检索就是查数据:
检索id为1的用户
只需执行HTTP GET请求并指出文档的“地址”——索引、类型和ID既可
GET /alibaba/employee/1返回Json(主要数据在_source字段中):
{
"_index" : "alibaba",//数据库
"_type" : "employee",//表
"_id" : "1",//这条数据的id
"_version" : 1,
"found" : true,
"_source" : {
"name" : "Jack Ma",
"age" : 50
}
}简单搜索所有用户
假设现在用 _search 代替原来的 id
GET /alibaba/employee/_search返回Json(hits数组中包括了所有的数据):
{
"took": 6,
"timed_out": false,
"_shards": { ... },
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "alibaba",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"name": "Mike",
"age": 35
}
},
{
"_index": "alibaba",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"name": "Jack Ma",
"age": 50
}
},
{
"_index": "alibaba",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source": {
"name": "Jane",
"age": 22
}
}
]
}
}搜索名字中含Jack的用户
GET /alibaba/employee/_search?q=last_name:Jack句中的q 就是查询字符串(query string)
返回Json:
{
...
"hits": {
"total": 2,
"max_score": 0.30685282,
"hits": [
{
...
"_source": {
"name": "Jack Ma",
"age": 50
}
}
]
}
}使用DSL语句查询Jack
(Domain Specific Language特定领域语言),以Json形式出现,更灵活
GET /alibaba/employee/_search
{
"query" : {
"match" : {
"name" : "jACK"
}
}
}更复杂的搜索,多条件查询
GET /alibaba/employee/_search
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 } <1> //区间过滤器(range filter),greater than 大于30
}
},
"query" : {
"match" : {
"name" : "Jack" <2> //名字叫Jack的
}
}
}
}
}全文搜索
以官方例子,搜索所有喜欢“rock climbing”的员工:
只要出现了rock或者climbing的都会有
GET /alibaba/employee/_search
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}返回:
{
...
"hits": {
"total": 2,
"max_score": 0.16273327,
"hits": [
{
...
"_score": 0.16273327, <1> //相关性评分
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
},
{
...
"_score": 0.016878016, <2> //相关性评分
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [ "music" ]
}
}
]
}
}短语搜索
一定要rock climbing相邻,是个短语phrase
GET /alibaba/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关键词
GET /alibaba/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 <em>rock</em> <em>climbing</em>" <1>
]
}
}
]
}
}木有了。。。