es like and or_ES系列08:Full text queries(3) query_string系列

bb8a3a04e3e10d9e53cffa6f3a2bc6b2.png
前面为大家介绍了:【ES系列06:ik分词+Full text queries 之match query、ES系列07:match_phrase与match_phrase_prefix query】。今天TeHero为大家分享 Full text queries 剩余的4种查询语句multi_match query、common terms query、query_string query、simple_query_string query, 同时结合倒排序索引原理,将DSL语句转化为sql语句,方便大家理解学习。 ps:文章最后有关于 Full text queries 所有查询的总结!

19bf85fc7c5f06c1e684e4cc48bb2ae4.png
Full Text queries 系列知识脑图
ps:上图的xmind文件获取方式见评论区!
在学习本节之前,请先参考:ES系列07:match_phrase与match_phrase_prefix query,完成数据导入和倒排列表的创建。

4d802b8b760f01c8031efa9fe085dfe7.png
Posting List
ps:如果看不懂上图,请先阅读学习:ElasticSearch系列05:倒排序索引与分词Analysis

一、multi_match query -match 的多字段版本

结合之间的match语法,这个是很好理解的:ES系列06:ik分词+Full text queries 之match query
# 1、同时查询  "content", "content.ik_smart_analyzer",得到文档3
GET /tehero_index/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "系统",
      "fields": [
        "content",
        "content.ik_smart_analyzer"
      ]
    }
  }
}

# 2、同时查询 所有字段 得到所有文档
GET /tehero_index/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "系统",
      "fields": [
        "content",
        "content.ik_smart_analyzer",
        "content.ik_max_analyzer"
      ]
    }
  }
}
需要注意的是, 多个Fields之间的查询关系是 or ,就 相当于mysql 的 【where 字段1=“检索词”or 字段2 = “检索词” or 字段3 = “检索词”】

字段^数字:表示增强该字段(权重影响相关性评分):先知道有这么个属性即可,相关性评分是一个重点和难点,后面再系统讲解。

GET /tehero_index/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "系统",
      "fields": [
        "content",
        "content.ik_smart_analyzer^3",
        "content.ik_max_analyzer"
      ]
    }
  }
}

1.1 multi_match query 对应的sql语句

GET /tehero_index/_doc/_search
{
  "query": {
    "multi_match": {
      "query": "系统学",
      "fields": [
        "content.ik_smart_analyzer",
        "content.ik_max_analyzer"
      ]
    }
  }
}

DSL执行分析:

1)检索关键词“系统学”,根据 搜索的field对应的分词器,进行不同的分词: "content.ik_smart_analyzer"字段(简称field1)分词,得到一个Token【系统学】;"content.ik_max_analyzer"字段(简称field2)分词,得到三个Token【系统学,系统,学】。
2)使用检索词的Token 在对应的field的PostingList中进行检索,等价于sql语句:【select id from field1-PostingList where Token = “系统学”】【select id from field2-PostingList where Token in ("系统学","系统","学")】;
3)最后再对 检索出来的两个 PostingList 做一个合并操作,得到文档。

二、common terms query——对停顿词的检索优化(简单了解即可)

对于这个语法, 先了解即可,主要还 是用于英语,对于中文,实用性不大。(ps:以下内容翻译至官网) 该查询将检索词分割分为两组: 更重要(即低频率而言)和不太重要的(即,高频率而言,如已停用词)首先,它搜索与更重要的术语匹配的文档。这些术语出现在 较少的文档中,并且 对相关性具有更大的影响。然后,它 对不那么重要的词执行第二次查询,这些词经常出现并且对相关性影响很小。但是, 它是在第一个查询的结果集基础上,而 不是计算所有匹配文档的相关性得分。这样, 高频项可以改善相关性计算,而无需付出性能不佳的代价。如果查询仅由高频词组成,则将单个查询作为AND(合并)查询执行 ,换句话说,所有词都是必需的。
# 文档频率大于0.1%的单词(例如"this"和"is")将被视为通用术语。
GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "low_freq_operator": "and"
            }
        }
    }
}

等价于:
GET /_search
{
    "query": {
        "bool": {
            "must": [
            { "term": { "body": "nelly"}},
            { "term": { "body": "elephant"}},
            { "term": { "body": "cartoon"}}
            ],
            "should": [
            { "term": { "body": "the"}},
            { "term": { "body": "as"}},
            { "term": { "body": "a"}}
            ]
        }
    }
}
总结: common terms query 目的:在保证检索性能的前提下,提高搜索结果的准确度。(能检索到the a 等高频率的停顿词)
# 简单理解,对 the a as 等进行分词
GET /_analyze
{
  "text": ["the a as"],
  "analyzer": "ik_max_word"
}
结果:为空,因为这些停顿词都被过滤掉了,这个时候就使用 common terms query,检索到这些词
{
  "tokens": []
}

三、query_string query

允许我们在 单个查询字符串中指定 AND | OR | NOT条件,同时也和 multi_match query 一样, 支持多字段搜索。
# 1、检索同时包含Token【系统学、es】的文档,结果为空
GET /tehero_index/_doc/_search
{
    "query": {
        "query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学 AND es"
        }
    }
}
# 2、检索包含Token【系统学、es】二者之一的文档,能检索到文档1、2、4
GET /tehero_index/_doc/_search
{
    "query": {
        "query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学 OR es"
        }
    }
}
有了前面的基础,query_string query是非常容易理解的, 语句1等价于sql语句【where Token = “系统学” and Token = “es” 】 注意点:1、中间的连接词 AND | OR | NOT 】必须是全大写;2、各个检索词依然会被对应的分词器分词,单个检索词就相当于match query。
GET /tehero_index/_doc/_search
{
    "query": {
        "query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统编程 OR es"
        }
    }
}
就比如上例, 单个检索词“系统编程”还是 会被分词器“ik_smart”分词为两个Token【系统、编程】,同时对这个检索词“系统编程” 执行 match query查询,所以上面的DSL会把 所有的文档都检索出来。

四、simple_query_string query

类似于query_string ,但是会忽略错误的语法,永远不会引发异常,并且会丢弃查询的无效部分。
simple_query_string支持以下特殊字符:

+ 表示与运算,相当于query_string 的 AND
| 表示或运算,相当于query_string  的 OR
- 取反单个令牌,相当于query_string 的 NOT
"" 表示对检索词进行 match_phrase query
* 字词末尾表示前缀查询

结合DSL语句简单理解下:

4.1 + 表示与运算,相当于query_string 的 AND

# 1、检索到文档4
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学 + 间隔"
        }
    }
}

4.2 | 表示或运算,相当于query_string 的 OR

# 2、检索到文档1、2、4
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学 | 间隔"
        }
    }
}

4.3 - 取反单个令牌,相当于query_string 的 NOT

# 3、检索到文档1、2
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学 -间隔",
            "default_operator": "and"
        }
    }
}
注意:参数"default_operator": "and"。该参数的默认值为or。
上述DSL对应的sql语句为:【where Token = 系统学 and Token <> 间隔】

4.4 "" 表示对检索词进行 match_phrase query

# 4、检索到文档2
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : ""系统学编程关注""
        }
    }
}
# 5、检索到所有文档
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统学编程关注"
        }
    }
}
分析:"query" : ""系统学编程关注"",会对检索词执行 match_phrase query !

4.5 * 字词末尾表示前缀查询 -match_phrase_prefix query

# 6、检索到文档 3
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统"
        }
    }
}
# 6、检索到所有文档,等价于match_phrase_prefix query
GET /tehero_index/_doc/_search
{
    "query": {
        "simple_query_string" : {
            "fields" : ["content.ik_smart_analyzer"],
            "query" : "系统*"
        }
    }
}

五、总结

到此,我们已经学完了 Full text queries 所有的查询语句:

1)match query:用于执行全文查询的标准查询,包括 模糊匹配和短语或接近查询。重要参数:控制Token之间的布尔关系:operator:or/and 2)match_phrase query:与match查询类似, 但用于匹配确切的短语或单词接近匹配。重要参数:Token之间的位置距离:slop 参数 3)match_phrase_prefix query:与match_phrase查询类似,但是会 对最后一个Token在倒排序索引列表中进行通配符搜索。重要参数:模糊匹配数控制:max_expansions 默认值50,最小值为1 4)multi_match query:match查询 的多字段版本。该查询在实际中使用较多, 可以降低DSL语句的复杂性。同时该语句 有多个查询类型,后面TeHero会专门进行讲解。 5)common terms query:对于中文检索意义不大。 6)query_string query 和 simple_query_string query,其实就是以上 query语句的合集,使用非常灵活,DSL编写简单。但是,TeHero认为这两个查询语句, 有一个很明显的弊端:类似于sql注入。如果用户在检索词输入了对应的“关键字”【比如OR、*】等,用户将获取到本不应该被查询到的数据。慎用!
下期预告:Term-level queries(精确匹配)

9d989aeb2b0bfd12021c3172d2d8f612.gif

●ElasticSearch系列01:如何系统学习ES

●ES系列05:倒排序索引与分词Analysis

●ES系列06:ik分词+Full text queries 之match query

●ES系列07:match_phrase与match_phrase_prefix query

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值