本文将继续使用elasticsearch学习7–Full text queries全文检索之match_phrase query与match_phrase_prefix query中的数据。数据内容如下(具体mapping见elasticsearch学习7):
{ “id” : 1,“content”:“关注我,系统学编程” }
{ “id” : 2,“content”:“系统学编程,关注我” }
{ “id” : 3,“content”:“系统编程,关注我” }
{ “id” : 4,“content”:“关注我,间隔系统学编程” }
1、multi_match query
# 1.同时查询 "content", "content.ik_smart_field",得到文档3
POST /test_003/_doc/_search
{
"query": {
"multi_match": {
"query": "系统",
"fields": [
"content",
"content.ik_smart_field"
]
}
}
}
# 2.同时查询所有字段,得到所有文档
POST /test_003/_doc/_search
{
"query": {
"multi_match": {
"query": "系统",
"fields": [
"content",
"content.ik_smart_field",
"content.ik_max_field"
]
}
}
}
结果分析: 首先要知道的是,多个field之间的查询关系是 or 。
【语句一】检索关键词“系统”,系统的Token就是【系统】,“content”字段没有指定分词器,默认为标准分词器,标准分词器将一个中文字分成一个Token,所以“content”字段不包含【系统】Token。ik_smart_field根据ik_smart分词,四个文档中有【系统】Token的只有文档3,其他都是【系统学】。
【语句二】检索关键词“系统”,“content”字段的结果为空,"content.ik_smart_field"的结果为文档3。"content.ik_max_field"使用的是“ik_max_word”分词器,所以每个文档中都有【系统】Token。
2、query_string query
允许我们在单个查询字符串中指定AND | OR | NOT条件,同时也和 multi_match query 一样,支持多字段搜索。
# 1.检索同时包含Token【系统、es】的文档,结果为空
POST /test_003/_doc/_search
{
"query": {
"query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统 AND es"
}
}
}
# 2.检索包含Token【系统、es】二者之一的文档,能检索到文档3
POST /test_003/_doc/_search
{
"query": {
"query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统 OR es"
}
}
}
结果分析:
【语句一】检索既包含【系统】Token又包含【es】Token的文档,由于所有文档都没有【es】Token,所以结果为null。
【语句二】检索包含【系统】Token或者包含【es】Token的文档,四个文档中有【系统】Token的只有文档3,其他都是【系统学】,所以结果为文档3。
3、simple_query_string query
类似于query_string ,但是会忽略错误的语法,永远不会引发异常,并且会丢弃查询的无效部分。
simple_query_string支持以下特殊字符:
+表示与运算,相当于query_string 的 AND
|表示或运算,相当于query_string 的 OR
-取反单个令牌,相当于query_string 的 NOT
“” 表示对检索词进行 match_phrase query
*字词末尾表示前缀查询
1) + 表示与运算,相当于query_string 的 AND
# 检索到文档4
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统学 + 间隔"
}
}
}
2) | 表示或运算,相当于query_string 的 OR
# 检索到文档1、2、4
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统学 | 间隔"
}
}
}
3) - 取反单个令牌,相当于query_string 的 NOT
# 检索到文档1、2
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统学 -间隔",
"default_operator": "and"
}
}
}
注意:参数"default_operator": “and”。该参数的默认值为or。 相当于【where Token = 系统学 and Token <> 间隔】
4) “” 表示对检索词进行 match_phrase query
# 检索到文档2
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "\"系统学编程关注\""
}
}
}
# 检索到所有文档
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统学编程关注"
}
}
}
分析:“query” : ““系统学编程关注””,会对检索词执行 match_phrase query !
5) * 字词末尾表示前缀查询 -match_phrase_prefix query
# 检索到文档 3
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统"
}
}
}
# 检索到所有文档,等价于match_phrase_prefix query
POST /test_003/_doc/_search
{
"query": {
"simple_query_string" : {
"fields" : ["content.ik_smart_field"],
"query" : "系统*"
}
}
}