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

祝大家学习愉快!

相关推荐
ZhengEnCi6 小时前
09bad-斯坦福CS336作业一-构建优化器
人工智能
哥不是小萝莉6 小时前
一文读懂 OpenAI Codex 源码的原理、架构与未来
ai
ZhengEnCi6 小时前
09bac-斯坦福CS336作业一-实现训练损失计算
人工智能
冬奇Lab7 小时前
Skill 系列(01):Skill 评测体系——如何量化一个 AI Skill 的质量
人工智能
IT_陈寒10 小时前
Redis内存爆了,原来我漏掉了这个致命配置
前端·人工智能·后端
大大大大晴天10 小时前
Hudi技术内幕:Key Generation原理与实践
大数据
用户35218024547511 小时前
🎆从 Prompt 到 Skill:让 Spring AI Agent 学会"装新技能"
人工智能·spring boot·ai编程
米小虾12 小时前
手把手教你搭建第一个生产级AI Agent:从选型到实战的完整指南
人工智能·agent
任沫12 小时前
Agent之Function Call
javascript·人工智能·go
米小虾12 小时前
2026年AI Agent全面爆发:从开源生态到企业级应用的进化之路
人工智能·agent