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 服务所使用:

祝大家学习愉快!

相关推荐
前沿科技说i9 小时前
2026年AI大模型API中转站:主流服务商性能与成本
人工智能
黄啊码11 小时前
【黄啊码】程序员真正该担心的,不是 AI 会写代码
人工智能
weixin_4684668512 小时前
Ava 2.0 智能应用场景落地指南
人工智能·自然语言处理·大模型·智能交互·ava
John_ToDebug12 小时前
MCP 深度解析:大模型的“万能插头”
人工智能·经验分享·ai
浦信仿真大讲堂12 小时前
CST 仿真软件与 AI 融合的工程应用实战
人工智能·仿真软件·达索仿真·达索软件
mit6.82412 小时前
A Software Engineer‘s Apology | CODA
人工智能
段一凡-华北理工大学12 小时前
2026 高炉炼铁智能化技术全景与演进路径~系列文章11:演进路径与行业未来
大数据·网络·人工智能·算法·工业智能体·高炉炼铁智能化
小脑斧12313 小时前
AI技能化落地:从对话式大模型到可生产、可复用的AI工程体系
人工智能·skills·openclaw·hermes·marvis
西陵13 小时前
Agent 为什么会陷入 Doom Loop?OpenClaw 的破解之道
前端·人工智能·ai编程
飞哥数智坊13 小时前
动动嘴皮子就把事干了,Mic Air + TRAE SOLO 让我越来越懒
人工智能