Elasticsearch:如何在 Elastic AI Builder 里使用 DSL 来查询 Elasticsearch

我们知道目前在 Elastic AI Builder 里,我们创建 tools 的时候,没有 DSL 的选项:

目前,它只支持 ES|QL,Index search,Workflow 及 MCP。当针对我们的 Elasticsearch 里的索引进行 DSL 查询时,我们感觉到无能为力,毕竟 DSL 是很多开发者心里最熟悉的查询语言,虽然 ES|QL 是我们最终的目标。那么我们该如何做呢?

答案就是:使用 Elastic Workflow 来完成。

在最近的一篇文章 "使用 TypeScript 创建 Elasticsearch MCP 服务器",它使用了 MCP 来完成这个 DSL 的查询。当然一个 MCP 的设计不是那么容易的,而且还需要语言设计。在下面,我们使用 Elastic Workflow 来实现同样的功能。

创建索引

我们的源码在地址: https://github.com/liu-xiao-guo/internal_knowkedge_search。我们使用如下的命令来下载源码:

复制代码
git clone https://github.com/liu-xiao-guo/internal_knowkedge_search

然后,我们需要针对 .env 文件进行配置:

.env

复制代码
ELASTICSEARCH_ENDPOINT="https://localhost:9200"
ELASTICSEARCH_API_KEY="WVRmNU1wMEJsc01KdjlmdDZ0ZEI6Z2dEMU5UZWFPenF0b3RqaF85RWtNQQ=="
# Optional: Path to your CA certificate for secure connections.
# If left empty, certificate verification will be disabled (not for production).
ES_CA_CERTS_PATH=""

注意:我们需要根据自己的配置做相应的修改。

我们使用如下的命令来创建一个索引:

复制代码
python ingest.py

我们可以在 Kibana 查看已经写入的文档:

创建 workflow

我们可以创建一个 workflow:

复制代码
name: Knowledge Base Search
enabled: true
description: | 
  Searches the 'documents' index for knowledge base articles based on a user query

triggers:
  - type: manual

inputs:
  - name: user_query
    type: string
    required: true
    description: knowledge to search for

consts:
  index_name: documents

steps:
  - name: search_knowledge_base
    type: elasticsearch.request
    with:
      method: POST
      path: /{{consts.index_name}}/_search
      headers:
        Content-Type: application/json
      body: |
        {
          "size": 50,
          "query": {
            "bool": {
              "must": [
                {
                  "multi_match": {
                    "query": "{{inputs.user_query}}",
                    "fields": ["title^2", "content", "tags"],
                    "fuzziness": "AUTO"
                  }
                }
              ],
              "should": [
                {
                  "match_phrase": {
                    "title": {
                      "query": "{{inputs.user_query}}",
                      "boost": 2
                    }
                  }
                }
              ]
            }
          },
          "highlight": {
            "fields": {
              "title": {},
              "content": {}
            }
          }
        }

  - name: generate_summary
    type: ai.prompt
    with:
      temperature: 0.2
      prompt: |
        You are a helpful assistant that answers questions based on provided documents. Summarize the provided search results to answer a question and return citation metadata for the sources used.

        Question: {{inputs.user_query}}

        Relevant Documents:
        {%- for hit in steps.search_knowledge_base.output.hits.hits limit:5 -%}
        [Document {{ loop.index }}: {{ hit._source.title }}]
        {{ hit._source.content }}

        ---

        {% endfor -%}

  - name: display_top_results
    type: console
    with:
      message: |-
        {%- assign total_hits = steps.search_knowledge_base.output.hits.total.value -%}
        {%- if total_hits == 0 -%}
        No results found for query: '{{ inputs.user_query }}'
        {%- else -%}
        {%- assign summary = steps.generate_summary.output.content -%}
        {{ summary }}

        ---
        Sources Used ({{ total_hits }} found):
        
        {% for hit in steps.search_knowledge_base.output.hits.hits limit:5 -%}
        - [{{ forloop.index }}] {{ hit._source.title }}
        {% endfor -%}
        {%- endif -%}

如上所示,我们使用了 DSL 来查询我们的数据库。

复制代码
Search for documents about authentication methods and role-based access control

创建 tool

我们可以创建一个如下的一个工具:

在 Agent 里使用这个工具:

我们再接下来创建一个 agent,并在这个 agent 里使用这个工具:

很显然,我们非常容易地使用 Elastic Workflow 来创建 DSL query,并在 AI builder 对它进行使用。当然,我们的工具也可以被其它的 MCP 服务所使用:

祝大家学习愉快!

相关推荐
数字融合2 分钟前
透明化视频驱动化工生产动态三维重建管控技术
大数据·人工智能·python
七夜zippoe1 小时前
多轮对话管理:Session/记忆在 Java 后端怎么存(存储设计实战)
java·开发语言·ai·session·记忆
演艺大赏1 小时前
两个 NameNode 一起起不来以及Hadoop 3.3.2 里那个认不出自家 JournalNode 的 Servlet
大数据·hadoop
知行产研1 小时前
会感知、能协同,金科【钻孔】智能化路线 | bauma CHINA 2026
大数据·人工智能
谙弆悕博士1 小时前
从“预测下一个词”到“预测世界下一状态”:世界模型是不是下一个AGI叙事?
人工智能·gpt·深度学习·神经网络·agi·jepa·genie 4
敖老师1 小时前
企业知识库 × 飞书机器人 —— 从 0 到 1 接入
人工智能
AI前端工程狮1 小时前
我给 MCP 长任务做了持久化,结果造出一堆僵尸
人工智能·程序员
演艺大赏1 小时前
RESTServer 起不来,日志只有一行:headerCacheSize 65536 exceeds maximum value of 65534
大数据·hadoop·hbase
黑科技iOS上架1 小时前
iOS游戏换皮上架困境:4.3同质化拒审
人工智能·ios·审核
咕泡科技1 小时前
关于机器学习,如何通俗理解全连接层(fully connected layer)?
人工智能·深度学习·机器学习