index_options

编辑

index_options 参数控制将哪些信息添加到倒排索引中,以用于搜索和高亮显示。只有基于词项的字段类型,如 textkeyword 支持此配置。

该参数接受以下值之一。每个值都会检索之前列出的值的信息。例如,freqs 包含 docspositions 包含 freqsdocs

docs
仅索引文档编号。可以回答问题 *此字段中是否存在此词项?*
freqs
索引文档编号和词项频率。词项频率用于对重复的词项进行高于单个词项的评分。
positions(默认)
索引文档编号、词项频率和词项位置(或顺序)。位置可用于邻近或短语查询
offsets
索引文档编号、词项频率、位置以及起始和结束字符偏移量(将词项映射回原始字符串)。统一高亮器使用偏移量来加速高亮显示。
resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "text": {
                "type": "text",
                "index_options": "offsets"
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="1",
    document={
        "text": "Quick brown fox"
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "match": {
            "text": "brown fox"
        }
    },
    highlight={
        "fields": {
            "text": {}
        }
    },
)
print(resp2)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        text: {
          type: 'text',
          index_options: 'offsets'
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    text: 'Quick brown fox'
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match: {
        text: 'brown fox'
      }
    },
    highlight: {
      fields: {
        text: {}
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      text: {
        type: "text",
        index_options: "offsets",
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    text: "Quick brown fox",
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    match: {
      text: "brown fox",
    },
  },
  highlight: {
    fields: {
      text: {},
    },
  },
});
console.log(response2);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "index_options": "offsets"
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "text": "Quick brown fox"
}

GET my-index-000001/_search
{
  "query": {
    "match": {
      "text": "brown fox"
    }
  },
  "highlight": {
    "fields": {
      "text": {} 
    }
  }
}

text 字段默认使用 postings 进行高亮显示,因为索引了 offsets