我们知道目前在 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 来实现同样的功能。
创建索引
我们的源码在地址: github.com/liu-xiao-gu…。我们使用如下的命令来下载源码:
`git clone https://github.com/liu-xiao-guo/internal_knowkedge_search`AI写代码
然后,我们需要针对 .env 文件进行配置:
.env
`
1. ELASTICSEARCH_ENDPOINT="https://localhost:9200"
2. ELASTICSEARCH_API_KEY="WVRmNU1wMEJsc01KdjlmdDZ0ZEI6Z2dEMU5UZWFPenF0b3RqaF85RWtNQQ=="
3. # Optional: Path to your CA certificate for secure connections.
4. # If left empty, certificate verification will be disabled (not for production).
5. ES_CA_CERTS_PATH=""
`AI写代码
注意:我们需要根据自己的配置做相应的修改。
我们使用如下的命令来创建一个索引:
`python ingest.py`AI写代码
我们可以在 Kibana 查看已经写入的文档:
创建 workflow
我们可以创建一个 workflow:
`
1. name: Knowledge Base Search
2. enabled: true
3. description: |
4. Searches the 'documents' index for knowledge base articles based on a user query
6. triggers:
7. - type: manual
9. inputs:
10. - name: user_query
11. type: string
12. required: true
13. description: knowledge to search for
15. consts:
16. index_name: documents
18. steps:
19. - name: search_knowledge_base
20. type: elasticsearch.request
21. with:
22. method: POST
23. path: /{{consts.index_name}}/_search
24. headers:
25. Content-Type: application/json
26. body: |
27. {
28. "size": 50,
29. "query": {
30. "bool": {
31. "must": [
32. {
33. "multi_match": {
34. "query": "{{inputs.user_query}}",
35. "fields": ["title^2", "content", "tags"],
36. "fuzziness": "AUTO"
37. }
38. }
39. ],
40. "should": [
41. {
42. "match_phrase": {
43. "title": {
44. "query": "{{inputs.user_query}}",
45. "boost": 2
46. }
47. }
48. }
49. ]
50. }
51. },
52. "highlight": {
53. "fields": {
54. "title": {},
55. "content": {}
56. }
57. }
58. }
60. - name: generate_summary
61. type: ai.prompt
62. with:
63. temperature: 0.2
64. prompt: |
65. 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.
67. Question: {{inputs.user_query}}
69. Relevant Documents:
70. {%- for hit in steps.search_knowledge_base.output.hits.hits limit:5 -%}
71. [Document {{ loop.index }}: {{ hit._source.title }}]
72. {{ hit._source.content }}
74. ---
76. {% endfor -%}
78. - name: display_top_results
79. type: console
80. with:
81. message: |-
82. {%- assign total_hits = steps.search_knowledge_base.output.hits.total.value -%}
83. {%- if total_hits == 0 -%}
84. No results found for query: '{{ inputs.user_query }}'
85. {%- else -%}
86. {%- assign summary = steps.generate_summary.output.content -%}
87. {{ summary }}
89. ---
90. Sources Used ({{ total_hits }} found):
92. {% for hit in steps.search_knowledge_base.output.hits.hits limit:5 -%}
93. - [{{ forloop.index }}] {{ hit._source.title }}
94. {% endfor -%}
95. {%- endif -%}
`AI写代码收起代码块
如上所示,我们使用了 DSL 来查询我们的数据库。
创建 tool
我们可以创建一个如下的一个工具:
在 Agent 里使用这个工具:
我们再接下来创建一个 agent,并在这个 agent 里使用这个工具:
很显然,我们非常容易地使用 Elastic Workflow 来创建 DSL query,并在 AI builder 对它进行使用。当然,我们的工具也可以被其它的 MCP 服务所使用:
祝大家学习愉快!