复合查询

bool query (布尔查询)

用bool 逻辑将较小的查询组合成较大的查询
bool查询包含四种操作符,分别是must,should,must_not,filter。他们均是一种数组,数组里面是对应的判断条件。

  • must: 必须匹配。贡献算分
  • must_not:过滤子句,必须不能匹配,但不贡献算分
  • should: 选择性匹配,至少满足一条。贡献算分
  • filter: 过滤子句,必须匹配,但不贡献算分
  1. GET /_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. { "match": { "name.first": { "query": "shay", "_name": "first" } } },
  7. { "match": { "name.last": { "query": "banon", "_name": "last" } } }
  8. ],
  9. "filter": {
  10. "terms": {
  11. "name.last": [ "banon", "kimchy" ],
  12. "_name": "test"
  13. }
  14. }
  15. }
  16. }
  17. }

每个query 都可以又一个name 属性来追踪搜索出来的数据match到了哪个条件

boosting query (提高查询)

对于bool 查询来说,只要有一个子查询不匹配就不会返回搜索结果,而boosting query 则是降低显示的权重和优先级
比如搜索逻辑是 name = ‘apple’ and type =’fruit’,对于只满足部分条件的数据,不是不显示,而是降低显示的优先级(即score)

例子: 对匹配pie的做显示降级处理

  1. GET /test-dsl-boosting/_search
  2. {
  3. "query": {
  4. "boosting": {
  5. "positive": {
  6. "term": {
  7. "content": "apple"
  8. }
  9. },
  10. "negative": {
  11. "term": {
  12. "content": "pie"
  13. }
  14. },
  15. "negative_boost": 0.5
  16. }
  17. }
  18. }

constant score (固定分数查询)

当不需要计算的时候,返回指定的score,例如filter 查询语句就不需要计算score:

  1. GET /test-dsl-constant/_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "term": { "content": "apple" }
  7. },
  8. "boost": 1.2
  9. }
  10. }
  11. }

dis_max (最佳匹配查询)

Disjunction Max Query 分离最大化查询,将任何与任意查询匹配的文档返回,但只将最佳匹配的评分作为查询的评分结果返回。

  1. GET /test-dsl-dis-max/_search
  2. {
  3. "query": {
  4. "dis_max": {
  5. "queries": [
  6. { "match": { "title": "Brown fox" }},
  7. { "match": { "body": "Brown fox" }}
  8. ],
  9. "tie_breaker": 0
  10. }
  11. }
  12. }

如上述例子,与任何一个match 匹配的文档都会返回,但最后的评分是所有match 的评分和

function_score (函数查询)

自定义函数来计算_score

  • script_score 使用自定义的脚本来完全控制分值计算逻辑。如果你需要以上预定义函数之外的功能,可以根据需要通过脚本进行实现。
  • weight 对每份文档适用一个简单的提升,且该提升不会被归约:当weight为2时,结果为2 * _score。
  • random_score 使用一致性随机分值计算来对每个用户采用不同的结果排序方式,对相同用户仍然使用相同的排序方式。
  • field_value_factor 使用文档中某个字段的值来改变_score,比如将受欢迎程度或者投票数量考虑在内。
  • 衰减函数(Decay Function) - linear,exp,gauss

Match 详解

match 查询步骤

  1. 检查字段类型
  2. 分析字符串(使用term or match)
  3. 查询匹配文档
  4. 为每个文档评分(tf-idf + 字段长度,长度越短相关度越高)

准备数据

  1. PUT /test-dsl-match
  2. { "settings": { "number_of_shards": 1 }}
  3. POST /test-dsl-match/_bulk
  4. { "index": { "_id": 1 }}
  5. { "title": "The quick brown fox" }
  6. { "index": { "_id": 2 }}
  7. { "title": "The quick brown fox jumps over the lazy dog" }
  8. { "index": { "_id": 3 }}
  9. { "title": "The quick brown fox jumps over the quick dog" }
  10. { "index": { "_id": 4 }}
  11. { "title": "Brown fox brown dog" }

match 多词处理逻辑

ES 会首先执行分词,再将分好的词分别查询并组合输出结果

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "BROWN DOG"
  6. }
  7. }
  8. }

等同于使用should

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "term": {
  8. "title": "brown"
  9. }
  10. },
  11. {
  12. "term": {
  13. "title": "dog"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

也等同于

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "BROWN DOG",
  7. "operator": "or"
  8. }
  9. }
  10. }
  11. }

如果要使用and 操作,就把operator 改为 and

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": { // 申明查询filed
  6. "query": "BROWN DOG", // 申明查询语句
  7. "operator": "and" // 申明算子
  8. }
  9. }
  10. }
  11. }

相当于使用bool 查询包含两个子查询

GET /test-dsl-match/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "title": "brown"
          }
        },
        {
          "term": {
            "title": "dog"
          }
        }
      ]
    }
  }
}

控制match的匹配精度

match 可以设置参数minimum_should_match。

GET /test-dsl-match/_search
{
  "query": {
    "match": {
      "title": {
        "query":                "quick brown dog",
        "minimum_should_match": "75%" / 2
      }
    }
  }
}

指定最少匹配的单词数,可以是百分数

match_phrase

GET /test-dsl-match/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "quick brown"
      }
    }
  }
}

match_phrase 提供词组搜索,只有匹配词组(连续)的才能返回正确结果。

match_phrase_prefix (前缀匹配)

GET /test-dsl-match/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": {
        "query": "quick brown f"
      }
    }
  }
}

在词组搜索的基础上加了前缀匹配
prefix的意思不是整个text的开始匹配,而是最后一个词项满足term的prefix查询而已。

Match bool prefix

match_bool_prefix 中的字段是无序的

GET /test-dsl-match/_search
{
  "query": {
    "match_bool_prefix": {
      "title": {
        "query": "quick brown f"
      }
    }
  }
}

相当于下列查询

GET /test-dsl-match/_search
{
  "query": {
    "bool" : {
      "should": [
        { "term": { "title": "quick" }},
        { "term": { "title": "brown" }},
        { "prefix": { "title": "f"}}
      ]
    }
  }
}

multi_match

以上的match 只查询一个字段,如果要一次查询多个字段就要求使用multi_match

{
  "query": {
    "multi_match" : {
      "query":    "Will Smith",
      "fields": [ "title", "*_name" ] 
    }
  }
}

*表示前缀字段

query_string 类型

根据运算符来解析和拆分查询字符串。
可以使用该query_string查询创建一个复杂的搜索,其中包括通配符,跨多个字段的搜索等等。尽管用途广泛,但查询是严格的,如果查询字符串包含任何无效语法,则返回错误。
参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

GET /test-dsl-match/_search
{
  "query": {
    "query_string": {
      "query": "(lazy dog) OR (brown dog)",
      "default_field": "title"
    }
  }
}


simple_query_string

GET /test-dsl-match/_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"over the\" + (lazy | quick) + dog",
        "fields": ["title"],
        "default_operator": "and"
    }
  }
}

simple_query_string 查询不会针对无效语法返回错误。而是,它将忽略查询字符串的任何无效部分
可以使用default_operator 来决定query_string 的子查询是用and/or 连接

Term 查询

准备数据

PUT /test-dsl-term-level
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "programming_languages": {
        "type": "keyword"
      },
      "required_matches": {
        "type": "long"
      }
    }
  }
}

POST /test-dsl-term-level/_bulk
{ "index": { "_id": 1 }}
{"name": "Jane Smith", "programming_languages": [ "c++", "java" ], "required_matches": 2}
{ "index": { "_id": 2 }}
{"name": "Jason Response", "programming_languages": [ "java", "php" ], "required_matches": 2}
{ "index": { "_id": 3 }}
{"name": "Dave Pdai", "programming_languages": [ "java", "c++", "php" ], "required_matches": 3, "remarks": "hello world"}

判断字段是否存在 exists

由于多种原因,文档字段的索引值可能不存在:

  • 源JSON中的字段是null或[]
  • 该字段已”index” : false在映射中设置
  • 字段值的长度超出ignore_above了映射中的设置
  • 字段值格式错误,并且ignore_malformed已在映射中定义
    GET /test-dsl-term-level/_search
    {
    "query": {
      "exists": {
        "field": "remarks"
      }
    }
    }
    

根据文档id 查找

GET /test-dsl-term-level/_search
{
  "query": {
    "ids": {
      "values": [3, 1]
    }
  }
}

通过前缀查找某个字段

prefix 申明某个字段的前缀

GET /test-dsl-term-level/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "Jan"
      }
    }
  }
}

term 多分词匹配 terms

不同term 之间是or的关系

GET /test-dsl-term-level/_search
{
  "query": {
    "terms": {
      "programming_languages": ["php","c++"]
    }
  }
}

用文档中的字段动态匹配满足查询term 的个数

GET /test-dsl-term-level/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "java", "php" ],
        "minimum_should_match_field": "required_matches"
      }
    }
  }
}

通配符:wildcard

GET /test-dsl-term-level/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "D*ai",
        "boost": 1.0,
        "rewrite": "constant_score"
      }
    }
  }
}

范围:range

GET /test-dsl-term-level/_search
{
  "query": {
    "range": {
      "required_matches": {
        "gte": 3,
        "lte": 4
      }
    }
  }
}

正则

GET /test-dsl-term-level/_search
{
  "query": {
    "regexp": {
      "name": {
        "value": "Ja.*",
        "case_insensitive": true
      }
    }
  }
}

模糊匹配

官方文档对模糊匹配:编辑距离是将一个术语转换为另一个术语所需的一个字符更改的次数。这些更改可以包括:

  • 更改字符(box→ fox)
  • 删除字符(black→ lack)
  • 插入字符(sic→ sick)
  • 转置两个相邻字符(act→ cat) ```json GET /test-dsl-term-level/_search { “query”: { “fuzzy”: {
    "remarks": {
      "value": "hell"
    }
    
    } } }

```