索引选项
index_options
映射参数控制文本字段倒排索引中存储的详细程度。此设置直接影响索引大小以及评分、短语匹配和高亮显示的功能。
index_options
参数具有以下有效值。
值 | 存储 | 描述 |
---|---|---|
docs | 仅文档 ID | 仅索引词项在文档集中的存在。不存储词频或位置信息。最小化索引大小;适用于简单的存在性检查。 |
freqs | 文档 ID + 词频 | 添加词频信息。有助于提高相关性评分,但不支持短语或邻近查询。 |
positions | 文档 ID + 词频 + 词项位置 | 包括词项在文档中的顺序和位置。短语查询和邻近搜索所需。 |
offsets | 文档 ID + 词频 + 词项位置 + 偏移量 | 最详细。为匹配的词项添加字符偏移量。对高亮显示有用,但会增加存储大小。 |
默认情况下,文本字段使用 positions
选项进行索引,以平衡功能和索引大小。
示例:在字段上设置 index_options
创建一个名为 products
的索引,其中 description
字段的 index_options
设置为 positions
PUT /products
{
"mappings": {
"properties": {
"description": {
"type": "text",
"index_options": "positions"
}
}
}
}
索引一个 description
字段包含内容的文档
PUT /products/_doc/1
{
"description": "This is a sample product description with several terms."
}
对 description
字段运行短语查询
POST /products/_search
{
"query": {
"match_phrase": {
"description": "product description"
}
}
}
短语查询成功匹配了文档,这展示了 index_options
中的 positions
设置如何实现在 description
字段中的精确短语匹配。
{
...
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "products",
"_id": "1",
"_score": 0.5753642,
"_source": {
"description": "This is a sample product description with several terms."
}
}
]
}
}