使用评分查询来进行全文搜索或者其它任何需要影响相关性得分的搜索。
除此以外的情况都使用过滤查询。过滤查询不需要计算得分,且会缓存结果。
1. constant_score 查询
常用于只有filter,查询结果使用常量评分
GET /_search
{
"query": {
"constant_score": {
"filter" : {
"term" : {
"playCount" : 113411
}
}
}
}
}
2. term 查询
用于精确值匹配
GET /_search
{
"query": {
"term" : {
"playCount" : 1134
}
}
}
3. terms查询
多值精确匹配
GET /_search
{
"query": {
"terms" : {
"playCount" : [1134,113411]
}
}
}
4. match 查询
经过分析器后进行匹配
GET /_search
{
"query": {
"match" : {
"title" : "wake"
}
}
}
5. match_phrase 查询
短语匹配,短语经过分析器后形成多个词,这些词必须按序出现在该字段中,这些词组成一个不可分割的短语。
GET /_search
{
"query": {
"match_phrase" : {
"title" : "wake me"
}
}
}
6. multi_match 查询
可匹配多个字段
GET /_search
{
"query": {
"multi_match": {
"query": "Tang",
"fields": [ "artist", "album" ]
}
}
7. exists查询
GET /_search
{
"query":{
"exists": {
"field": "album"
}
}
}
8. range 查询
范围匹配,用于日期/数值
可选比较:gt/gte/lt/lte
GET /_search
{
"query":{
"range":{
"playCount":{
"gt":10,
"lte":1134
}
}
}
}
9. 组合查询
should并不影响结果,但影响评分
如果不存在must,至少匹配一条should
GET /_search
{
"query": {
"bool": {
"must":[{
"term":{
"playCount":1134
}
}],
"must_not":[{
"exists":{
"field":"album"
}
}],
"should":[{
"term":{
"playCount":1134
}
}],
"filter":{
"term":{
"playCount":1134
}
}
}
}
}
10. 分页查询
GET /_search
{
"from": 1,
"size": 3
}
11. 多字段搜索 控制权重
GET /books/_search
{
"query": {
"multi_match": {
"query": "peter smith",
"type": "cross_fields",
"fields": [ "title^2", "description" ]
}
}
}
12. 控制精度
GET /_search
{
"query": {
"bool": {
"must":[{
"term":{
"playCount":1134
}
}],
"must_not":[{
"exists":{
"field":"album"
}
}],
"should":[{
"term":{
"playCount":1134
}
}],
"filter":{
"term":{
"playCount":1134
}
},
"minimum_should_match": "75%"
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": {
"query": "quick brown dog",
"minimum_should_match": "75%"
}
}
}
}
本文详细介绍了Elasticsearch中各种查询方法,包括评分查询、过滤查询、常量评分查询、精确值匹配、多值精确匹配、经过分析器后的匹配、短语匹配、多字段匹配、存在性检查、范围匹配、组合查询、分页查询、多字段搜索控制权重及控制精度等,为用户提供全面的搜索解决方案。
1934

被折叠的 条评论
为什么被折叠?



