索引(index)> 映射(mapping) >  类型(type)   >  文档(document)

数据库(database)> 提纲(schema)  >  表(table)    >  表中一行记录(row)

一、索引的增删改查

1.1 创建空索引

PUT http://192.168.215.140:9200/haoke

{
    "settings": {
        "index": {
            "number_of_shards": "2", 
            "number_of_replicas": "0"
        }
    }
}

1.2 删除空索引

DELETE http://192.168.215.140:9200/haoke

 响应
{ 
 "acknowledged": true 
}

二、映射的创建

2.1 创建映射

PUT  http://192.168.215.140:9200/itcast?include_type_name=true

{
    "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    },
    "mappings": {
        "person": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "age": {
                    "type": "integer"
                },
                "mail": {
                    "type": "keyword"
                },
                "hobby": {
                    "type": "text"
                }
            }
        }
    }
}

2.2 查询映射

GET  http://192.168.215.140:9200/itcast/_mapping

三、文档的增删改查

3.1 创建文档

POST /{索引}/{类型}/{id}

POST http://192.168.215.140:9200/haoke/user/1007

{
    "id": 1007,
    "name": "黄九",
    "age": 33,
    "sex": "女"
}

3.2 修改文档(全量覆盖/部分覆盖)

一、全量覆盖
在Elasticsearch中,文档数据是不为修改的,但是可以通过覆盖的方式进行更新。

POST http://192.168.215.140:9200/haoke/user/1001

{
    "id": 1001,
    "name": "王五",
    "age": 25
}

二、部分覆盖
 1. 从旧文档中检索JSON
 2. 修改它
 3. 删除旧文档
 4. 索引新文档

POST  http://192.168.215.140:9200/haoke/user/1001/_update

{
    "doc": {
        "age": 18
    }
}

3.3 删除文档

DELETE http://192.168.215.140:9200/haoke/user/1001

3.4 搜索文档

一、根据id搜索

GET http://192.168.215.140:9200/haoke/user/iqic7n8Bx1AQawMwffow

二、全量搜索 

http://192.168.215.140:9200/haoke/user/_search

三、条件搜索

http://192.168.215.140:9200/haoke/user/_search?q=age:20

2.5 判断文档是否存在

HEAD   http://192.168.215.140:9200/haoke/user/1005

存在     返回200
不存在   返回404

四、DSL查询


DSL(Domain Specifific Language 特定领域语言 )



4.1 DSL查询 _search

查询30岁以上的男性

POST  http://192.168.215.140:9200/haoke/user/_search

{
    "query": {
        "bool": {
            "filter": {
                "range": {
                    "age": {
                        "gt": 30
                    }
                }
            },
            "must": {
                "match": {
                    "sex": "男"
                }
            }
        }
    }
}

4.2 DSL高亮查询 _search

POST http://192.168.215.140:9200/haoke/user/_search

{
    "query": {
        "match": {
            "name": "张三 李四"
        }
    },
    "highlight": {
        "fields": {
            "name": {}
        }
    }
}

4.3 DSL 聚合查询 _search

POST http://192.168.215.140:9200/haoke/user/_search

{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "age"
            }
        }
    }
}

五、高级查询(结构化查询)

5.1 pretty美化

可以在查询url后面添加pretty参数,使得返回的json更易查看

GET  http://192.168.215.140:9200/haoke/user/1005?pretty

5.2  _source 指定只响应的字段

GET  http://192.168.215.140:9200/haoke/user/1005?_source=id,name

# 响应
{
    "_index":"haoke",
    "_type":"user",
    "_id":"1005",
    "_version":1,
    "found":true,
    "_source":{
        "name":"孙七",
        "id":1005
    }
}

5.3 term 精确查询(=)

term 主要用于精确匹配哪些值,比如数字,日期,布尔值或not_analyzed的字符串

POST  http://192.168.215.140:9200/itcast/person/_search

{
    "query": {
        "term": {
            "age": 20
        }
    }
}

5.4 terms 多值精确查询(like)

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。
如果某个字段指定了多个值,那么文档需要一起去做匹配

POST http://192.168.215.140:9200/itcast/person/_search

{
    "query": {
        "terms": {
            "age": [20,21,22]
        }
    }
}

5.5 range 范围查询 (< <=  > >= )

gt : 大于
gte : 大于等于
lt : 小于
lte : 小于等于

POST http://192.168.215.140:9200/itcast/person/_search

{
    "query":{
        "range":{
            "age":{
                "gte":20,
                "lte":22
            }
        }
    }
}

5.6 exists 是否存在某个字段

exists 查询可以用于查找文档中是否包含指定字段或没有某个字段

POST http://192.168.215.140:9200/haoke/user/_search

{
    "query":{
        "exists":{
            "field":"card"
        }
    }
}

5.7 match 标准查询 

match 查询是一个标准查询,
不管你需要全文本查询还是精确查询基本上都要用到它。
如果你使用 match 查询一个全文本字段,
它会在真正查询之前用分析器先分析 match 一下查询字符。

{ "match": { "age": 26 }} 
{ "match": { "date": "2014-09-01" }} 
{ "match": { "public": true }} 
{ "match": { "tag": "full_text" }}

5.8 bool 逻辑合并查询

bool 查询可以用来合并多个条件查询结果的布尔逻辑,它包含一下操作符:
 must :: 多个查询条件的完全匹配,相当于 and 。
 must_not :: 多个查询条件的相反匹配,相当于 not 。
 should :: 至少有一个查询条件匹配, 相当于 or 。
 这些参数可以分别继承一个查询条件或者一个查询条件的数组

{
    "bool":{
        "must":{
            "term":{
                "folder":"inbox"
            }
        },
        "must_not":{
            "term":{
                "tag":"spam"
            }
        },
        "should":[
            {
                "term":{
                    "starred":true
                }
            },
            {
                "term":{
                    "unread":true
                }
            }
        ]
    }
}

5.9 filter 过滤查询

做精确匹配搜索时,最好用过滤语句,因为过滤语句可以缓存数据

POST http://192.168.215.140:9200/itcast/person/_search

{
    "query":{
        "bool":{
            "filter":{
                "term":{
                    "age":20
                }
            }
        }
    }
}

5.10 _mget 批量查询

POST http://192.168.215.140:9200/haoke/user/_mget

{ 
    "ids" : [ "1001", "1003" ] 
}

5.11 _bulk 批量插入删除

最后一行有换行

批量插入
POST http://192.168.215.140:9200/haoke/user/_bulk

{"create":{"_index":"haoke","_type":"user","_id":2001}} 
{"id":2001,"name":"name1","age": 20,"sex": "男"} 
{"create":{"_index":"haoke","_type":"user","_id":2002}}
{"id":2002,"name":"name2","age": 20,"sex": "男"}
{"create":{"_index":"haoke","_type":"user","_id":2003}} 
{"id":2003,"name":"name3","age": 20,"sex": "男"}

批量删除
POST  http://192.168.215.140:9200/haoke/user/_bulk

{"delete":{"_index":"haoke","_type":"user","_id":2001}}
{"delete":{"_index":"haoke","_type":"user","_id":2002}}
{"delete":{"_index":"haoke","_type":"user","_id":2003}}

5.12 分页查询(size、from)

GET  http://192.168.215.140:9200/haoke/user/_search?size=1&from=1

六、中文分词

6.1 标准分词

POST http://192.168.215.140:9200/itcast/_analyze

{
    "analyzer": "standard",
    "field":"hobby",
    "text": "听音乐"
}

6.2 ik分词

POST  http://192.168.215.140:9200/_analyze

{
    "analyzer": "ik_max_word",
    "text": "我爱吃拉面"
}

结果

{
    "tokens":[
        {
            "token":"我",
            "start_offset":0,
            "end_offset":1,
            "type":"CN_CHAR",
            "position":0
        },
        {
            "token":"爱",
            "start_offset":1,
            "end_offset":2,
            "type":"CN_CHAR",
            "position":1
        },
        {
            "token":"吃",
            "start_offset":2,
            "end_offset":5,
            "type":"CN_WORD",
            "position":2
        },
        {
            "token":"吃拉面",
            "start_offset":2,
            "end_offset":4,
            "type":"CN_WORD",
            "position":3
        },
        {
            "token":"拉面",
            "start_offset":3,
            "end_offset":5,
            "type":"CN_WORD",
            "position":4
        }
    ]
}

七、全文检索

7.1 构造数据(创建索引与文档)

PUT http://192.168.215.140:9200/itcast

{
    "settings":{
        "index":{
            "number_of_shards":"1",
            "number_of_replicas":"0"
        }
    },
    "mappings":{
        "person":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "age":{
                    "type":"integer"
                },
                "mail":{
                    "type":"keyword"
                },
                "hobby":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                }
            }
        }
    }
}


POST http://192.168.215.140:9200/itcast/_bulk 

{"index":{"_index":"itcast","_type":"person"}} 
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"}
{"index":{"_index":"itcast","_type":"person"}} 
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"}
{"index":{"_index":"itcast","_type":"person"}} 
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"}
{"index":{"_index":"itcast","_type":"person"}} 
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳、篮球"} 
{"index":{"_index":"itcast","_type":"person"}} 
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影、羽毛球"}

7.2 单词搜索

POST http://192.168.215.140/itcast/person/_search  

{
    "query":{
        "match":{
            "hobby":"音乐"
        }
    },
    "highlight":{
        "fields":{
            "hobby":{

            }
        }
    }
}

结果

{
    "took":9,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":2,
        "max_score":0.6841192,
        "hits":[
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"Uv0cDWgBR-bSw8-LpdkZ",
                "_score":0.6841192,
                "_source":{
                    "name":"王五",
                    "age":22,
                    "mail":"333@qq.com",
                    "hobby":"羽毛球、篮球、游泳、听音乐"
                },
                "highlight":{
                    "hobby":[
                        "羽毛球、篮球、游泳、听<em>音乐</em>"
                    ]
                }
            },
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"VP0cDWgBR-bSw8-LpdkZ",
                "_score":0.6841192,
                "_source":{
                    "name":"孙七",
                    "age":24,
                    "mail":"555@qq.com",
                    "hobby":"听音乐、看电影、羽毛球"
                },
                "highlight":{
                    "hobby":[
                        "听<em>音乐</em>、看电影、羽毛球"
                    ]
                }
            }
        ]
    }
}

7.3 多词搜索

POST http://192.168.215.140:9200/itcast/person/_search  

{
    "query":{
        "match":{
            "hobby":"音乐 篮球"
        }
    },
    "highlight":{
        "fields":{
            "hobby":{

            }
        }
    }
}

结果

{
    "took":3,
    "timed_out":false,
    "_shards":{
        "total":1,
        "successful":1,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":4,
        "max_score":1.3192271,
        "hits":[
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"Uv0cDWgBR-bSw8-LpdkZ",
                "_score":1.3192271,
                "_source":{
                    "name":"王五",
                    "age":22,
                    "mail":"333@qq.com",
                    "hobby":"羽毛球、篮球、游泳、听音乐"
                },
                "highlight":{
                    "hobby":[
                        "羽毛球、<em>篮球</em>、游泳、听<em>音乐</em>"
                    ]
                }
            },
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"VP0cDWgBR-bSw8-LpdkZ",
                "_score":0.81652206,
                "_source":{
                    "name":"孙七",
                    "age":24,
                    "mail":"555@qq.com",
                    "hobby":"听音乐、看电影、羽毛球"
                },
                "highlight":{
                    "hobby":[
                        "听<em>音乐</em>、看电影、羽毛球"
                    ]
                }
            },
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"Vf0gDWgBR-bSw8-LOdm_",
                "_score":0.6987338,
                "_source":{
                    "name":"赵六",
                    "age":23,
                    "mail":"444@qq.com",
                    "hobby":"跑步、游泳、篮球"
                },
                "highlight":{
                    "hobby":[
                        "跑步、游泳、<em>篮球</em>"
                    ]
                }
            },
            {
                "_index":"itcast",
                "_type":"person",
                "_id":"Uf0cDWgBR-bSw8-LpdkZ",
                "_score":0.50270504,
                "_source":{
                    "name":"李四",
                    "age":21,
                    "mail":"222@qq.com",
                    "hobby":"羽毛球、乒乓球、足球、篮球"
                },
                "highlight":{
                    "hobby":[
                        "羽毛球、乒乓球、足球、<em>篮球</em>"
                    ]
                }
            }
        ]
    }
}

7.4 组合搜索

POST http://192.168.215.140:9200/itcast/person/_search


搜索结果中必须包含篮球,不能包含音乐,如果包含了游泳,那么它的相似度更高。

{
    "query":{
        "bool":{
            "must":{
                "match":{
                    "hobby":"篮球"
                }
            },
            "must_not":{
                "match":{
                    "hobby":"音乐"
                }
            },
            "should":[
                {
                    "match":{
                        "hobby":"游泳"
                    }
                }
            ]
        }
    },
    "highlight":{
        "fields":{
            "hobby":{

            }
        }
    }
}

结果

es创建数据_字段

 7.5 权重

搜索关键字为“游泳篮球”,如果结果中包含了“音乐”权重为10,包含了“跑步”权重为2


POST http://192.168.215.140:9200/itcast/person/_search

{
    "query":{
        "bool":{
            "must":{
                "match":{
                    "hobby":{
                        "query":"游泳篮球",
                        "operator":"and"
                    }
                }
            },
            "should":[
                {
                    "match":{
                        "hobby":{
                            "query":"音乐",
                            "boost":10
                        }
                    }
                },
                {
                    "match":{
                        "hobby":{
                            "query":"跑步",
                            "boost":2
                        }
                    }
                }
            ]
        }
    },
    "highlight":{
        "fields":{
            "hobby":{

            }
        }
    }
}

es创建数据_搜索_02