【还不会用ElasticSearch ?】 ElasticSearch 客户端 DSL查询 使用ik分词器 命令 kibana 演示
作者:互联网
1.IK分词器测试
GET _analyze
{
"analyzer": "ik_smart",
"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
}
]
}
2.DSL查询
2.1 match查询
get csdn/_search
{
"query":{
"match": {
# "name" 查询的字段 "赵牛修改1" 分词后匹配的词
"name": "赵牛修改1"
}
}
}
返回结果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.6407243,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 0.6407243,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 11
}
}
]
}
}
2.1.1 term查询
get csdn/_search
{
"query":{
"term": {
"name": "赵牛修改1"
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
查询结果为0 同样的查询条件为什么用 match能查到,用term 却差不多呢
2.1.1.1 match与term 的区别
match查询会对查询条件进行分析,先将查询条件分词,再进行查询,并求集,类似与like
term查询不会对查询条件进行分词,类似与 =
2.1.2 terms查询
get csdn/_search
{
"query":{
"terms": {
"age": [
11,
18
]
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
terms查询可以对字段设置多个查询值.类似 于MySQL中的 in
2.1.3 match_all查询
post csdn/_search
{
"query":{
"match_all": {}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
查询所有文档 与 GET /索引名/_search 一样
2.1.4 wildcard查询
get csdn/_search
{
"query":{
"wildcard": {
"name": "*修改"
}
}
}
返回结果
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
允许使用通配符*和?进行查询(注意是词条的匹配) *:表示0或者多个字符 ?:表示任意一个字符
2.1.5 PrefixQuery 查询
get csdn/_search
{
"query":{
"prefix": {
"name": "修"
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
get csdn/_search
{
"query":{
"prefix": {
"name": "改"
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
对name进行分词字段测试
GET _analyze
{
"analyzer": "ik_smart",
"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" : 4,
"type" : "CN_WORD",
"position" : 2
}
]
}
由以上三级查询结果可以得出结论
前缀查询 注意是分词后的词条 前缀查询 如果用 "修"能匹配到结果 而用 "改"则找不到任何结果
2.2 range(范围查询)
get csdn/_search
{
"query":{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
get 大于等于 gt 大于 lte 小于等于 lt 小于
2.3 bool(组合查询)
get csdn/_search
{
"query":{
"bool": {
"filter": [
{
"range": {
"age": {
"gte": 1,
"lte": 100
}
}
}
]
, "must": [
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
]
,
"should": [
{
"match_all": {}
}
]
,
"must_not": [
{
"range": {
"id": {
"gte": 30,
"lte": 40
}
}
}
]
}
}
}
返回结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 2.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
结果分析
- must 该条款(查询)必须出现在匹配的文件,并将有助于得分
- should 子句(查询) 应该出现在匹配文档中,计算得分
- filter 子句(查询)必须出现在匹配的文档中,不计算得分
- must_not 子句(查询)不能出现在匹配的文档中,不计算得分
2.4 aggs(聚合查询)
get csdn/_search
{
"aggs": {
#group_by_age 分组后结果集合名
"group_by_age": {
"terms":{
#根据 id 字段分组
"field":"age"
}
}
}
}
返回结果
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 1
},
{
"key" : 18,
"doc_count" : 1
}
]
}
}
}
get csdn/_search
{
"aggs": {
"sum_age": {
#求age的和
"sum":{
"field":"age"
}
}
}
}
返回结果
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
},
"aggregations" : {
"sum_age" : {
"value" : 29.0
}
}
}
聚合查询
1. sum:求总数值,等同sum()
2. min:求最小值,等同min()
3. max:求最大值,等同max()
4. avg:求平均值,等同avg()
5. cardinality:求基数,等同count()
6. terms:分组,等同group by
标签:hits,name,age,kibana,ik,score,ElasticSearch,csdn,id 来源: https://blog.csdn.net/JAVAlife2021/article/details/123167507