目的
? ? ? ? 1.學(xué)習(xí)elasticsearch初體驗(yàn),更快了解elasticsearch的魅力
? ? ? ? 2.作為日常開(kāi)發(fā)操作elasticsearchAPI的參考
常用API
IK分詞器
? ? ? ? 即把一段中文或者別的劃分成一個(gè)個(gè)的關(guān)鍵字怠蹂,我們?cè)谒阉鲿r(shí)候會(huì)把自己的信息進(jìn)行分詞抗愁,會(huì)把 數(shù)據(jù)庫(kù)中或者索引庫(kù)中的數(shù)據(jù)進(jìn)行分詞锐朴,然后進(jìn)行一個(gè)匹配操作殷费,默認(rèn)的中文分詞是將每個(gè)字看成一個(gè)詞追驴。有時(shí)我們就要搜索固定一個(gè)詞組眉枕,比如"我愛(ài)學(xué)習(xí)"恶复;如果不用分詞器,就會(huì)拆分為"我"齐遵、"愛(ài)"寂玲、"學(xué)"、"習(xí)"這種或類似的情況梗摇,顯然不是我們想要的拓哟,所以需要使用中文分詞器IK來(lái)解決。
? ? ? ? IK提供了兩個(gè)分詞算法:ik_smart和ik_max_word伶授,其中ik_smart為最少切分断序、ik_max_word為最細(xì)粒度劃分
GET _analyze
{
"analyzer": "ik_smart",
"text": "我愛(ài)學(xué)習(xí)"
}
#IK分詞器算法 最細(xì)粒度劃分
GET _analyze
{
? "analyzer": "ik_max_word",
? "text": "我愛(ài)學(xué)習(xí)"
}
創(chuàng)建索引
# 方式一:直接創(chuàng)建索引,默認(rèn)分片和備份數(shù)量為1
PUT test1
# 方式二:創(chuàng)建索引糜烹,設(shè)置分片數(shù)量為5违诗、備份數(shù)量為1
PUT test2
{
? "settings": {
? ? "number_of_replicas": 1,
? ? "number_of_shards": 5
? }
}
# 方式三:創(chuàng)建索引,設(shè)置分片數(shù)量為5疮蹦、備份數(shù)量為1, 同時(shí)設(shè)置關(guān)系型結(jié)構(gòu)诸迟,類似表結(jié)構(gòu),分片和備份可不設(shè)置愕乎,有默認(rèn)值
PUT test3
{
? "settings": {
? ? "number_of_replicas": 1,
? ? "number_of_shards": 5
? },
? "mappings": {
? ? "properties": {
? ? ? "name": {
? ? ? ? "type": "keyword"
? ? ? },
? ? ? "age": {
? ? ? ? "type": "integer"
? ? ? },
? ? ? "birthday": {
? ? ? ? "type": "date",
? ? ? ? "format": "yyyy-MM-dd"
? ? ? }
? ? }
? }
}
刪除索引
DELETE test1
查看索引
GET test1
插入文檔數(shù)據(jù)
#(/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id})
#方式一: put方式必須指明id
PUT test1/_doc/1
{
? "name":"狂神說(shuō)",
? "age":3
}
#方式二: id選填阵苇,不指明時(shí)采用uuid
POST test3/_doc
{
? "name":"狂神說(shuō)22",
? "age":311
}
#方式四:?
PUT test1/_create/2
{
? "name":"狂神說(shuō)",
? "age":3
}
PUT test3/_create/2
{
? "name":"狂神說(shuō)22",
? "age":3,
? "birthday":"1997-02-01"
}
刪除文檔
DELETE test1/_doc/1
修改文檔數(shù)據(jù)
# 方式一: put請(qǐng)求,全覆蓋存在id的數(shù)據(jù)感论,故如name不傳值時(shí)绅项,會(huì)將name置空,如果id不存在則為新增
PUT test1/_doc/1
{
? "name":"狂神說(shuō)",
? "age":3
}
# 方式二: post請(qǐng)求,全覆蓋存在id的數(shù)據(jù)比肄,故如name不傳值時(shí)快耿,會(huì)將name置空囊陡,如果id不存在則為新增
POST test3/_doc/1
{
? "name":"狂神說(shuō)22",
? "age":311
}
# 方式三: post請(qǐng)求, 按指定id修改指定字段,該方法已過(guò)時(shí)
POST test3/_doc/1/_update
{
? "doc":{
? ? "name": "狂神說(shuō)33"
? }
}
# 方式四: post請(qǐng)求, 按指定id修改指定字段, 推薦
POST test3/_update/1
{
? "doc":{
? ? "name": "狂神說(shuō)3657573"
? }
}
基本查詢
# 方式一: 最簡(jiǎn)單的查詢
GET test3/_doc/1
# 方式二: 查詢索引全部數(shù)據(jù)
GET test1/_search
{
? "query": {
? ? "match_all": {}
? }
}
# 方式三: 查詢索引指定字段數(shù)據(jù)
GET test1/_search
{
? "query": {
? ? "match_all": {}
? },
? "_source": ["name"]
}
# 方式四: 查詢索引指定字段數(shù)據(jù)
#匹配含有name的文檔數(shù)據(jù)掀亥,如果name的類型為keyword不會(huì)走分詞器解析
#match是會(huì)使用分詞器解析的
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "name": "狂神說(shuō)"
? ? }
? }
}
#方式五:term直接精確查詢撞反,通過(guò)倒排索引查詢,效率高
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "age": "3"
? ? }
? }
}
復(fù)雜查詢
#排序
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "name": "狂神說(shuō)"
? ? }
? },
? "sort": [
? ? {
? ? ? "age": {
? ? ? ? "order": "desc"
? ? ? }
? ? }
? ]
}
#分頁(yè)
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "name": "狂神說(shuō)"
? ? }
? },
? "sort": [
? ? {
? ? ? "age": {
? ? ? ? "order": "desc"
? ? ? }
? ? }
? ],
? "from": 0,
? "size": 2
}
#布爾值查詢
# must(and)搪花,所有的條件都要符合
GET test3/_search
{
? "query": {
? ? "bool": {
? ? ? "must": [
? ? ? ? {
? ? ? ? ? "match": {
? ? ? ? ? ? "name": "狂神說(shuō)3657573"
? ? ? ? ? }
? ? ? ? },
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "age": "311"
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}
# should(or), 滿足一個(gè)條件即可
GET test3/_search
{
? "query": {
? ? "bool": {
? ? ? "should": [
? ? ? ? {
? ? ? ? ? "match": {
? ? ? ? ? ? "name": "狂神說(shuō)3657573"
? ? ? ? ? }
? ? ? ? },
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "age": "3"
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}
# must_not (not) 不等于
GET test3/_search
{
? "query": {
? ? "bool": {
? ? ? "must_not": [
? ? ? ? {
? ? ? ? ? "match": {
? ? ? ? ? ? "name": "狂神說(shuō)3657573"
? ? ? ? ? }
? ? ? ? },
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "age": "3"
? ? ? ? ? }
? ? ? ? }
? ? ? ]
? ? }
? }
}
#過(guò)濾器filter gt 大于 gte 大于等于 lt 小于 lte 小于等于痢畜!
GET test3/_search
{
? "query": {
? ? "bool": {
? ? ? "should": [
? ? ? ? {
? ? ? ? ? "match": {
? ? ? ? ? ? "name": "狂神說(shuō)3657573"
? ? ? ? ? }
? ? ? ? },
? ? ? ? {
? ? ? ? ? "term": {
? ? ? ? ? ? "age": "3"
? ? ? ? ? }
? ? ? ? }
? ? ? ],
? ? ? "filter": {
? ? ? ? "range": {
? ? ? ? ? "age": {
? ? ? ? ? ? "gte": 1,
? ? ? ? ? ? "lte": 20
? ? ? ? ? }
? ? ? ? }
? ? ? }
? ? }
? }
}
高亮查詢
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "name": "狂神說(shuō)"
? ? }
? },
? "highlight": {
? ? "fields": {
? ? ? "name": {}
? ? }
? }
}
#自定義高亮
GET test1/_search
{
? "query": {
? ? "match": {
? ? ? "name": "狂神說(shuō)"
? ? }
? },
? "highlight": {
? ? "pre_tags": "<p class='key' style='color:red'>",
? ? "post_tags": "</p>",
? ? "fields": {
? ? ? "name": {}
? ? }
? }
}