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

发布时间:2026/6/25 8:02:06

Elasticsearch:如何在 Elastic AI Builder 里使用 DSL 来查询 Elasticsearch 我们知道目前在 Elastic AI Builder 里我们创建 tools 的时候没有 DSL 的选项目前它只支持 ES|QLIndex searchWorkflow 及 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 文件进行配置.envELASTICSEARCH_ENDPOINThttps://localhost:9200 ELASTICSEARCH_API_KEYWVRmNU1wMEJsc01KdjlmdDZ0ZEI6Z2dEMU5UZWFPenF0b3RqaF85RWtNQQ # 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我们可以创建一个 workflowname: 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 服务所使用祝大家学习愉快

相关新闻