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 小时前
ultralytics yolo入门实践
yolo·ai
musicml2 小时前
从 Vibe Coding 到 SDD(规范驱动开发):AI 原生时代的软件工程化实践
人工智能·驱动开发·软件工程
Deepoch2 小时前
Deepoc具身模型:重塑无人机无遥控器作业
人工智能·科技·无人机·具身模型·deepoc
i建模2 小时前
Claude Code在编程之外的能力
人工智能
言之。2 小时前
用Claude Code搞定产品上线
人工智能
tian_jiangnan2 小时前
flink大数据15天速成教程
大数据·flink
HIT_Weston2 小时前
31、【Agent】【OpenCode】模型配置(OpenRouter&OpenCode)
人工智能·agent·opencode
Coder个人博客2 小时前
06_apollo_third_party子模块整体软件架构深入分析文档
linux·人工智能·架构
uzong2 小时前
ClaudeCode 入门详细教程,手把手带你Vibe Coding
前端·人工智能