
# 关联模型与向量索引
通过将已配置的模型服务关联到向量索引，可以为索引赋予AI处理能力。该配置通过索引级别的设置实现以下核心功能：
- 向量化处理：在数据写入时自动调用Embedding模型服务将文本转换为向量。
- 智能搜索：在查询时结合向量相似度和传统文本相关性进行混合检索。
- 结果优化：通过Rerank模型服务对搜索结果进行智能重排序。
#### 前提条件
已创建并完成模型服务的配置，操作指导请参见[配置模型服务](https://support.huaweicloud.com/usermanual-css/css_01_0282.html)。
#### 登录Kibana
登录Kibana进入命令执行页面。Elasticsearch集群支持多种客户端访问，本文仅以CSS服务集成的Kibana为例介绍配置指导。
1. 登录[云搜索服务管理控制台](https://console.huaweicloud.com/elasticsearch/)。
2. 在左侧导航栏，选择"集群管理 \> Elasticsearch"。
3. 在集群列表，选择目标集群，单击操作列的"Kibana"，登录Kibana。
4. 在Kibana左侧导航栏选择"Dev Tools"，进入操作页面。 控制台左侧是命令输入框，其右侧的三角形图标为执行按钮，右侧区域则显示执行结果。
   
 
#### 创建并配置向量索引
在Kibana中执行以下命令，创建并配置向量索引，通过索引级别的配置项，实现集群在写入和查询时自动关联到模型服务，实现索引级别的AI处理能力。
```
PUT {index_name}
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "desc": {
        "type": "text"
      },
      "content": {
        "type": "text"
      },
      "author": {
        "type": "keyword"
      }
    }
  },
  "settings": {
    "index.vector": true,    
    "index.inference.field": [
      "title:100",
      "content:30",
      "desc:30"
    ],
    "index.inference.semantic_search_enabled": true,
    "index.inference.embedding_model": "pangu_vector",
    "index.inference.reorder_enabled": true,
    "index.inference.reorder_model": "pangu_ranking",
    "index.inference.semantic_search_type": "vector",
    "index.inference.semantic_search_topk": 20
  }
}
```
表1参数说明 
| 参数                                      | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|:---|:---|:---|:---|
| index_name                              | 是    | String  | 索引名称。自定义，如pangu_index。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| index.vector                            | 是    | Boolean | 启用语义向量查询。 此处必须配置为"true"，启用语义向量查询，否则无法执行语义查询。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| index.inference.semantic_search_enabled | 否    | Boolean | 启用语义向量搜索。 取值范围： - true：启用语义向量查询，数据导入和查询都会经过Embedding模型服务进行向量化。  - false：不启用向量查询，查询使用Elasticsearch文本查询。   默认值：false                                                                                                                                 |
| index.inference.embedding_model         | 否    | String  | Embedding模型服务的名称，要和[配置模型服务](https://support.huaweicloud.com/usermanual-css/css_01_0282.html)中的Embedding模型服务的名称保持一致。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| index.inference.semantic_search_topk    | 否    | Integer | 向量查询的Top K。 Top K的取值越大，召回的向量越多，精度越高，但也会消耗更多的性能。 取值范围： - "index.inference.semantic_search_type"为"vector"时：1\~150  - "index.inference.semantic_search_type"为"merge"时：1\~75   默认值：100                                                   |
| index.inference.search_timeout          | 否    | Integer | 向量查询的超时时间，超时会请求失败。 默认值：30 单位：秒                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| index.inference.reorder_enabled         | 否    | Boolean | 精排模型服务开关。 取值范围： - true：开启Rerank模型服务，查询结果使用精排模型进行重新排序。  - false：关闭Rerank模型服务。   默认值：false                                                                                                                                                                                                                                                |
| index.inference.reorder_model           | 否    | String  | 精排模型服务的名称。要和[配置模型服务](https://support.huaweicloud.com/usermanual-css/css_01_0282.html)中的精排模型服务的名称保持一致。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| index.inference.semantic_search_type    | 否    | String  | 语义向量搜索的类型。 取值范围： - vector：向量搜索，只使用语义查询。该搜索类型完全依赖向量相似度检索、忽略文本匹配的场景。  - merge：双路召回，同时执行文本匹配和向量搜索，合并结果后通过精排模型重新排序。该搜索类型可以提升召回率，避免单一检索方式的局限性。 选择merge时，必须启用精排模型服务（即"index.inference.reorder_enabled"配置为"true"），否则返回向量查询结果。    默认值：vector                                       |
| index.inference.field                   | 是    | List    | 转换向量文本字段。配置需要转换为向量的文本字段和权重。 - 在写入文档时，如果文档中出现了field配置，则会调用Embedding模型服务将文档转换为语义向量存储，否则直接写入。  - 在multi_match查询时，先判断query中是否有field字段配置，如果出现则进行查询改写，否则直接查询。   配置举例： ``` "index.inference.field": [ "title:100", "desc:80", "content:30" ] ``` 表示当文档中出现title、desc、content三个字段时，会被转换为语义向量。 |
| index.inference.multi_fields_merge_type | 否    | String  | 多字段拼接类型。 取值范围： - text_merge：表示将文本拼接成一个字段后进行Embedding。  - embedding_merge：表示对多个字段按照重要性，先Embedding再合并。   默认值：text_merge                                                                                                                                                                                                                    |
| index.inference.vector_search_mode      | 否    | String  | 向量查询模式。 取值范围： - auto：向量查询模式由向量数据库决定。  - script：粗排先使用文本查询，对文本查询的结果进行向量查询排序。  - bool：粗排使用文本查询和向量分别查询，再将查询结果合并。   默认值：auto                                                                                                                       |
| index.inference.reorder_vector_topn     | 否    | Integer | 精排时获取的向量查询结果的数量。 使用双路召回时，建议向量查询的结果数配置为文本查询的结果数的4倍。 取值范围：0\~1000 默认值：0                                                                                                                                                                                                                                                                                                                                                                                                                     |
| index.inference.reorder_text_topn       | 否    | Integer | 精排时获取的文本查询的结果数量。 使用双路召回时，建议向量查询的结果数配置为文本查询的结果数的4倍。 取值范围：0\~1000 默认值：0                                                                                                                                                                                                                                                                                                                                                                                                                     |
   
