elasticsearch 查询 term和match

一.match 查询

1.match 之 match (按条件查询)

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  }
}

2.match 之 match_all (查询全部)

GET zhifou/doc/_search
{
  "query": {
    "match_all": {}
  }
}

#相当于
GET zhifou/doc/_search  也是查询全部

3.match 之 match_phrase(短语查询)

首先创建一些示例:

PUT t1/doc/1
{
  "title": "中国是世界上人口最多的国家"
}
PUT t1/doc/2
{
  "title": "美国是世界上军事实力最强大的国家"
}
PUT t1/doc/3
{
  "title": "北京是中国的首都"
}

需求1: 查询关于中国的字段(只希望返回中国相关的文档)

1.按照match查询  会出现三个结果    因为ES在内部对文档做分词的时候,对中文是一个字一个字分的,所以搜中国,就是中 和  国 都符合条件

2.如果要中文分词需要下中文分词插件,现在的解决办法就是 采用 短语查询  match_phrase

GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "中国"
      }
    }
  }
}

需求2: 搜索类似于正则的 中国.*?世界 的文档

用slop 参数   表示  中国  世界 这两个词组之间 隔了2个词

GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "中国世界",
        "slop": 2
      }
    }
  }
}

4.match 之 match_phrase_prefix(最左前缀查询)

准备数据:

PUT t3/doc/1
{
  "title": "maggie",
  "desc": "beautiful girl you are beautiful so"
}
PUT t3/doc/2
{
  "title": "sun and beach",
  "desc": "I like basking on the beach"
}

需求: 要查询desc 中 有 bea 的文档

GET t3/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": "bea"
    }
  }
}

注意: 有时会和max_expanions搭配使用

5.match 之 multi_match(多字段查询) 

准备数据:

PUT t3/doc/1
{
  "title": "maggie is beautiful girl",
  "desc": "beautiful girl you are beautiful so"
}
PUT t3/doc/2
{
  "title": "beautiful beach",
  "desc": "I like basking on the beach,and you? beautiful girl"
}

需求:我们要查询 多个字段中含有同一个关键字的文档

GET t3/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "beautiful"
          }
        },
        {
          "match": {
            "desc": "beautiful"
          }
        }
      ]
    }
  }
}

如果字段很多的情况下  我们需要写非常多的 match

因此  我们用到了 multi_match 与上述查询结果一致

GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "beautiful",
      "fields": ["title", "desc"]
    }
  }
}

除此之外,multi_match 甚至可以当作 match_phrase  和 match_phrase_prefix 使用,只需要指定type类型即可.

GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "gi",
      "fields": ["title"],
      "type": "phrase_prefix"
    }
  }
}
GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "girl",
      "fields": ["title"],
      "type": "phrase"
    }
  }
}

小结

  1.match:返回所有匹配的分词

  2. match_all: 查询全部

  3.match_phrase: 短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔

  4.match_phrase_prefix:前缀查询,根据短语中最后一个词组做前缀匹配,可以用于搜索提示,但注意和max_expanions 搭配。 其实默认是50.

  5.multi_match:多字段查询,使用相当灵活,可以完成match_phrase 和 match_phrase_prefix的工作

6.term查询 (精确查询)

默认情况下, ES 在对文档分析期间(将文档分词后保存到倒排索引中),会对文档进行分词,比如默认的标准分析器会对文档进行:

  1.删除大多数标点符号

  2.将文档分解为带个词条,我们称为token

  3.将token 转为 小写

term 查询区分大小写 不会将 搜索字段值 转成小写  

因此, 不要使用 term 对类型是text的字段进行查询

如果想要查询多个精准值 使用terms

GET w10/doc/_search
{
  "query": {
    "terms": {
      "t1": ["beautiful", "sexy"]
    }
  }
}
原文地址:https://www.cnblogs.com/s686zhou/p/12253027.html