国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

elasticsearch的常用查詢語法(大全)

這篇具有很好參考價值的文章主要介紹了elasticsearch的常用查詢語法(大全)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

參考文章網(wǎng)址:https://www.cnblogs.com/xiohao/p/12970224.html
es查詢

1. 簡單的增刪改查

1.1. 創(chuàng)建一篇文檔(type)(有則修改,無則創(chuàng)建)

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":"點在我心內(nèi)的幾首歌"
}

1.2. 查詢指定索引信息

 GET test

1.3. 查詢指定文檔(type)信息

GET test/doc/1
GET test/doc/2

1.4. 查詢對應(yīng)索引下所有的數(shù)據(jù)

GET test/doc/_search
或
GET test/doc/_search
{
  "query": {
    "match_all": {}
  }
}

1.5. 刪除指定文檔(type)

DELETE test/doc/3

1.6. 刪除索引

DELETE test 

1.7. 修改指定文檔方式

修改時,不指定的屬性會自動覆蓋,只保留指定的屬性(不正確的修改指定文檔方式)

PUT test/doc/1
{
  "name":"王計飛"
}

使用POST命令,在id后面跟_update,要修改的內(nèi)容放到doc文檔(屬性)中(正確的修改指定文檔方式)

POST test/doc/1/_update
{
  "doc":{
    "desc":"生活就像 茫茫海上"
  }
}

2. ES查詢的兩種方式

2.1. 查詢字符串搜索

GET test/doc/_search?q=name:wangfei

2.2. 結(jié)構(gòu)化查詢(單字段查詢,不能多字段組合查詢)

GET test/doc/_search
{
	"query":{
		"match":{
			"name":"wangfei"
		}
	}
}

3. match系列操作

3.1. match_all(查詢?nèi)浚?/h3>
GET test/doc/_search
{
  "query": {
    "match_all": {}
  }
}

3.2. match_phrase (短語查詢)

準(zhǔn)備數(shù)據(jù)

PUT test1/doc/1
{
  "title": "中國是世界上人口最多的國家"
}
PUT test1/doc/2
{
  "title": "美國是世界上軍事實力最強(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" : "美國是世界上軍事實力最強(qiáng)大的國家"
        }
      }
    ]
  }
}

通過觀察結(jié)果可以發(fā)現(xiàn),雖然如期的返回了中國的文檔。但是卻把和美國的文檔也返回了,這并不是我們想要的。
因為這是elasticsearch在內(nèi)部對文檔做分詞的時候,對于中文來說,就是一個字一個字分的。
所以,我們搜中國,中和國都符合條件,返回,而美國的國也符合。而我們認(rèn)為中國是個短語,是一個有具體含義的詞。所以elasticsearch在處理中文分詞方面比較弱勢。后面會講針對中文的插件。
但目前我們還有辦法解決,那就是使用短語查詢
用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" : "北京是中國的首都"
        }
      }
    ]
  }
}

我們搜索中國和世界這兩個指定詞組時,但又不清楚兩個詞組之間有多少別的詞間隔。那么在搜的時候就要留有一些余地。這時就要用到了slop了。相當(dāng)于正則中的中國.*?世界。這個間隔默認(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.3. 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ù)理解 前綴查詢會非常的影響性能,要對結(jié)果集進(jìn)行限制,就加上這個參數(shù)。

GET test2/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "bea",
        "max_expansions":1
      }
    }
  }
}

3.4. multi_match(多字段查詢)

multi_match是要在多個字段中查詢同一個關(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"
        }
      }
    ]
  }
}
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 時 等同于 短語查詢

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" : "北京是中國的首都"
        }
      }
    ]
  }
}

match 查詢相關(guān)總結(jié)
1、match:返回所有匹配的分詞。
2、match_all:查詢?nèi)俊?br> 3、match_phrase:短語查詢,在match的基礎(chǔ)上進(jìn)一步查詢詞組,可以指定slop分詞間隔。
4、match_phrase_prefix:前綴查詢,根據(jù)短語中最后一個詞組做前綴匹配,可以應(yīng)用于搜索提示,但注意和max_expanions搭配。其實默認(rèn)是50
5、multi_match:多字段查詢,使用相當(dāng)?shù)撵`活,可以完成match_phrase和match_phrase_prefix的工作。

4. ES的排序查詢

es6.8.4版本中,需要分詞的字段不可以直接排序,比如:text類型,如果想要對這類字段進(jìn)行排序,需要特別設(shè)置:對字段索引兩次,一次索引分詞(用于搜索)一次索引不分詞(用于排序),es默認(rèn)生成的text類型字段就是通過這樣的方法實現(xiàn)可排序的。

降序排序

GET test/doc/_search
{
	"query":{
		"match_all":{}
	},
	"sort":[
		{
			"age":{
				"order":"desc"
			}
		}
	]
}

升序排序

GET test/doc/_search
{
	"query":{
		"match_all":{}
	},
	"sort":[
		{
			"age":{
				"order":"asc"
			}
		}
	]
}

5. 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)同"
        }
      }
    ]
  }
}

6 bool查詢(must,should)

6.1. must查詢

must字段對應(yīng)的是個列表,也就是說可以有多個并列查詢的條件,滿足所有條件的結(jié)果才返回

單條件

GET test/doc/_search
{
	"query":{
		"bool":{
			"must":[
				{
					"match":{
						"name":"wangfei"
					}
				}
			]
		}
	}
}

多條件查詢

GET test/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "wanggfei"
          }
        },{
          "match": {
            "age": 25
          }
        }
      ]
    }
  }
}

6.2. should (只要符合其中一個條件就返回)

GET test/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
          "name": "wangjifei"
        }
        },{
          "match": {
            "age": 27
          }
        }
      ]
    }
  }
}

6.3. must_not 查詢

must_not 文檔必須不 匹配這些條件才能被包含進(jìn)來,就是條件都不滿足的才返回

GET test/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "wangjifei"
          }
        },{
          "match": {
            "age": 27
          }
        }
      ]
    }
  }
}

6.4. filter字段過濾查詢

(條件過濾查詢,過濾條件的范圍用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)

GET test/doc/_search
{
	"query:{
		"bool":{
			"must":[
				{
					"match":{
						"name":"wangjifei"
					}
				}
			],
			"filter":{
				"range":{
					"age":{
						"gte":10,
						"lt":27
					}
				}
			}
		}
	}
}

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ù)庫中的 <=

7. 查詢結(jié)果過濾

準(zhǔn)備數(shù)據(jù)

PUT test3/doc/1
{
  "name":"顧老二",
  "age":30,
  "from": "gu",
  "desc": "皮膚黑、武器長、性格直",
  "tags": ["黑", "長", "直"]
}

字段過濾,如只需查看name和age兩個屬性

GET test3/doc/_search
{
	"query":{
		"match":{
			"name":"顧老二"
		}
	}
	"_source":["name","age"]
}

8. 查詢結(jié)果高亮顯示

GET test3/doc/_search
{
  "query": {
    "match": {
      "name": "顧老二"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

ES自定義高亮顯示(在highlight中,pre_tags用來實現(xiàn)我們的自定義標(biāo)簽的前半部分,在這里,我們也可以為自定義的 標(biāo)簽添加屬性和樣式。post_tags實現(xiàn)標(biāo)簽的后半部分,組成一個完整的標(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": {}
    }
  }
}

9. 精確查詢與模糊查詢

9.1. term查詢查找包含文檔精確的倒排索引指定的詞條。也就是精確查找。

term和match的區(qū)別是:
match是經(jīng)過analyer的,也就是說,文檔首先被分析器給處理了。根據(jù)不同的分析器,然后再根據(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"
}

t1類型為text,會經(jīng)過分詞。match查詢時條件也會經(jīng)過分詞,所以下面兩種查詢都能查到結(jié)果

GET w1/doc/_search
{
	"query":{
		"match":{
			"t1":"hi single dog"
		}
	}
}

只要t1查詢條件中包含了"hi",“single”,“dog"分詞的就可以,比如"hi dog”,“dog hi”,“hi 123145”,"hi doc"都能查到結(jié)果

GET w1/doc/_search
{
	"query":{
		"match":{
			"t1":"h1"
		}
	}
}

t2類型為keyword類型,不會經(jīng)過分詞,match查詢時條件會經(jīng)過分詞,所以只能當(dāng)值為"hi single dog"時能查詢到

GET w1/doc/_search
{
	"query":{
		"match":{
			"t2":"hi"
		}
	}
}

無結(jié)果

GET w1/doc/_search
{
	"query":{
		"match":{
			"t2":"hi single dog"
		}
	}
}

t1類型為text,會經(jīng)過分詞分為"hi",“single”,“dog”,term查詢時條件不會經(jīng)過分詞,所以只有當(dāng)值為"hi",“single”,“dog”,"hi single dog"四種情況時才能查詢到結(jié)果

GET w1/doc/_search
{
  "query": {
    "term": {
      "t1": "hi single dog" 
    }
  }
}
GET w1/doc/_search
{
  "query": {
    "term": {
      "t1": "hi" 
    }
  }
}

t2類型為keyword類型,不會經(jīng)過分詞,term查詢時條件不會經(jīng)過分詞,所以只能當(dāng)值為"hi single dog"時能查詢到

GET w1/doc/_search
{
  "query": {
    "term": {
      "t2": "hi single dog" 
    }
  }
}
GET w1/doc/_search
{
  "query": {
    "term": {
      "t2": "hi" 
    }
  }
}

無結(jié)果

9.2. 查詢多個精確值 多個查詢用terms,單個用term

GET test/doc/_search
{
	"query":{
		"terms":{
			"age":[
				"27",
				"28"
			]
		}
	}
}

也可以與bool中should共同使用
查詢age為27 || 28,或者name為wangyang || wangfei

GET test/_search
{
  "query": {
    "bool": {
      "should": [
        {"terms": {
          "age": [
              "27","28"
            ]
        }
        },
        {
          "terms": {
            "name": [
              "wangyang","wangfei"
              ]
          }
        }
      ]
    }
  }
}

也可以與bool中must共同使用
查詢age為27 || 28,且 name為wangyang || wangfei

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {"terms": {
          "age": [
              "27","28"
            ]
        }
        },
        {
          "terms": {
            "name": [
              "wangyang","wangfei"
              ]
          }
        }
      ]
    }
  }
}

10. 聚合查詢 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":"廣云臺",
  "desc":"仿佛兮若輕云之蔽月,飄飄兮若流風(fēng)之回雪,mmp,最后竟然沒有嫁給顧老二!",
  "tags":["閉月","羞花"]
}

10.1. avg 平均值查詢

查詢form為gu的人的平均年齡

GET zhifou/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

上例中,首先匹配查詢from是gu的數(shù)據(jù)。在此基礎(chǔ)上做查詢平均值的操作,這里就用到了聚合函數(shù),其語法被封裝在aggs中,而my_avg則是為查詢結(jié)果起個別名,封裝了計算出的平均值。那么,要以什么屬性作為條件呢?是age年齡,查年齡的什么呢?是avg,查平均年齡。
如果只想看輸出的值,而不關(guān)心輸出的文檔的話可以通過size=0來控制

GET zhifou/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
}

10.2 max最大查詢

查詢年齡的最大值

GET zhifou/_search
{
  "aggs": {
    "my_max": {
      "max": {
        "field": "age"
      }
    }
  },
  "size": 0
}

10.3 min最小值查詢

查詢年齡的最小值

GET zhifou/_search
{
  "aggs": {
    "my_min": {
      "min": {
        "field": "age"
      }
    }
  },
  "size": 0
}

10.4 sum求和查詢

查詢符合條件的年齡之和

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_sum": {
      "sum": {
        "field": "age"
      }
    }
  },
  "size": 0
}

“size”:x
表示返回結(jié)果數(shù)據(jù)多少條,相當(dāng)于limit。

ES的聚合查詢的總結(jié):聚合函數(shù)的使用,一定是先查出結(jié)果,然后對結(jié)果使用聚合函數(shù)做處理
avg:求平均
max:最大值
min:最小值
sum:求和

11. 分組查詢

查詢所有人的年齡段,并且按照15-20,20-25,25-30分組。

GET zhifou/_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的自定義別名age_group中,使用range來做分組,field是以age為分組,分組使用ranges來做,from和to是范圍. from 表示的是大于等于,to表示的是小于

查詢所有人的年齡段,并且按照15-20,20-25,25-30分組,并計算出每組的平均年齡。

GET zhifou/_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": {
        "myavg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

12. 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)建時間,主副分片的信息,UUID等等。

12.1. mappings 是什么?

映射就是在創(chuàng)建索引的時候,有更多定制的內(nèi)容,更加的貼合業(yè)務(wù)場景。
用來定義一個文檔及其包含的字段如何存儲和索引的過程。

12.2. 字段的數(shù)據(jù)類型

簡單類型如文本(text)、關(guān)鍵字(keyword)、日期(data)、整形(long)、雙精度
(double)、布爾(boolean)或ip。 可以是支持JSON的層次結(jié)構(gòu)性質(zhì)的類型,如對象或嵌套。
或者一種特殊類型,如geo_point、geo_shape或completion。為了不同的目的,
以不同的方式索引相同的字段通常是有用的。例如,字符串字段可以作為全文搜索的文本字段進(jìn)行索引,
也可以作為排序或聚合的關(guān)鍵字字段進(jìn)行索引?;蛘?,可以使用標(biāo)準(zhǔn)分析器、英語分析器和
法語分析器索引字符串字段。這就是多字段的目的。大多數(shù)數(shù)據(jù)類型通過fields參數(shù)支持多字段。
一個簡單的映射示例

PUT mapping_test
{
  "mappings": {
    "test1":{
      "properties":{
        "name":{"type": "text"},
        "age":{"type":"long"}
      }
    }
  }
}

我們在創(chuàng)建索引PUT mapping_test1的過程中,為該索引定制化類型(設(shè)計表結(jié)構(gòu)),添加一個映射類型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中。

12.3. ES mappings之dynamic的三種狀態(tài)

一般的,mapping則又可以分為動態(tài)映射(dynamic mapping)和靜態(tài)(顯示)映射(explicit mapping)和精確(嚴(yán)格)映射(strict mappings),具體由dynamic屬性控制。默認(rèn)為動態(tài)映射
默認(rèn)為動態(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察覺到有新增字段時,因為dynamic:false的關(guān)系,會忽略該字段,但是仍會存儲該字段。

創(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察覺到有新增字段時,因為dynamic:strict 的關(guān)系,就會報錯,不能插入成功。

創(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é): 動態(tài)映射(dynamic:true):動態(tài)添加新的字段(或缺?。?。 靜態(tài)映射(dynamic:false):忽略新的字段。在原有的映射基礎(chǔ)上,當(dāng)有新的字段時,不會主動的添加新的映射關(guān)系,只作為查詢結(jié)果出現(xiàn)在查詢中。 嚴(yán)格模式(dynamic:strict):如果遇到新的字段,就拋出異常。一般靜態(tài)映射用的較多。就像HTML的img標(biāo)簽一樣,src為自帶的屬性,你可以在需要的時候添加id或者class屬性。當(dāng)然,如果你非常非常了解你的數(shù)據(jù),并且未來很長一段時間不會改變,strict不失為一個好選擇。

12.4. ES之mappings的 index 屬性

index屬性默認(rèn)為true,如果該屬性設(shè)置為false,那么,elasticsearch不會為該屬性創(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
}

12.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ù)為多個條件的查詢關(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兩個字段都可以查出來

12.6. ES 之mappings的對象屬性

首先先看看ES自動創(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既是一個屬性,也是一個對象,我們稱為info這類字段為對象型字段。該對象內(nèi)又包含addr和tel兩個字段,如上例這種以嵌套內(nèi)的字段為查詢條件的話,查詢語句可以以字段點子字段的方式來寫即可

12.7. ES之mappings的settings 設(shè)置

在創(chuàng)建一個索引的時候,我們可以在settings中指定分片信息:

PUT test11
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }, 
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 5
  }
}

number_of_shards是主分片數(shù)量(每個索引默認(rèn)5個主分片),而number_of_replicas是復(fù)制分片,默認(rèn)一個主分片搭配一個復(fù)制分片。

12.8. ES 之mappings的ignore_above參數(shù)

ignore_above參數(shù)僅針對于keyword類型有用
這樣設(shè)置是會報錯的

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è)定的值后會被存儲但不會建立索引
那么如果字符串的類型是text時能用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 字段查不出來,因為設(shè)置的是keyword類型 限制了5個字符的長度,
存儲的值超過了最大限制

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個字符的長度,存儲的值超過了最大限制,但是,
當(dāng)字段類型設(shè)置為text之后,ignore_above參數(shù)的限制就失效了。(了解就好,意義不大)

GET test13/doc/_search
{
  "query": {
    "match": {
      "name2": "wangjifei"
    }
  }
}

查詢結(jié)果文章來源地址http://www.zghlxwxcb.cn/news/detail-754511.html

{
  "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"
        }
      }
    ]
  }
}

到了這里,關(guān)于elasticsearch的常用查詢語法(大全)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Springboot 整合ElasticSearch 常用的插入查詢,模糊查詢,范圍查詢

    Springboot 整合ElasticSearch 常用的插入查詢,模糊查詢,范圍查詢

    repository.deleteById(id); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } public Dog saveDog(Dog dog) { try { Dog save = repository.save(dog); System.out.println(“結(jié)果:”+save.toString()); return save; } catch (Exception ex) { ex.printStackTrace(); return null; } } public Boolean saveDogAll(List dogsList) { try { reposit

    2024年04月22日
    瀏覽(22)
  • 【ElasticSearch】ElasticSearch常用查詢api集合(一)

    【ElasticSearch】ElasticSearch常用查詢api集合(一)

    本文為es常見DSL搜索入門帖子 開始之前先貼個對應(yīng)關(guān)系,方便各位理解 在es7+的版本中,是沒有類型的概念的,所以,添加數(shù)據(jù)直接在索引中添加; ?請求es地址均為 localhost:9200/{索引}/_search ,為了編寫方便些,在下面的例子中會直接寫請求體; bool : 用來組合多個條件

    2024年02月11日
    瀏覽(52)
  • 常用的Elasticsearch查詢DSL

    1.基本查詢 2.多條件查詢 3.范圍查詢 4.復(fù)雜查詢 5.聚合查詢

    2024年02月12日
    瀏覽(27)
  • ElasticSearch常用查詢命令查看基本信息

    查看es的集群狀態(tài) http://IP:9200/_cat/healthv 注: IP指es集群某個ip地址, v表示格式化輸出 查看es集群節(jié)點列表 http://IP:9200/_cat/nodesv 查看es集群所有索引及數(shù)據(jù)大小 http://IP:9200/_cat/indicesv 查看單個索引結(jié)構(gòu)信息 http://IP:9200/indexNamepretty=true 注: indexName是索引名字, pretty=true表示格式化輸出

    2024年02月03日
    瀏覽(24)
  • 【微服務(wù)】Elasticsearch常用查詢&結(jié)果處理(三)

    【微服務(wù)】Elasticsearch常用查詢&結(jié)果處理(三)

    ??Es學(xué)習(xí)·第三站~ ??Es學(xué)習(xí)起始站:【微服務(wù)】Elasticsearch概述環(huán)境搭建(一) ??本文已收錄至專欄:微服務(wù)探索之旅 ??希望您能有所收獲 在上一站的學(xué)習(xí)中,我們已經(jīng)導(dǎo)入了大量數(shù)據(jù)到es中,實現(xiàn)了數(shù)據(jù)存儲功能。接下來如需看自己實操效果請根據(jù)上一站的 三.環(huán)境搭建 部

    2024年02月04日
    瀏覽(21)
  • Elasticsearch 常用的增加、刪除、修改、查詢語句

    新增 刪除 修改 批量增刪改 查詢 查詢指定索引下全部數(shù)據(jù) 查詢指定索引下指定id的數(shù)據(jù) 查詢指定索引下全部數(shù)據(jù) 查詢指定值(match) 在多個字段中查詢指定值(multi_match) match、multi_match是模糊匹配,匹配時會對所查找的進(jìn)行分詞,然后按分詞匹配查找。 term 查詢

    2024年02月03日
    瀏覽(32)
  • 【ElasticSearch】ElasticSearch Java API的使用——常用索引、文檔、查詢操作(二)

    Elaticsearch ,簡稱為es,es是一個開源的 高擴(kuò)展 的 分布式全文檢索引擎 ,它可以近乎 實時的存儲 、 檢索數(shù)據(jù); 本身擴(kuò)展性很好,可以擴(kuò)展到上百臺服務(wù)器,處理PB級別(大數(shù)據(jù)時代)的數(shù)據(jù)。es也使用java開發(fā)并使用Lucene作為其核心來實現(xiàn)所有索引和搜索的功能,但是它的 目的

    2024年01月16日
    瀏覽(124)
  • Elasticsearch基礎(chǔ)學(xué)習(xí)-常用查詢和基本的JavaAPI操作ES

    Elasticsearch基礎(chǔ)學(xué)習(xí)-常用查詢和基本的JavaAPI操作ES

    Elasticsearch是一個基于 lucene 、分布式、通過Restful方式進(jìn)行交互的 近實時搜索 平臺框架。 ELK技術(shù)棧是Elasticsearch、Logstash、Kibana三大開元框架首字母大寫簡稱。 而Elasticsearch 是一個 開源的高擴(kuò)展的分布式全文搜索引擎 , 是整個 ELK技術(shù)棧的核心。 Elasticsearch是一個基于lucene、分

    2024年02月11日
    瀏覽(20)
  • DB SQL 轉(zhuǎn) ES DSL(支持多種數(shù)據(jù)庫常用查詢、統(tǒng)計、平均值、最大值、最小值、求和語法)...

    DB SQL 轉(zhuǎn) ES DSL(支持多種數(shù)據(jù)庫常用查詢、統(tǒng)計、平均值、最大值、最小值、求和語法)...

    1. 簡介 ??日常開發(fā)中需要查詢 Elasticsearch 中的數(shù)據(jù)時,一般會采用 RestHighLevelClient 高級客戶端封裝的API。項目中一般采用一種或多種關(guān)系型數(shù)據(jù)庫(如: Mysql 、 PostgreSQL 、 Oracle 等) + NoSQL(如: Elasticsearch )存儲方案;不同關(guān)系數(shù)據(jù)庫可以采用 Mybatis-Plus 方案屏蔽數(shù)據(jù)庫的方言

    2024年01月17日
    瀏覽(22)
  • Elasticsearch8常用查詢api,java ElasticsearchClient寫法和原生Rest DSL寫法

    Java Client Rest Api Java Client Rest Api Java Client Rest Api Java Client Rest Api ? 用來匹配任意字符, * 用來匹配零個或者多個字符。 Java Client Rest Api Java Client Rest Api Java Client Java Client Rest Api

    2024年02月08日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包