Link Search Menu Expand Document Documentation Menu

即输即搜字段类型

1.0 版引入

即输即搜字段类型提供使用前缀和中缀补全的即输即搜功能。

示例

映射即输即搜字段会为此字段创建 N-gram 子字段,其中 N 的范围是 [2, max_shingle_size]。此外,它还会创建一个索引前缀子字段。

创建带有即输即搜字段的映射

PUT books
{
  "mappings": {
    "properties": {
      "suggestions": {
        "type": "search_as_you_type"
      }
    }
  }
}

除了 suggestions 字段,这还会创建 suggestions._2gramsuggestions._3gramsuggestions._index_prefix 字段。

索引带有即输即搜字段的文档

PUT books/_doc/1
{
  "suggestions": "one two three four"
}

要匹配任何顺序的词条,请使用 bool_prefix 或 multi-match 查询。这些查询会将搜索词条按指定顺序出现的文档排在乱序出现的文档之前。

GET books/_search
{
  "query": {
    "multi_match": {
      "query": "tw one",
      "type": "bool_prefix",
      "fields": [
        "suggestions",
        "suggestions._2gram",
        "suggestions._3gram"
      ]
    }
  }
}

响应包含匹配的文档

{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "suggestions" : "one two three four"
        }
      }
    ]
  }
}

要按顺序匹配词条,请使用 match_phrase_prefix 查询

GET books/_search
{
  "query": {
    "match_phrase_prefix": {
      "suggestions": "two th"
    }
  }
}

响应包含匹配的文档

{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.4793051,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.4793051,
        "_source" : {
          "suggestions" : "one two three four"
        }
      }
    ]
  }
}

要精确匹配最后一个词条,请使用 match_phrase 查询

GET books/_search
{
  "query": {
    "match_phrase": {
      "suggestions": "four"
    }
  }
}

响应

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "suggestions" : "one two three four"
        }
      }
    ]
  }
}

参数

下表列出了即输即搜字段类型接受的参数。所有参数均为可选。

参数 描述
分析器 用于此字段的分析器。默认情况下,它将在索引时和搜索时使用。要在搜索时覆盖它,请设置 search_analyzer 参数。默认值为 standard 分析器,它使用基于语法的分词,并基于 Unicode 文本分段算法。配置根字段和子字段。
index 一个布尔值,指定字段是否可搜索。默认为 true。配置根字段和子字段。
index_options 指定要存储在索引中用于搜索和高亮显示的信息。有效值:docs(仅文档编号),freqs(文档编号和词频),positions(文档编号、词频和词位置),offsets(文档编号、词频、词位置以及起始和结束字符偏移量)。默认值为 positions。配置根字段和子字段。
max_shingle_size 一个整数,指定最大的 N-gram 大小。有效值范围为 [2, 4]。将创建的 N-gram 范围是 [2, max_shingle_size]。默认值为 3,这将创建 2-gram 和 3-gram。较大的 max_shingle_size 值对于更具体的查询效果更好,但会导致更大的索引大小。
norms 一个布尔值,指定在计算相关性分数时是否应使用字段长度。配置根字段和 N-gram 子字段(默认为 false)。不配置前缀子字段(在前缀子字段中,normsfalse)。
search_analyzer 搜索时使用的分析器。默认为 analyzer 参数中指定的分析器。配置根字段和子字段。
search_quote_analyzer 搜索短语时使用的分析器。默认为 analyzer 参数中指定的分析器。配置根字段和子字段。
similarity 用于计算相关性分数的排序算法。默认为 BM25。配置根字段和子字段。
存储 一个布尔值,指定字段值是否应存储并可与 _source 字段分开检索。默认为 false。仅配置根字段。
term_vector 一个布尔值,指定是否应存储此字段的词条向量。默认为 no。配置根字段和 N-gram 子字段。不配置前缀子字段。
剩余 350 字符

有问题?

想贡献吗?