一钓葫、 ES簡單的增刪改查
1悄蕾、創(chuàng)建一篇文檔(有則修改,無則創(chuàng)建;注意:PUT/POST直接修改必須全字段)
PUT test/doc/2
{
"name":"wangfei",
"age":27,
"desc":"熱天還不讓后人不認(rèn)同"
}
PUT test/doc/1
{
"name":"wangjifei",
"age":27,
"desc":"薩芬我反胃為范圍額"
}
PUT test/doc/3
{
"name":"wangyang",
"age":30,
"desc":"點(diǎn)在我心內(nèi)的幾首歌"
}
// 指定修改name字段
POST test/doc/_update/3
{
"doc": {
"name":"kingtao"
}
}
2础浮、查詢指定索引信息
GET test
3帆调、 查詢指定文檔信息
GET test/doc/1
GET test/doc/2
4、查詢對(duì)應(yīng)索引下所有數(shù)據(jù)
GET test/doc/_search
或
GET test/doc/_search
{
"query": {
"match_all": {}
}
}
5豆同、刪除指定文檔
DELETE test/doc/3
POST test/_delete_by_query
{
"query":{
"term":{
"title":"666"
}
}
}
6番刊、刪除索引
DELETE test
7、修改指定文檔方式
修改時(shí)诱告,不指定的屬性會(huì)自動(dòng)覆蓋撵枢,只保留指定的屬性(不正確的修改指定文檔方式)
PUT test/doc/1
{
"name":"王計(jì)飛"
}
使用POST命令宅倒,在id后面跟_update倔撞,要修改的內(nèi)容放到doc文檔(屬性)中(正確的修改指定文檔方式)
POST test/doc/_update/1
{
"doc":{
"desc":"生活就像 茫茫海上"
}
}
二、ES查詢的兩種方式
1、查詢字符串搜索
GET test/doc/_search?q=name:wangfei
2凤价、結(jié)構(gòu)化查詢(單字段查詢,不能多字段組合查詢)
GET test/doc/_search
{
"query":{
"match":{
"name":"wang"
}
}
}
三、match系列之操作
1可款、match系列之match_all (查詢?nèi)?
GET test/doc/_search
{
"query":{
"match_all": {
}
}
}
2拣挪、match系列之match_phrase(短語查詢)
準(zhǔn)備數(shù)據(jù)
PUT test1/doc/1
{
"title": "中國是世界上人口最多的國家"
}
PUT test1/doc/2
{
"title": "美國是世界上軍事實(shí)力最強(qiáng)大的國家"
}
PUT test1/doc/3
{
"title": "北京是中國的首都"
}
查詢語句
GET test1/doc/_search
{
"query":{
"match":{
"title":"中國"
}
}
}
輸出結(jié)果
{
"took" : 241,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.68324494,
"hits" : [
{
"_index" : "test1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.68324494,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "test1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
},
{
"_index" : "test1",
"_type" : "doc",
"_id" : "2",
"_score" : 0.39556286,
"_source" : {
"title" : "美國是世界上軍事實(shí)力最強(qiáng)大的國家"
}
}
]
}
}
通過觀察結(jié)果可以發(fā)現(xiàn),雖然如期的返回了中國的文檔宵晚。但是卻把和美國的文檔也返回了垂攘,這并不是我們想要的。是怎么回事呢淤刃?因?yàn)檫@是elasticsearch在內(nèi)部對(duì)文檔做分詞的時(shí)候晒他,對(duì)于中文來說,就是一個(gè)字一個(gè)字分的逸贾,所以陨仅,我們搜中國,中和國都符合條件铝侵,返回灼伤,而美國的國也符合。而我們認(rèn)為中國是個(gè)短語咪鲜,是一個(gè)有具體含義的詞狐赡。所以elasticsearch在處理中文分詞方面比較弱勢。后面會(huì)講針對(duì)中文的插件疟丙。但目前我們還有辦法解決颖侄,那就是使用短語查詢 用match_phrase
GET test1/doc/_search
{
"query":{
"match_phrase": {
"title": "中國"
}
}
}
查詢結(jié)果
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "test1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
}
]
}
}
我們搜索中國和世界這兩個(gè)指定詞組時(shí),但又不清楚兩個(gè)詞組之間有多少別的詞間隔隆敢。那么在搜的時(shí)候就要留有一些余地发皿。這時(shí)就要用到了slop了。相當(dāng)于正則中的中國.*?世界拂蝎。這個(gè)間隔默認(rèn)為0
GET test1/doc/_search
{
"query":{
"match_phrase": {
"title": {
"query": "中國世界",
"slop":2
}
}
}
}
查詢結(jié)果
{
"took" : 23,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.7445889,
"hits" : [
{
"_index" : "test1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.7445889,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
}
]
}
}
3穴墅、match系列之match_phrase_prefix(最左前綴查詢)智能搜索--以什么開頭
數(shù)據(jù)準(zhǔn)備
PUT test2/doc/1
{
"title": "prefix1",
"desc": "beautiful girl you are beautiful so"
}
PUT test2/doc/2
{
"title": "beautiful",
"desc": "I like basking on the beach"
}
搜索特定英文開頭的數(shù)據(jù)
查詢語句
GET test2/doc/_search
{
"query": {
"match_phrase_prefix": {
"desc": "bea"
}
}
}
查詢結(jié)果()
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.39556286,
"hits" : [
{
"_index" : "test2",
"_type" : "doc",
"_id" : "1",
"_score" : 0.39556286,
"_source" : {
"title" : "prefix1",
"desc" : "beautiful girl you are beautiful so"
}
},
{
"_index" : "test2",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"title" : "beautiful",
"desc" : "I like basking on the beach"
}
}
]
}
}
查詢短語
GET test2/doc/_search
{
"query": {
"match_phrase_prefix": {
"desc": "you are bea"
}
}
}
查詢結(jié)果
{
"took" : 28,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "test2",
"_type" : "doc",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"title" : "prefix1",
"desc" : "beautiful girl you are beautiful so"
}
}
]
}
}
max_expansions 參數(shù)理解 前綴查詢會(huì)非常的影響性能,要對(duì)結(jié)果集進(jìn)行限制温自,就加上這個(gè)參數(shù)玄货。
GET test2/doc/_search
{
"query": {
"match_phrase_prefix": {
"desc": {
"query": "bea",
"max_expansions":1
}
}
}
}
4、match系列之multi_match(多字段查詢)
multi_match是要在多個(gè)字段中查詢同一個(gè)關(guān)鍵字 除此之外悼泌,mulit_match甚至可以當(dāng)做match_phrase和match_phrase_prefix使用松捉,只需要指定type類型即可
GET test2/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["title","desc"]
}
}
}
查詢結(jié)果
{
"took" : 43,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.39556286,
"hits" : [
{
"_index" : "test2",
"_type" : "doc",
"_id" : "1",
"_score" : 0.39556286,
"_source" : {
"title" : "prefix1",
"desc" : "beautiful girl you are beautiful so"
}
},
{
"_index" : "test2",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"title" : "beautiful",
"desc" : "I like basking on the beach"
}
}
]
}
}
當(dāng)設(shè)置屬性 type:phrase 時(shí) 等同于 短語查詢
GET test1/doc/_search
{
"query": {
"multi_match": {
"query": "中國",
"fields": ["title"],
"type": "phrase"
}
}
}
查詢結(jié)果
{
"took" : 47,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "test1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
}
]
}
}
當(dāng)設(shè)置屬性 type:phrase_prefix時(shí) 等同于 最左前綴查詢
GET test2/doc/_search
{
"query": {
"multi_match": {
"query": "bea",
"fields": ["desc"],
"type": "phrase_prefix"
}
}
}
查詢結(jié)果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "test1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "test1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
}
]
}
}
match 查詢相關(guān)總結(jié)
1、match:返回所有匹配的分詞馆里。
2隘世、match_all:查詢?nèi)俊?/p>
3可柿、match_phrase:短語查詢,在match的基礎(chǔ)上進(jìn)一步查詢?cè)~組丙者,可以指定slop分詞間隔复斥。
4、match_phrase_prefix:前綴查詢械媒,根據(jù)短語中最后一個(gè)詞組做前綴匹配目锭,可以應(yīng)用于搜索提示,但注意和max_expanions搭配纷捞。其實(shí)默認(rèn)是50.......
5痢虹、multi_match:多字段查詢,使用相當(dāng)?shù)撵`活主儡,可以完成match_phrase和match_phrase_prefix的工作奖唯。
四、ES的排序查詢
es 6.8.4版本中缀辩,需要分詞的字段不可以直接排序臭埋,比如:text類型,如果想要對(duì)這類字段進(jìn)行排序臀玄,需要特別設(shè)置:對(duì)字段索引兩次,一次索引分詞(用于搜索)一次索引不分詞(用于排序)畅蹂,es默認(rèn)生成的text類型字段就是通過這樣的方法實(shí)現(xiàn)可排序的健无。
text類型字段排序問題
1.倒敘排序
GET test/doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
排序結(jié)果
{
"took" : 152,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "wangyang",
"age" : 30,
"desc" : "點(diǎn)在我心內(nèi)的幾首歌"
},
"sort" : [
30
]
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "wangfei",
"age" : 27,
"desc" : "熱天還不讓后人不認(rèn)同"
},
"sort" : [
27
]
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "wangjifei",
"age" : 27,
"desc" : "生活就像 茫茫海上"
},
"sort" : [
27
]
}
]
}
}
2.升序排序
GET test/doc/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
五、ES的分頁查詢
from:從哪開始查 size:返回幾條結(jié)果
GET test/doc/_search
{
"query": {
"match_phrase_prefix": {
"name": "wang"
}
},
"from": 0,
"size": 1
}
查詢結(jié)果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "wangfei",
"age" : 27,
"desc" : "熱天還不讓后人不認(rèn)同"
}
}
]
}
}
六液斜、ES的bool查詢 (must累贤、should)
must (must字段對(duì)應(yīng)的是個(gè)列表,也就是說可以有多個(gè)并列的查詢條件少漆,一個(gè)文檔滿足各個(gè)子條件后才最終返回)
1.must單條件查詢
GET test/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wangfei"
}
}
]
}
}
}
查詢結(jié)果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "wangfei",
"age" : 27,
"desc" : "熱天還不讓后人不認(rèn)同"
}
}
]
}
}
2.must多條件組合查詢
GET test/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wanggfei"
}
},{
"match": {
"age": 25
}
}
]
}
}
}
查詢結(jié)果
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
3.should (只要符合其中一個(gè)條件就返回)
GET test/doc/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "wangjifei"
}
},{
"match": {
"age": 27
}
}
]
}
}
}
查詢結(jié)果
{
"took" : 34,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.287682,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "1",
"_score" : 1.287682,
"_source" : {
"name" : "wangjifei",
"age" : 27,
"desc" : "生活就像 茫茫海上"
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "wangfei",
"age" : 27,
"desc" : "熱天還不讓后人不認(rèn)同"
}
}
]
}
}
4.must_not 顧名思義
GET test/doc/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "wangjifei"
}
},{
"match": {
"age": 27
}
}
]
}
}
}
查詢結(jié)果
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "wangyang",
"age" : 30,
"desc" : "點(diǎn)在我心內(nèi)的幾首歌"
}
}
]
}
}
5.filter(條件過濾查詢臼膏,過濾條件的范圍用range表示gt表示大于、lt表示小于示损、gte表示大于等于渗磅、lte表示小于等于)
GET test/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "wangjifei"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lt": 27
}
}
}
}
}
}
查詢結(jié)果
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
bool查詢總結(jié)
must:與關(guān)系,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 and检访。
should:或關(guān)系始鱼,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 or。
must_not:非關(guān)系脆贵,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 not医清。
filter:過濾條件。
range:條件篩選范圍卖氨。
gt:大于会烙,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 >负懦。
gte:大于等于,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 >=柏腻。
lt:小于密似,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 <葫盼。
lte:小于等于残腌,相當(dāng)于關(guān)系型數(shù)據(jù)庫中的 <=。
七贫导、ES之查詢結(jié)果過濾
準(zhǔn)備數(shù)據(jù)
PUT test3/doc/1
{
"name":"顧老二",
"age":30,
"from": "gu",
"desc": "皮膚黑抛猫、武器長、性格直",
"tags": ["黑", "長", "直"]
}
現(xiàn)在孩灯,在所有的結(jié)果中闺金,我只需要查看name和age兩個(gè)屬性,提高查詢效率
GET test3/doc/_search
{
"query": {
"match": {
"name": "顧"
}
},
"_source": ["name","age"]
}
查詢結(jié)果
{
"took" : 58,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "顧老二",
"age" : 30
}
}
]
}
}
八、ES之查詢結(jié)果高亮顯示
ES的默認(rèn)高亮顯示
GET test3/doc/_search
{
"query": {
"match": {
"name": "顧老二"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
查詢結(jié)果
{
"took" : 216,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "test3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"name" : "顧老二",
"age" : 30,
"from" : "gu",
"desc" : "皮膚黑峰档、武器長败匹、性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"highlight" : {
"name" : [
"<em>顧</em><em>老</em><em>二</em>"
]
}
}
]
}
}
ES自定義高亮顯示(在highlight中,pre_tags用來實(shí)現(xiàn)我們的自定義標(biāo)簽的前半部分讥巡,在這里掀亩,我們也可以為自定義的 標(biāo)簽添加屬性和樣式。post_tags實(shí)現(xiàn)標(biāo)簽的后半部分欢顷,組成一個(gè)完整的標(biāo)簽槽棍。至于標(biāo)簽中的內(nèi)容,則還是交給fields來完成)
GET test3/doc/_search
{
"query": {
"match": {
"desc": "性格直"
}
},
"highlight": {
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"desc": {}
}
}
}
查詢結(jié)果
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "test3",
"_type" : "doc",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"name" : "顧老二",
"age" : 30,
"from" : "gu",
"desc" : "皮膚黑抬驴、武器長炼七、性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"highlight" : {
"desc" : [
"皮膚黑、武器長布持、<b class='key' style='color:red'>性</b><b class='key' style='color:red'>格</b><b class='key' style='color:red'>直</b>"
]
}
}
]
}
}
九豌拙、ES之精確查詢與模糊查詢
term查詢查找包含文檔精確的倒排索引指定的詞條。也就是精確查找题暖。
term和match的區(qū)別是:
match是經(jīng)過analyer的按傅,也就是說,文檔首先被分析器給處理了芙委。根據(jù)不同的分析器逞敷,分析的結(jié)果也稍顯不同,然后再根據(jù)分詞結(jié)果進(jìn)行匹配灌侣。
term則不經(jīng)過分詞推捐,它是直接去倒排索引中查找了精確的值了。
準(zhǔn)備數(shù)據(jù)
PUT w1
{
"mappings": {
"doc": {
"properties":{
"t1":{
"type": "text"
},
"t2": {
"type": "keyword"
}
}
}
}
}
PUT w1/doc/1
{
"t1": "hi single dog",
"t2": "hi single dog"
}
對(duì)比兩者的不同 (結(jié)果就不展示出來了侧啼,只展示結(jié)果的文字?jǐn)⑹?
t1類型為text牛柒,會(huì)經(jīng)過分詞堪簿,match查詢時(shí)條件也會(huì)經(jīng)過分詞,所以下面兩種查詢都能查到結(jié)果
GET w1/doc/_search
{
"query": {
"match": {
"t1": "hi single dog"
}
}
}
GET w1/doc/_search
{
"query": {
"match": {
"t1": "hi"
}
}
}
t2類型為keyword類型皮壁,不會(huì)經(jīng)過分詞椭更,match查詢時(shí)條件會(huì)經(jīng)過分詞,所以只能當(dāng)值為"hi single dog"時(shí)能查詢到
GET w1/doc/_search
{
"query": {
"match": {
"t2": "hi"
}
}
}
GET w1/doc/_search
{
"query": {
"match": {
"t2": "hi single dog"
}
}
}
t1類型為text蛾魄,會(huì)經(jīng)過分詞虑瀑,term查詢時(shí)條件不會(huì)經(jīng)過分詞,所以只有當(dāng)值為"hi"時(shí)能查詢到
GET w1/doc/_search
{
"query": {
"term": {
"t1": "hi single dog"
}
}
}
GET w1/doc/_search
{
"query": {
"term": {
"t1": "hi"
}
}
}
t2類型為keyword類型滴须,不會(huì)經(jīng)過分詞舌狗,term查詢時(shí)條件不會(huì)經(jīng)過分詞,所以只能當(dāng)值為"hi single dog"時(shí)能查詢到
GET w1/doc/_search
{
"query": {
"term": {
"t2": "hi single dog"
}
}
}
GET w1/doc/_search
{
"query": {
"term": {
"t2": "hi"
}
}
}
查找多個(gè)精確值(terms)
第一個(gè)查詢方式
GET test/doc/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"age":27
}
},{
"term":{
"age":28
}
}
]
}
}
}
第二個(gè)查詢方式
GET test/doc/_search
{
"query": {
"terms": {
"age": [
"27",
"28"
]
}
}
}
兩種方式的查詢結(jié)果都是一下結(jié)果
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "wangfei",
"age" : 27,
"desc" : "熱天還不讓后人不認(rèn)同"
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "wangjifei",
"age" : 27,
"desc" : "生活就像 茫茫海上"
}
}
]
}
}
第三個(gè)查詢方式-精確查數(shù)組-or
GET indexname/_search
{
"query": {
"terms": {
"images.keyword": [
"2457741_58feb159-c7a1-4527-8af5-64e87b29b2c8.jpg"扔水,"xxxx" // a or b查詢
]
}
}
}
索引中有個(gè)字段
"images" : [
"2457741_58feb159-c7a1-4527-8af5-64e87b29b2c8.jpg"
],
字段的mapping設(shè)置
"images": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
十痛侍、ES的聚合查詢avg、max魔市、min主届、sum
數(shù)據(jù)準(zhǔn)備
PUT zhifou/doc/1
{
"name":"顧老二",
"age":30,
"from": "gu",
"desc": "皮膚黑、武器長待德、性格直",
"tags": ["黑", "長", "直"]
}
PUT zhifou/doc/2
{
"name":"大娘子",
"age":18,
"from":"sheng",
"desc":"膚白貌美君丁,嬌憨可愛",
"tags":["白", "富","美"]
}
PUT zhifou/doc/3
{
"name":"龍?zhí)灼?,
"age":22,
"from":"gu",
"desc":"mmp,沒怎么看磅网,不知道怎么形容",
"tags":["造數(shù)據(jù)", "真","難"]
}
PUT zhifou/doc/4
{
"name":"石頭",
"age":29,
"from":"gu",
"desc":"粗中有細(xì)谈截,狐假虎威",
"tags":["粗", "大","猛"]
}
PUT zhifou/doc/5
{
"name":"魏行首",
"age":25,
"from":"廣云臺(tái)",
"desc":"仿佛兮若輕云之蔽月,飄飄兮若流風(fēng)之回雪,mmp,最后竟然沒有嫁給顧老二涧偷!",
"tags":["閉月","羞花"]
}
GET zhifou/doc/_search
{
"query": {
"match_all": {}
}
}
1、查詢from是gu的人的平均年齡毙死。
GET zhifou/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}
查詢結(jié)果
{
"took" : 83,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "4",
"_score" : 0.6931472,
"_source" : {
"name" : "石頭",
"age" : 29
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "顧老二",
"age" : 30
}
},
{
"_index" : "zhifou",
"_type" : "doc",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "龍?zhí)灼?,
"age" : 22
}
}
]
},
"aggregations" : {
"my_avg" : {
"value" : 27.0
}
}
}
上例中燎潮,首先匹配查詢from是gu的數(shù)據(jù)。在此基礎(chǔ)上做查詢平均值的操作扼倘,這里就用到了聚合函數(shù)确封,其語法被封裝在aggs中,而my_avg則是為查詢結(jié)果起個(gè)別名再菊,封裝了計(jì)算出的平均值爪喘。那么,要以什么屬性作為條件呢纠拔?是age年齡秉剑,查年齡的什么呢?是avg稠诲,查平均年齡侦鹏。
如果只想看輸出的值诡曙,而不關(guān)心輸出的文檔的話可以通過size=0來控制
GET zhifou/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs":{
"my_avg":{
"avg": {
"field": "age"
}
}
},
"size":0,
"_source":["name","age"]
}
查詢結(jié)果
{
"took" : 35,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"my_avg" : {
"value" : 27.0
}
}
}
2、查詢年齡的最大值
GET zhifou/doc/_search
{
"query": {
"match_all": {}
},
"aggs": {
"my_max": {
"max": {
"field": "age"
}
}
},
"size": 0,
"_source": ["name","age","from"]
}
查詢結(jié)果
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"my_max" : {
"value" : 30.0
}
}
}
3略水、查詢年齡的最小值
GET zhifou/doc/_search
{
"query": {
"match_all": {}
},
"aggs": {
"my_min": {
"min": {
"field": "age"
}
}
},
"size": 0,
"_source": ["name","age","from"]
}
查詢結(jié)果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"my_min" : {
"value" : 18.0
}
}
}
4价卤、查詢符合條件的年齡之和
GET zhifou/doc/_search
{
"query": {
"match": {
"from": "gu"
}
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
},
"size": 0,
"_source": ["name","age","from"]
}
查詢結(jié)果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"my_sum" : {
"value" : 81.0
}
}
}
十一、ES的分組查詢
需求: 要查詢所有人的年齡段渊涝,并且按照1520慎璧,2025,25~30分組,并且算出每組的平均年齡跨释。
GET zhifou/doc/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"age_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 15,
"to": 20
},
{
"from": 20,
"to": 25
},
{
"from": 25,
"to": 30
}
]
}
}
}
}
查詢結(jié)果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"age_group" : {
"buckets" : [
{
"key" : "15.0-20.0",
"from" : 15.0,
"to" : 20.0,
"doc_count" : 1
},
{
"key" : "20.0-25.0",
"from" : 20.0,
"to" : 25.0,
"doc_count" : 1
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 2
}
]
}
}
}
上例中胸私,在aggs的自定義別名age_group中,使用range來做分組煤傍,field是以age為分組盖文,分組使用ranges來做,from和to是范圍
接下來蚯姆,我們就要對(duì)每個(gè)小組內(nèi)的數(shù)據(jù)做平均年齡處理五续。
GET zhifou/doc/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"age_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 15,
"to": 20
},
{
"from": 20,
"to": 25
},
{
"from": 25,
"to": 30
}
]
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}
查詢結(jié)果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"age_group" : {
"buckets" : [
{
"key" : "15.0-20.0",
"from" : 15.0,
"to" : 20.0,
"doc_count" : 1,
"my_avg" : {
"value" : 18.0
}
},
{
"key" : "20.0-25.0",
"from" : 20.0,
"to" : 25.0,
"doc_count" : 1,
"my_avg" : {
"value" : 22.0
}
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 2,
"my_avg" : {
"value" : 27.0
}
}
]
}
}
}
ES的聚合查詢的總結(jié):聚合函數(shù)的使用,一定是先查出結(jié)果龄恋,然后對(duì)結(jié)果使用聚合函數(shù)做處理
avg:求平均
max:最大值
min:最小值
sum:求和
十二疙驾、ES之Mappings
GET test
查詢結(jié)果
{
"test" : {
"aliases" : { },
"mappings" : {
"doc" : {
"properties" : {
"age" : {
"type" : "long"
},
"desc" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1569133097594",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "AztO9waYQiyHvzP6dlk4tA",
"version" : {
"created" : "6080299"
},
"provided_name" : "test"
}
}
}
}
由返回結(jié)果可以看到,分為兩大部分:
第一部分關(guān)于t1索引類型相關(guān)的郭毕,包括該索引是否有別名aliases它碎,然后就是mappings信息,
包括索引類型doc显押,各字段的詳細(xì)映射關(guān)系都收集在properties中扳肛。
另一部分是關(guān)于索引t1的settings設(shè)置。包括該索引的創(chuàng)建時(shí)間乘碑,主副分片的信息挖息,UUID等等。
- mappings 是什么兽肤?
- 映射就是在創(chuàng)建索引的時(shí)候套腹,有更多定制的內(nèi)容,更加的貼合業(yè)務(wù)場景资铡。
- 用來定義一個(gè)文檔及其包含的字段如何存儲(chǔ)和索引的過程电禀。
- 字段的數(shù)據(jù)類型
- 簡單類型如文本(text)、關(guān)鍵字(keyword)笤休、日期(data)尖飞、整形(long)、雙精度
(double)、布爾(boolean)或ip葫松。 - 可以是支持JSON的層次結(jié)構(gòu)性質(zhì)的類型瓦糕,如對(duì)象或嵌套∫该矗或者一種特殊類型咕娄,如geo_point、geo_shape或completion珊擂。為了不同的目的圣勒,
以不同的方式索引相同的字段通常是有用的。例如摧扇,字符串字段可以作為全文搜索的文本字段進(jìn)行索引圣贸,
也可以作為排序或聚合的關(guān)鍵字字段進(jìn)行索引】富或者吁峻,可以使用標(biāo)準(zhǔn)分析器、英語分析器和
法語分析器索引字符串字段在张。這就是多字段的目的用含。大多數(shù)數(shù)據(jù)類型通過fields參數(shù)支持多字段。
一個(gè)簡單的映射示例
PUT mapping_test { "mappings": { "test1":{ "properties":{ "name":{"type": "text"}, "age":{"type":"long"} } } } }
- 簡單類型如文本(text)、關(guān)鍵字(keyword)笤休、日期(data)尖飞、整形(long)、雙精度
我們?cè)趧?chuàng)建索引PUT mapping_test1的過程中帮匾,為該索引定制化類型(設(shè)計(jì)表結(jié)構(gòu))啄骇,添加一個(gè)映射類型test1;指定字段或者屬性都在properties內(nèi)完成瘟斜。
GET mapping_test
查詢結(jié)果
{
"mapping_test" : {
"aliases" : { },
"mappings" : {
"test1" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1570794586526",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "P4-trriPTxq-nJj89iYXZA",
"version" : {
"created" : "6080299"
},
"provided_name" : "mapping_test"
}
}
}
}
返回的結(jié)果中你肯定很熟悉缸夹!映射類型是test1,具體的屬性都被封裝在properties中螺句。
3. ES mappings之dynamic的三種狀態(tài)
一般的虽惭,mapping則又可以分為動(dòng)態(tài)映射(dynamic mapping)和靜態(tài)(顯示)映射(explicit mapping)和精確(嚴(yán)格)映射(strict mappings),具體由dynamic屬性控制蛇尚。默認(rèn)為動(dòng)態(tài)映射
默認(rèn)為動(dòng)態(tài)映射
PUT test4
{
"mappings": {
"doc":{
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
}
}
}
}
}
GET test4/_mapping
查詢結(jié)果
{
"test4" : {
"mappings" : {
"doc" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text"
},
"sex" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
添加數(shù)據(jù)
PUT test4/doc/1
{
"name":"wangjifei",
"age":"18",
"sex":"不詳"
}
查看數(shù)據(jù)
GET test4/doc/_search
{
"query": {
"match_all": {}
}
}
查詢結(jié)果
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test4",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "wangjifei",
"age" : "18",
"sex" : "不詳"
}
}
]
}
}
測試靜態(tài)映射:當(dāng)elasticsearch察覺到有新增字段時(shí)趟妥,因?yàn)閐ynamic:false的關(guān)系,會(huì)忽略該字段佣蓉,但是仍會(huì)存儲(chǔ)該字段。
創(chuàng)建靜態(tài)mapping
PUT test5
{
"mappings": {
"doc":{
"dynamic":false,
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
}
}
}
}
}
插入數(shù)據(jù)
PUT test5/doc/1
{
"name":"wangjifei",
"age":"18",
"sex":"不詳"
}
條件查詢
GET test5/doc/_search
{
"query": {
"match": {
"sex": "不詳"
}
}
}
查詢結(jié)果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
查看所有數(shù)據(jù)
GET /test5/doc/_search
{
"query": {
"match_all": {}
}
}
查詢結(jié)果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test5",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "wangjifei",
"age" : "18",
"sex" : "不詳"
}
}
]
}
}
測試嚴(yán)格映射:當(dāng)elasticsearch察覺到有新增字段時(shí)亲雪,因?yàn)閐ynamic:strict 的關(guān)系勇凭,就會(huì)報(bào)錯(cuò),不能插入成功义辕。
創(chuàng)建嚴(yán)格mapping
PUT test6
{
"mappings": {
"doc":{
"dynamic":"strict",
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
}
}
}
}
}
插入數(shù)據(jù)
PUT test6/doc/1
{
"name":"wangjifei",
"age":"18",
"sex":"不詳"
}
插入結(jié)果
{
"error": {
"root_cause": [
{
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [sex] within [doc] is not allowed"
}
],
"type": "strict_dynamic_mapping_exception",
"reason": "mapping set to strict, dynamic introduction of [sex] within [doc] is not allowed"
},
"status": 400
}
小結(jié):
動(dòng)態(tài)映射(dynamic:true):動(dòng)態(tài)添加新的字段(或缺氏罕辍)。
靜態(tài)映射(dynamic:false):忽略新的字段灌砖。在原有的映射基礎(chǔ)上璧函,當(dāng)有新的字段時(shí)傀蚌,不會(huì)主動(dòng)的添加新的映射關(guān)系,只作為查詢結(jié)果出現(xiàn)在查詢中蘸吓。
嚴(yán)格模式(dynamic:strict):如果遇到新的字段善炫,就拋出異常。一般靜態(tài)映射用的較多库继。就像HTML的img標(biāo)簽一樣箩艺,src為自帶的屬性,你可以在需要的時(shí)候添加id或者class屬性宪萄。當(dāng)然艺谆,如果你非常非常了解你的數(shù)據(jù),并且未來很長一段時(shí)間不會(huì)改變拜英,strict不失為一個(gè)好選擇静汤。
4. ES之mappings的 index 屬性
index屬性默認(rèn)為true,如果該屬性設(shè)置為false居凶,那么虫给,elasticsearch不會(huì)為該屬性創(chuàng)建索引,也就是說無法當(dāng)做主查詢條件排监。
PUT test7
{
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
"index": true
},
"age": {
"type": "long",
"index": false
}
}
}
}
}
插入數(shù)據(jù)
PUT test7/doc/1
{
"name":"wangjifei",
"age":18
}
條件查詢數(shù)據(jù)
GET test7/doc/_search
{
"query": {
"match": {
"name": "wangjifei"
}
}
}
查詢結(jié)果
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test7",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "wangjifei",
"age" : 18
}
}
]
}
}
條件查詢
GET test7/doc/_search
{
"query": {
"match": {
"age": 18
}
}
}
查詢結(jié)果
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"match\" : {\n \"age\" : {\n \"query\" : 18,\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "fzN9frSZRy2OzinRjeMKGA",
"index": "test7"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test7",
"node": "INueKtviRpO1dbNWngcjJA",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"match\" : {\n \"age\" : {\n \"query\" : 18,\n \"operator\" : \"OR\",\n \"prefix_length\" : 0,\n \"max_expansions\" : 50,\n \"fuzzy_transpositions\" : true,\n \"lenient\" : false,\n \"zero_terms_query\" : \"NONE\",\n \"auto_generate_synonyms_phrase_query\" : true,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "fzN9frSZRy2OzinRjeMKGA",
"index": "test7",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [age] since it is not indexed."
}
}
}
]
},
"status": 400
}
5. ES 之 mappings 的copy_to屬性
PUT test8
{
"mappings": {
"doc": {
"dynamic":false,
"properties": {
"first_name":{
"type": "text",
"copy_to": "full_name"
},
"last_name": {
"type": "text",
"copy_to": "full_name"
},
"full_name": {
"type": "text"
}
}
}
}
}
插入數(shù)據(jù)
PUT test8/doc/1
{
"first_name":"tom",
"last_name":"ben"
}
PUT test8/doc/2
{
"first_name":"john",
"last_name":"smith"
}
查詢所有
GET test8/doc/_search
{
"query": {
"match_all": {}
}
}
查詢結(jié)果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test8",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "john",
"last_name" : "smith"
}
},
{
"_index" : "test8",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
條件查詢
GET test8/doc/_search
{
"query": {
"match": {
"first_name": "tom"
}
}
}
查詢結(jié)果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test8",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
條件查詢
GET test8/doc/_search
{
"query": {
"match": {
"full_name": "ben"
}
}
}
查詢結(jié)果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test8",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
上例中狰右,我們將first_name和last_name都復(fù)制到full_name中。并且使用full_name查詢也返回了結(jié)果
既要查詢tom還要查詢smith該怎么辦舆床?
GET test8/doc/_search
{
"query": {
"match": {
"full_name": {
"query": "tom smith",
"operator": "or"
}
}
}
}
查詢結(jié)果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test8",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "john",
"last_name" : "smith"
}
},
{
"_index" : "test8",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
operator參數(shù)為多個(gè)條件的查詢關(guān)系也可以是and
上面的查詢還可以簡寫成一下:
GET test8/doc/_search
{
"query": {
"match": {
"full_name": "tom smith"
}
}
}
copy_to還支持將相同的屬性值復(fù)制給不同的字段棋蚌。
PUT test9
{
"mappings": {
"doc": {
"dynamic":false,
"properties": {
"first_name":{
"type": "text",
"copy_to": ["full_name1","full_name2"]
},
"last_name": {
"type": "text",
"copy_to": ["full_name1","full_name2"]
},
"full_name1": {
"type": "text"
},
"full_name2":{
"type":"text"
}
}
}
}
}
插入數(shù)據(jù)
PUT test9/doc/1
{
"first_name":"tom",
"last_name":"ben"
}
PUT test9/doc/2
{
"first_name":"john",
"last_name":"smith"
}
條件查詢
GET test9/doc/_search
{
"query": {
"match": {
"full_name1": "tom smith"
}
}
}
查詢結(jié)果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test9",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "john",
"last_name" : "smith"
}
},
{
"_index" : "test9",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
條件查詢
GET test9/doc/_search
{
"query": {
"match": {
"full_name2": "tom smith"
}
}
}
查詢結(jié)果
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test9",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "john",
"last_name" : "smith"
}
},
{
"_index" : "test9",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "tom",
"last_name" : "ben"
}
}
]
}
}
full_name1 full_name2兩個(gè)字段都可以查出來
6. ES 之mappings的對(duì)象屬性
首先先看看ES自動(dòng)創(chuàng)建的mappings
PUT test10/doc/1
{
"name":"wangjifei",
"age":18,
"info":{
"addr":"北京",
"tel":"18500327026"
}
}
GET test10
查詢結(jié)果
{
"test10" : {
"aliases" : { },
"mappings" : {
"doc" : {
"properties" : {
"age" : {
"type" : "long"
},
"info" : {
"properties" : {
"addr" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tel" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1570975011394",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "YvMGDHxkSri0Lgx6GGXiNw",
"version" : {
"created" : "6080299"
},
"provided_name" : "test10"
}
}
}
}
現(xiàn)在如果要以info中的tel為條件怎么寫查詢語句呢?
GET test10/doc/_search
{
"query": {
"match": {
"info.tel": "18500327026"
}
}
}
查詢結(jié)果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test10",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "wangjifei",
"age" : 18,
"info" : {
"addr" : "北京",
"tel" : "18500327026"
}
}
}
]
}
}
info既是一個(gè)屬性挨队,也是一個(gè)對(duì)象谷暮,我們稱為info這類字段為對(duì)象型字段。該對(duì)象內(nèi)又包含addr和tel兩個(gè)字段盛垦,如上例這種以嵌套內(nèi)的字段為查詢條件的話湿弦,查詢語句可以以字段點(diǎn)子字段的方式來寫即可
7. ES之mappings的settings 設(shè)置
在創(chuàng)建一個(gè)索引的時(shí)候,我們可以在settings中指定分片信息:
PUT test11
{
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text"
}
}
}
},
"settings": {
"number_of_replicas": 1,
"number_of_shards": 5
}
}
number_of_shards是主分片數(shù)量(每個(gè)索引默認(rèn)5個(gè)主分片)腾夯,而number_of_replicas是復(fù)制分片颊埃,默認(rèn)一個(gè)主分片搭配一個(gè)復(fù)制分片。
8. ES 之mappings的ignore_above參數(shù)
ignore_above參數(shù)僅針對(duì)于keyword類型有用
這樣設(shè)置是會(huì)報(bào)錯(cuò)的
PUT test12
{
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
"ignore_above":5
}
}
}
}
}
顯示結(jié)果
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "Mapping definition for [name] has unsupported parameters: [ignore_above : 5]"
}
],
"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [doc]: Mapping definition for [name] has unsupported parameters: [ignore_above : 5]",
"caused_by": {
"type": "mapper_parsing_exception",
"reason": "Mapping definition for [name] has unsupported parameters: [ignore_above : 5]"
}
},
"status": 400
}
正確的打開方式
PUT test12
{
"mappings": {
"doc": {
"properties": {
"name": {
"type": "keyword",
"ignore_above":5
}
}
}
}
}
PUT test12/doc/1
{
"name":"wangjifei"
}
這樣查詢能查出結(jié)果
GET test12/doc/_search
{
"query": {
"match_all": {}
}
}
查詢結(jié)果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test12",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "wangjifei"
}
}
]
}
}
這樣查詢不能查詢出結(jié)果
GET test12/doc/_search
{
"query": {
"match": {
"name": "wangjifei"
}
}
}
查詢結(jié)果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
上面的例子證明超過ignore_above設(shè)定的值后會(huì)被存儲(chǔ)但不會(huì)建立索引
那么如果字符串的類型是text時(shí)能用ignore_above嗎蝶俱,答案是能班利,但要特殊設(shè)置:
PUT test13
{
"mappings": {
"doc":{
"properties":{
"name1":{
"type":"keyword",
"ignore_above":5
},
"name2":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above": 10
}
}
}
}
}
}
}
PUT test13/doc/1
{
"name1":"wangfei",
"name2":"wangjifei hello"
}
能查出來
GET test13/doc/_search
{
"query": {
"match_all": {}
}
}
查詢結(jié)果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "test13",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name1" : "wangfei",
"name2" : "wangjifei hello"
}
}
]
}
}
通過name1 字段查不出來,因?yàn)樵O(shè)置的是keyword類型 限制了5個(gè)字符的長度榨呆,
存儲(chǔ)的值超過了最大限制
GET test13/doc/_search
{
"query": {
"match": {
"name1": "wangfei"
}
}
}
查詢結(jié)果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
通過name2 字段能查出來罗标,雖然限制了5個(gè)字符的長度,存儲(chǔ)的值超過了最大限制,
但是闯割,字段類型設(shè)置為text之后彻消,ignore_above參數(shù)的限制就失效了。(了解就好宙拉,意義不大)
GET test13/doc/_search
{
"query": {
"match": {
"name2": "wangjifei"
}
}
}
查詢結(jié)果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "test13",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name1" : "wangfei",
"name2" : "wangjifei hello"
}
}
]
}
}