Link Search Menu Expand Document Documentation Menu

短语匹配查询

使用 match_phrase 查询匹配包含按指定顺序排列的精确短语的文档。您可以通过提供 slop 参数来增加短语匹配的灵活性。

match_phrase 查询创建一个 短语查询,用于匹配一系列词条。

以下示例展示了一个基本的 match_phrase 查询

GET _search
{
  "query": {
    "match_phrase": {
      "title": "the wind"
    }
  }
}

要传递附加参数,可以使用扩展语法

GET _search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "the wind",
        "analyzer": "stop"
      }
    }
  }
}

示例

例如,考虑一个包含以下文档的索引

PUT testindex/_doc/1
{
  "title": "The wind rises"
}

PUT testindex/_doc/2
{
  "title": "Gone with the wind"
  
}

以下 match_phrase 查询搜索短语 wind rises,其中单词 wind 后跟单词 rises

GET testindex/_search
{
  "query": {
    "match_phrase": {
      "title": "wind rises"
    }
  }
}

响应包含匹配的文档

响应
{
  "took": 30,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.92980814,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.92980814,
        "_source": {
          "title": "The wind rises"
        }
      }
    ]
  }
}

分析器

默认情况下,当您对 text 字段运行查询时,搜索文本会使用与该字段关联的索引分析器进行分析。您可以在 analyzer 参数中指定不同的搜索分析器。例如,以下查询使用 english 分析器

GET testindex/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "the winds",
        "analyzer": "english"
      }
    }
  }
}

english 分析器会移除停用词 the 并执行词干提取,生成词元 wind。这两个文档都与此词元匹配,并显示在结果中

响应
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.19363807,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.19363807,
        "_source": {
          "title": "The wind rises"
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 0.17225474,
        "_source": {
          "title": "Gone with the wind"
        }
      }
    ]
  }
}

Slop

如果您提供 slop 参数,查询将容忍搜索词的重新排序。Slop 指定了查询短语中单词之间允许存在的其他单词数量。例如,在以下查询中,搜索文本与文档文本相比进行了重新排序

GET _search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "wind rises the",
        "slop": 3
      }
    }
  }
}

查询仍然返回匹配的文档

响应
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.44026947,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.44026947,
        "_source": {
          "title": "The wind rises"
        }
      }
    ]
  }
}

空查询

有关可能的空查询的信息,请参阅相应的 match query 部分

参数

查询接受字段名称 (<field>) 作为顶级参数

GET _search
{
  "query": {
    "match_phrase": {
      "<field>": {
        "query": "text to search for",
        ... 
      }
    }
  }
}

<field> 接受以下参数。除 query 外,所有参数均为可选。

参数 数据类型 描述
query 字符串 用于搜索的查询字符串。必填。
分析器 字符串 用于将查询字符串文本分词的 分析器。默认值为为 default_field 指定的索引时分析器。如果未为 default_field 指定分析器,则 analyzer 是索引的默认分析器。有关 index.query.default_field 的更多信息,请参阅 动态索引级别索引设置
slop 0(默认值)或正整数 控制查询中单词的乱序程度,同时仍被视为匹配。来自 Lucene 文档:“查询短语中单词之间允许存在的其他单词数量。例如,交换两个单词的顺序需要两次移动(第一次移动将单词放在彼此之上),因此为了允许短语的重新排序,slop 必须至少为二。值为零表示需要精确匹配。”
zero_terms_query 字符串 在某些情况下,分析器会从查询字符串中删除所有词条。例如,stop 分析器会从字符串 an but this 中删除所有词条。在这种情况下,zero_terms_query 指定是匹配零文档 (none) 还是所有文档 (all)。有效值为 noneall。默认值为 none
剩余 350 字符

有问题?

想贡献?