文章40 | 阅读 24286 | 点赞0
废话不多说首先准备数据,我们先添加几个文档
PUT /lib3/user/1
{
"name" : "zhaoliu",
"address" :"hei long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1970-12-12",
"interests": "xi buan hejiu, duanlian, lvyou"
}
PUT /lib3/user/2
{
"name" :"zhaoming" ,
"address" : "bei jing hai dian qu qing he zhen",
"age" : 20,
"birthday" : "1998-10-12" ,
"interests": "xi huan hejiu, duanlian, changge"
}
PUT /lib3/user/3
{
"name" : "lisi",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 23,
"birthday" : "1998-10-12" ,
"interests": "xi huan hejiu,duanlian, changge"
}
PUT /lib3/user/4
{
"name": "wangwu",
"address" : "bei jing hai dian qu qing he zhen",
"age": 26,
"birthday" : "1995-10-12" ,
"interests": "xi huan biancheng, tingyinyue, lvyou"
}
PUT /lib3/user/5
{
"name" :"zhangsan",
"address" : "bei jing chao yang qu",
"age" : 29,
"birthday" : "1988-10-12",
"interests": "xi huan tingyinyue , changge , tiaowu"
}
简单通过name查找lisi这个人的基本信息 GET /lib3/user/_search?q=name:lisi
{
"took" : 43,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "lisi",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 23,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu,duanlian, changge"
}
}
]
}
}
这个max_score:相关匹配度分数(这是根据ElasticSearch的算法计算出来的)
查询兴趣爱好interests喜欢changge(唱歌)的人,并且年龄倒叙排
GET /lib3/user/_search?q=interests:changge&sort=age:desc
{
"took" : 124,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "zhangsan",
"address" : "bei jing chao yang qu",
"age" : 29,
"birthday" : "1988-10-12",
"interests" : "xi huan tingyinyue , changge , tiaowu"
},
"sort" : [
29
]
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "lisi",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 23,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu,duanlian, changge"
},
"sort" : [
23
]
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "zhaoming",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 20,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu, duanlian, changge"
},
"sort" : [
20
]
}
]
}
}
term query会去倒排索弓|中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword、numeric. date.
term:查询某个字段里含有某个关键词的文档
GET /lib3/user/_search/****{ "query":{ "term": {interests": "changge"}}}
terms:查询某个字段里合有多个关键词的文档
GET /ib3/user/_search { "query":{ "terms':{ "interests": ["hejiu","changge']}}
GET /lib3/user/_search
{
"query": {
"term": {
"name": {
"value": "zhaoliu"
}
}
}
}
GET /lib3/user/_search
{
"query": {
"term": {
"name": "zhaoliu"
}
}
}
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "zhaoliu",
"address" : "hei long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1970-12-12",
"interests" : "xi buan hejiu, duanlian, lvyou"
}
}
]
}
}
查找兴趣爱好interests为hejiu changge的人的信息
GET /lib3/user/_search
{
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
{
"took" : 56,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "zhangsan",
"address" : "bei jing chao yang qu",
"age" : 29,
"birthday" : "1988-10-12",
"interests" : "xi huan tingyinyue , changge , tiaowu"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "zhaoming",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 20,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu, duanlian, changge"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "zhaoliu",
"address" : "hei long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1970-12-12",
"interests" : "xi buan hejiu, duanlian, lvyou"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "lisi",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 23,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu,duanlian, changge"
}
}
]
}
}
只要含有一个关键词的都会被查询出来总共四个人有关于hejiu changge要么changge要么hejiu 要么两者都
如果我只想取前2个人使用from:0 (表示从第一个文档开始) size:2(取2个文档)
GET /lib3/user/_search
{
"from":0,
"size":2,
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
{
"took" : 81,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "zhangsan",
"address" : "bei jing chao yang qu",
"age" : 29,
"birthday" : "1988-10-12",
"interests" : "xi huan tingyinyue , changge , tiaowu"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "zhaoming",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 20,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu, duanlian, changge"
}
}
]
}
}
以上查询都是没有版本号的我们要获取版本号,只需要加一个version:true
GET /lib3/user/_search
{
"version": true,
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
{
"took" : 33,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "lib3",
"_type" : "user",
"_id" : "5",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"name" : "zhangsan",
"address" : "bei jing chao yang qu",
"age" : 29,
"birthday" : "1988-10-12",
"interests" : "xi huan tingyinyue , changge , tiaowu"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "2",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"name" : "zhaoming",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 20,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu, duanlian, changge"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"name" : "zhaoliu",
"address" : "hei long jiang sheng tie ling shi",
"age" : 50,
"birthday" : "1970-12-12",
"interests" : "xi buan hejiu, duanlian, lvyou"
}
},
{
"_index" : "lib3",
"_type" : "user",
"_id" : "3",
"_version" : 1,
"_score" : 1.0,
"_source" : {
"name" : "lisi",
"address" : "bei jing hai dian qu qing he zhen",
"age" : 23,
"birthday" : "1998-10-12",
"interests" : "xi huan hejiu,duanlian, changge"
}
}
]
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/ywl470812087/article/details/104865525
内容来源于网络,如有侵权,请联系作者删除!