elasticsearch高级查询

子条件查询:(特定字段查询所指特定值)

  1. Query Context: 在查询的过程中,除了判断文档是否满足条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件的匹配有多好。
  2. 复合条件查询:(以一定的逻辑组合子条件查询)

查询的模拟文档如下:

一、Query Context:

1.全文本查询:

针对文本类型数据

(1)模糊匹配查询:

  • 查询作者为李歘歘的书籍:

输入:

{
    "query": {
        "match": {
            "author": "李歘歘"
        }
    }
}

查询结果:

 

(2)短语匹配查询:

  • 查询包含短语“elasticsearch入门”的书籍:

输入:

{
    "query": {
        "match_phrase": {
            "title": "elasticsearch入门"
        }
    }
}

查询结果:

(3)多个字段的匹配查询:

  • 查询作者和标题包含“李歘歘”的书籍:

输入:

{
    "query": {
        "multi_match": {
            "query": "李歘歘",
            "fields": ["author","title"]
        }
    }
}

查询结果:

(3)语法查询:

  • 查询包含“elasticsearch”和“入门”或者“java”的书籍:

输入:

{
    "query": {
        "query_string": {
            "query": "(elasticsearch AND 入门) OR java "
        }
    }
}

查询结果:

(3)语法查询:

  • 查询title和author中包含“elasticsearch”和“李歘歘”的书籍:

输入:

{
    "query": {
        "query_string": {
            "query": "elasticsearch OR 李歘歘 ",
            "fields": [
                "title",
                "author"
            ]
        }
    }
}

查询结果:

 

2.字段级别的查询:

针对结构化数据,如数字、日期等

  • 查询字数在1000的书籍:

输入:

{
    "query": {
        "term": {
        	"word_count": 1000
        }
    }
}

 

 查询结果:

 

 

  • 字数在1000到2000的书籍:

输入:

{
    "query": {
        "range": {
        	"word_count": {
        		"gte": 1000,
        		"lte": 2000
        	}
        }
    }
}

gte和lte中‘e’代表等于

 

查询结果:

 

 

 

  • 查询字数在1999-01-01到2000-01-01的书籍:

输入:

{
    "query": {
        "range": {
        	"publish_date": {
        		"gte": "1999-01-01",
        		"lte": "2000-01-01"
        	}
        }
    }
}

 

查询结果:

二、Filter Context

在查询过程中,只判断该文档是否满足条件,只有YES或者NO

查询字数是1000的书:

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "word_count": 1000
                }
            }
        }
    }
}

 

 查询结果:

三、复合条件查询

1.固定分数查询:

查询title包含“elasticsearch”并且评分在3的书籍

输入:

{
    "query": {
       "constant_score": {
       	"filter": {
       		"match": {
       			"title": "elasticsearch"
       		}
       	},
       	"boost": 3
       }
    }
}

查询结果:

2.布尔查询:

(1)查询title包含“elasticsearch”或者author包含“李歘歘”的书籍

输入:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ]
        }
    }
}

运行结果:

(2)查询title包含“elasticsearch”并且author包含“李歘歘”的书籍

输入:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ]
        }
    }
}

运行结果:

 

(3)查询title包含“elasticsearch”并且author包含“李歘歘”并且字数是1000的书籍

输入:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "author": "李歘歘"
                    }
                },
                {
                    "match": {
                        "title": "elasticsearch"
                    }
                }
            ],
            "filter": {
                "term": {
                    "word_count": 1000
                }
            }
        }
    }
}

运行结果:

 

(3)查询author不包含“李歘歘”的书籍

输入:

{
    "query": {
        "bool": {
            "must_not": {
                "term": {
                    "author": "李歘歘"
                }
            }
        }
    }
}

运行结果:

 

来源于:微信公众号【李歘歘】

作者:李歘歘

扫码关注,领取众多粉丝福利,观看更多原创文章,联系作者

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值