话不多说,开整
term(完全匹配)
{
"query": {
"bool": {
"must": [
{
"term": { #term 可以匹配任意数据
"harmLevel": "3"
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
range(范围查询)
{
"query": {
"bool": {
"must": [
{
"range": { #一般用于时间与数值类型的范围查找
"harmLevel": {
"gt": "1", #gt代表大于 #gte代表大于等于
"lt": "2" #lte代表小于 #lte代表小于等于
}
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
fuzzy(误拼写查询)
{
"query": {
"bool": {
"must": [
{
"fuzzy": {
"sourcePort": { #字段名称
"value": "505", #字段内容
"max_expansions": 1 #最大误拼写检查
}
}
}
]
}
}
}
#说明:以上例子是es允许在value内容中出现差别,例如输入505,然后查询,504的也会被查询到,105的也会被查询到,总之是只要是一位内容差别的都会查询出来,这里是因为max_expansions这个参数做了限制,最大就允许你输错一位,当然也是可以调整。另外还有一个最小误拼写检查参数min_similarity
wildcard(模糊查询)
GET website/_search
{
"query": {
"bool": {
"must": [
{
"wildcard": { #模糊查询关键字
"title": "*first*" # 只要句子中含有first内容都显示
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
exists(排除null)
{
"query": {
"bool": {
"must": [
{
"exists": { #关键字
"field": "title" #要排除那个字段
}
}
],
"should": [
{
"match_all": { }
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
凡是title字段为null的数据将不会展示,但是title字段为""(空字符串)依旧显示,如果要实现空字符串排除,可以配合must_not使用
match(分词查询)
GET website/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "这是"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
结果 :
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2814486,
"hits" : [
{
"_index" : "website",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.2814486,
"_source" : {
"title" : "这是一个标题",
"text" : "哈哈一个文本"
}
}
]
}
这个match只会对text字段生效,与模糊查询有区别,例如我们输入的是"这是",用了match关键字会帮我们拆成“这”跟“是”,然后因为text类型的数据,数据本身是分词的,例如“这是一个标题”,可能会被分词成“这”,“是”,“一个”,“标题”所以它们会按照分词完毕之后的内容进行匹配。故会匹配除这是一个标题这段内容。
match_phrase(短语匹配)
GET website/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"title": "标题啊"
}
}
]
}
}
}
这个match_phrase 采用的是短语匹配,也就是说把传入的内容当成是 一个句子,去匹配内容,只有当有匹配的字段中有满足这个句子的时候才会返回匹配内容
那这个match_phrase与match有什么区别的
match查找
GET website/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "标题啊"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
结果 :
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2814486,
"hits" : [
{
"_index" : "website",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.2814486,
"_source" : {
"title" : "这是一个标题",
"text" : "哈哈一个文本"
}
}
]
}
match_phrase 查找
GET website/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"title": "标题啊"
}
}
]
}
}
}
结果 :
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
由此可见,match是分词匹配(也就是说将输入的词进行分词),但match_phrase是短语匹配(将整个输入文本进行查找)
以上就是经常用到的关键字了,基本上可以满足简单的查找功能