说明
精确查询,查询某个字段的内容 与 查询条件完全相同
相当于mysql中的 =
应用场景
适合 keyword、numeric、date等明确值,区分大小写
注意: 不能查询 text 类型字段
例子
查询 author 是 李雷的记录
相当于 SQL:select * from toutiao_v1 where author = '李雷'
GET /toutiao_v1/_search
{
"query": {
"term": {
"author": "李雷"
}
}
}
执行结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.6931471,
"hits" : [
{
"_index" : "toutiao_v1",
"_type" : "_doc",
"_id" : "iV-h1H8BcF98mmCacx9Y",
"_score" : 0.6931471,
"_source" : {
"title" : "JAVA技巧",
"content" : "JAVA高级技巧",
"author" : "李雷",
"like" : 229,
"date" : "2021-08-30",
"type" : "java"
}
},
{
"_index" : "toutiao_v1",
"_type" : "_doc",
"_id" : "jF-h1H8BcF98mmCagx8w",
"_score" : 0.6931471,
"_source" : {
"title" : "JAVA mysql技巧大全",
"content" : "JAVA mysql技巧大全",
"author" : "李雷",
"like" : 528,
"date" : "2021-08-29",
"type" : "java"
}
}
]
}
}
查询 数字类型 字段
GET /toutiao_v1/_search
{
"query": {
"term": {
"like": 528
}
}
}
term不能查询 text 类型的字段
content 字段是 text 类型,不能用 term 精确查询
get /toutiao_v1/_search
{
"query":{
"term":{
"content":"mysql技术内幕"
}
}
}
执行结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0, // 查询记录是 0 条
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
精确查询 title 字段
查询 映射
GET /toutiao_v1/_mapping
执行结果:
{
"toutiao_v1" : {
"mappings" : {
"properties" : {
"author" : {
"type" : "keyword"
},
"content" : {
"type" : "text"
},
"date" : {
"type" : "date"
},
"like" : {
"type" : "integer"
},
"title" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"type" : {
"type" : "keyword"
}
}
}
}
}
注意: title 字段有2个类型,其本身类型是 text,子类型是 keyword
所以想要 term 查询,只能根据其 子类型 进行查询
根据 title 的 keyword 进行 term 查询
GET /toutiao_v1/_search
{
"query": {
"term": {
"title.keyword": "JAVA从入门到放弃"
}
}
}
注意: 对于keyword类型,查询出完全匹配的记录,相当于 mysql where title='JAVA从入门到放弃'
执行结果:
{
"took":0,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":1,
"relation":"eq"
},
"max_score":1.2039728,
"hits":[
{
"_index":"toutiao_v2",
"_type":"_doc",
"_id":"md1I5nsByvoeiV4PYe1E",
"_score":1.2039728,
"_source":{
"title":"JAVA从入门到放弃",
"content":"JAVA高手的技巧,java基础、高级",
"author":"韩梅梅",
"like":983,
"date":"2021-08-29"
}
}
]
}
}