有条件查询
叶子查询(单条件)
模糊匹配
通过match关键词模糊匹配条件内容,如果字段是数字就会变成精确查询
GET /user/_search
{
"query": {
"match": {
"name": "李四"
}
}
}
GET /user/_search
{
"query": {
"match": {
"name": "李四"
}
}
}
会根据分词模糊匹配进行全文检索,全文检索会按照得分进行排序
{
"took" : 559,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.89712,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.89712,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 0.6931471,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
{
"took" : 559,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.89712,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.89712,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 0.6931471,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
通过 multi_match 关键词在多个字段中检索
GET /user/_search
{
"query": {
"multi_match": {
"query": "李四",
"fields": ["name","address"]
}
}
}
GET /user/_search
{
"query": {
"multi_match": {
"query": "李四",
"fields": ["name","address"]
}
}
}
prefix 前缀匹配
GET /user/_search
{
"query": {
"prefix": {
"name": "李"
}
}
}
GET /user/_search
{
"query": {
"prefix": {
"name": "李"
}
}
}
会查询以 李
开头的数据
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
regexp 通过正则表达来匹配数据
正则表达式语法中使用了许多符号和运算符来表示通配符和字符范围
- 句号 “.” 用于代表任何字符。
- 用括号括起来的一系列字符,例如 [a-z],是一个字符类。 字符类表示字符范围; 在此示例中,它充当任何字母的替代。
- 加号 “+” 用于表示重复的字符; 例如,“Mississippi” 中的 “pp”。
GET /user/_search
{
"query": {
"regexp": {
"name": "李.*"
}
}
}
GET /user/_search
{
"query": {
"regexp": {
"name": "李.*"
}
}
}
查询结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
wildcard 通配符匹配 (*表示任意字符串,?表示任意一个字符)
GET /user/_search
{
"query": {
"wildcard": {
"name": "李*"
}
}
}
GET /user/_search
{
"query": {
"wildcard": {
"name": "李*"
}
}
}
精确匹配
term 单个条件相等 (keyword代表不分词)
GET /user/_search
{
"query": {
"term": {
"name.keyword": "李四"
}
}
}
GET /user/_search
{
"query": {
"term": {
"name.keyword": "李四"
}
}
}
terms 单个字段属于某个值数组内的值
GET /user/_search
{
"query": {
"terms": {
"name.keyword": ["李四","王五"]
}
}
}
GET /user/_search
{
"query": {
"terms": {
"name.keyword": ["李四","王五"]
}
}
}
range 字段属于某个范围内的值(包含临界值)
GET /user/_search
{
"query": {
"range": {
"age": {
"gte": 24,
"lte": 28
}
}
}
}
GET /user/_search
{
"query": {
"range": {
"age": {
"gte": 24,
"lte": 28
}
}
}
}
exists 某个字段的值是否存在,相当于
select * from user where gender is not null
GET /user/_search
{
"query": {
"exists": {
"field": "gender"
}
}
}
GET /user/_search
{
"query": {
"exists": {
"field": "gender"
}
}
}
miss 某个字段的值是否缺失,相当于
select * from user where gender is null
GET /user/_search
{
"query": {
"miss": {
"field": "gender"
}
}
}
GET /user/_search
{
"query": {
"miss": {
"field": "gender"
}
}
}
ids 通过ID批量查询
GET /user/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
GET /user/_search
{
"query": {
"ids": {
"values": [1,2,3]
}
}
}
组合查询(多条件)
bool
must 各个条件都必须满足,即各条件是and的关系
GET /user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
GET /user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
should 各个条件有一个满足即可,即各条件是or的关
GET /user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
GET /user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
must_not 不满足所有条件,即各条件是not的关系
GET /user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
GET /user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "李四"
}
},
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
filter 不计算相关度评分,不计算_score即相关度评分,效率更高
GET /user/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
GET /user/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"address.keyword": "河北省"
}
}
]
}
}
}
constant_score
不计算相关度评分 must/filter/shoud/must_not 等的子条件是通过 term/terms/range/ids/exists/match 等叶子条件为参数的
需要传两个参数
- filter 你希望执行的Filter查询,每一个返回的文档都必须匹配这个查询,Filter查询不会计算相关度分数,为了提高性能,Elasticsearch会自动缓存经常使用的Filter查询。
- 一个Float类型的数,用于指定匹配成功的文档的相关度分数。默认值为1.0。
GET /user_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": {
"term": {
"address.keyword": "河北省"
}
}
}
}
]
}
}
}
GET /user_search
{
"query": {
"bool": {
"should": [
{
"bool": {
"filter": {
"term": {
"address.keyword": "河北省"
}
}
}
}
]
}
}
}
dis_max
支持多并发查询,可返回与任意查询条件子句匹配的文档类型。与bool查询可以将所有匹配查询的分数相结合的方式不同,dis_max查询只使用最佳查询条件的分数。