Elasticsearch 的结构化文档配置 - 递归分块实践

今天我发表的一篇文章 "在 Elasticsearch 中为结构化文档配置递归分块" 看着很简单。可能很多开发者还是不能得其要领,特别是在最新的版本中,我们的向量在默认的情况下是不在 source 里进行展示的。很难理解其中精髓。我在这篇文章里,使用一个具体的例子来进行展示。

例子

我们可以参考官方文档 "Inference integrations" 来进行展示。首先,我们选择系统自带的 ELSER 模型来进行展示。我们必须安装好 ELSER 模型:

首先我们定义如下的一个 inference endpoint:

bash 复制代码
`

1.  PUT _inference/sparse_embedding/recursive_markdown_chunks
2.  {
3.    "service": "elasticsearch",
4.    "service_settings": {
5.      "model_id": ".elser_model_2",
6.      "num_allocations": 1,
7.      "num_threads": 1
8.    },
9.    "chunking_settings": {
10.      "strategy": "recursive",
11.      "max_chunk_size": 25,
12.      "separators": [
13.        "\n# ",
14.        "\n## "
15.      ]
16.    }
17.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

我们可以参考文档来了解 recursive 是如何工作的。递归策略根据可配置的分隔符模式列表(例如换行符或 Markdown 标题)拆分输入文本。分块器按顺序应用这些分隔符,递归地拆分超过 max_chunk_size 单词限制的任何分块。如果没有分隔符生成足够小的分块,该策略将退回到句子级拆分。

在上面,我们使用了 custom separator group:也就是基于第一第二级的 heading 来进行分隔提前。

接下来,我们定义一个索引:

bash 复制代码
`

1.  PUT recursive_markdown_vectors
2.  {
3.    "mappings": {
4.      "properties": {
5.        "content": {
6.          "type": "text",
7.          "copy_to": "inference_field"
8.        },
9.        "inference_field": {
10.          "type": "semantic_text",
11.          "inference_id": "recursive_markdown_chunks"
12.        }
13.      }
14.    }
15.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

然后,我们写入文档:

less 复制代码
`

1.  POST recursive_markdown_vectors/_doc/1
2.  {
3.    "content": "# First Header
4.  This first test sentence has ten total words in it.

6.  ## Second header
7.  This second test sentence has ten total words in it.

9.  # Third Header
10.  This third test sendtence has ten total words in it.

12.  ## Fourth Header
13.  This Fourth test sendtence has ten total words in it.

16.  ## Fifth Header
17.  This fifth test sentence has ten total words in it. This sixth test sentence has ten total words in it. This seventh test sentence has ten total words in it."
18.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

这样我们就大功告成了。

我们可以使用如下的查询来进行展示:

bash 复制代码
`

1.  GET recursive_markdown_vectors/_search?filter_path=**.hits
2.  {
3.    "fields": [
4.      {
5.        "field": "inference_field",
6.        "format": "chunks"
7.      }
8.    ]
9.  }

`AI写代码

注意:上面的 fields 只对 9.2+ 及以上版本起作用。

我们可以看到如下的查询结果:

从上面的结果中,我们可以看到有 3 个 chunks。

我们接下来把 max_chunk_size 设置为 10,看看最后的结果:

bash 复制代码
`

1.  PUT _inference/sparse_embedding/recursive_markdown_chunks
2.  {
3.    "service": "elasticsearch",
4.    "service_settings": {
5.      "model_id": ".elser_model_2",
6.      "num_allocations": 1,
7.      "num_threads": 1
8.    },
9.    "chunking_settings": {
10.      "strategy": "recursive",
11.      "max_chunk_size": 10,
12.      "separators": [
13.        "\n# ",
14.        "\n## "
15.      ]
16.    }
17.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

很显然,它和我们之前的分块还是有点不一样。我们可以试试不同的值来看看。

bash 复制代码
`

1.  PUT _inference/sparse_embedding/recursive_markdown_chunks
2.  {
3.    "service": "elasticsearch",
4.    "service_settings": {
5.      "model_id": ".elser_model_2",
6.      "num_allocations": 1,
7.      "num_threads": 1
8.    },
9.    "chunking_settings": {
10.      "strategy": "recursive",
11.      "max_chunk_size": 35,
12.      "separators": [
13.        "\n# ",
14.        "\n## "
15.      ]
16.    }
17.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

我们也可以尝试使用 "separator_group": "markdown":

bash 复制代码
`

1.  PUT _inference/sparse_embedding/recursive_markdown_chunks
2.  {
3.    "service": "elasticsearch",
4.    "service_settings": {
5.      "model_id": ".elser_model_2",
6.      "num_allocations": 1,
7.      "num_threads": 1
8.    },
9.    "chunking_settings": {
10.      "strategy": "recursive",
11.      "max_chunk_size": 25,
12.      "separator_group": "markdown"
13.    }
14.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

可以看出来,它的结果和我们之前第一个结果的没有什么不一样的,这是因为 markdown 所包含的 separators 里也是对 heading 进行分段的。

相关推荐
yumgpkpm44 分钟前
华为 GaussDB 商业版(本地部署)部署方案及相关步骤
hive·hadoop·redis·elasticsearch·华为·kafka·gaussdb
潇潇云起12 小时前
【elastic-start-local 本地docker 30天后需要重新试用处理】
elasticsearch
大厂技术总监下海15 小时前
根治LLM胡说八道!用 Elasticsearch 构建 RAG,给你一个“有据可查”的AI
大数据·elasticsearch·开源
f***281417 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins
Elasticsearch1 天前
使用 Elastic Agent 混合摄取加速 Otel 采用
elasticsearch
W***r261 天前
SpringBoot整合easy-es
spring boot·后端·elasticsearch
それども1 天前
ES KQL 支持词频统计吗
elasticsearch
zhyf1191 天前
Max395(ubuntu24.04)AMD显卡GLM-4.7-UD-IQ1-M量化模型部署手册
大数据·elasticsearch·搜索引擎
学好statistics和DS1 天前
Git 同步冲突
大数据·git·elasticsearch
chen<>2 天前
Git原理与应用
大数据·git·elasticsearch·svn