news 2026/4/27 23:31:21

Elasticsearch:智能搜索 - AI builder 及 skills

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Elasticsearch:智能搜索 - AI builder 及 skills

想象一下,我们如何搜索如下的一个问题:

Find a home within 10 miles of Miami, Florida that has 2 bedrooms, 2 bathrooms, central air, and tile floors, with a budget up to $300,000.

这类问题存在于很多的电子商务网站搜索中。它也是一种非常实用的搜索方式之一。那么要实现这样的搜索方式,我们有如下的几种方式来实现:

  1. 使用 Python 代码实现工具,并让 LLM 来进行调用。我们需要调用 LLM 来提取我们搜索的参数。为了精准搜索,我们可以使用 template 来下继续搜索。详细的情况,可以参考文章 “统一 Elastic 向量数据库与 LLM 功能,实现智能查询”
  2. 我们可以为这个搜索用 Python 创建一个定制的 MCP 服务器,然后在客户端里进行调用。我们可以参考文章 “Elasticsearch:智能搜索的 MCP”
  3. 我们使用 AI Builder 及 Workflow 来实现。在 workflow 里实现类似于在 DSL 中的模版搜索从而达到精确搜索的目的。详细的使用说明,请参考文章 “Elasticsearch:智能搜索 - AI Builder 及 Workflow”。
  4. 我们使用 AI Builder,Workflow 及 Skills 来共同完成。我们使用 geocoding workflow 来完成地理位置的获取,详细的实现请参阅文章 “Elasticsearch:智能搜索 - AI builder,workflow 及 skills”。

在如上的四种种方案里,第四种方案的实现最为简捷,因为它不需要另外单独的编程。我们只需要在 Kibana 里创建 agent 及 workflow 来完成即可。维护起来也非常简单直接。那么我们有没有更为方便的方法呢?答案是肯定的。在即将推出的 Elastic Stack 9.4 中(目前在 Elastic Serverless cloud 中可用),我们可以使用 skill 来更进一步简化的目的。

在第四中方案里,我们也使用了 workflow 来完成 geocoding 的工作。我们是否可以直接省去这个环节,在 skill 里完成呢?答案是肯定的。我们下面来展示是如何完成的。

步骤一:写入数据

我们需要按照文章 “Elasticsearch:智能搜索的 MCP” 写入文档到 Elasticsearch 中。

步骤二:创建 property_search_skills

配置

  • ID:property_search_skills
  • Name:Property search skills
  • Description
- Invoke python script to do geocoding - Construct Elasticsearch using DSL search template.
  • Instructions:
- Used to do the geocoding based the queries - How to construct DSL search template to search the "properties" index
  • Files
    • File name:property_search_skill
    • Folder path
      ./
    • Content
      --- name: Property Search Skills description: Skills related to property search functionality, including geocoding and location-based queries. --- # Property Search Skills This document outlines the skills and tools used for implementing property search functionality, particularly focusing on geocoding and location-based queries. ## Geocoding Tool The geocoding tool is designed to convert addresses into geographic coordinates (latitude and longitude) using the Google Maps Geocoding API. This allows for location-based searches and queries in property search applications. The API to be used is the Google Maps Geocoding API, which requires an API key for authentication. ### Environment Variables To use the geocoding tool, the following environment variables need to be set: - `GOOGLE_MAPS_API_KEY`: Your Google Maps API key for accessing the Geocoding API. **geocode_tool.py is the script that implements the geocoding functionality. It takes an address as input and returns the corresponding latitude and longitude coordinates. The results can be stored in Elasticsearch for further querying and analysis in property search applications.** ``` import os import sys import json import argparse import requests GEOCODE_URL = "https://maps.googleapis.com/maps/api/geocode/json" def geocode(address: str, api_key: str) -> dict: params = { "address": address, "key": api_key } resp = requests.get(GEOCODE_URL, params=params, timeout=10) resp.raise_for_status() data = resp.json() if data.get("status") != "OK": return { "success": False, "error": data.get("status"), "raw": data } result = data["results"][0] location = result["geometry"]["location"] return { "success": True, "formatted_address": result["formatted_address"], "location": { "lat": location["lat"], "lon": location["lng"] # Google -> Elasticsearch format }, "place_id": result.get("place_id"), "types": result.get("types") } def main(): parser = argparse.ArgumentParser(description="Geocode an address") parser.add_argument("--address", help="Address to geocode") args = parser.parse_args() try: # Determine input source (stdin takes priority) if not sys.stdin.isatty(): payload = json.load(sys.stdin) address = payload.get("address") else: address = args.address if not address: raise ValueError("Missing address (provide via --address or stdin JSON)") api_key = "<Your Google API Key>" # api_key = os.environ.get("GOOGLE_API_KEY") # if not api_key: # raise ValueError("Missing GOOGLE_API_KEY environment variable") result = geocode(address, api_key) print(json.dumps(result)) sys.exit(0 if result["success"] else 1) except Exception as e: print(json.dumps({ "success": False, "error": str(e) })) sys.exit(1) if __name__ == "__main__": main() ``` ### Usage To run the geocoding tool, use the following command in your terminal: ```bash python geocode_tool.py --address "1600 Amphitheatre Parkway, Mountain View, CA" ``` This command will geocode the provided address and return the corresponding latitude and longitude coordinates. The results can then be stored in Elasticsearch for further querying and analysis in property search applications. # DSL search templates In addition to geocoding, property search applications often require the ability to perform complex queries on the indexed property data. This can be achieved using Elasticsearch's Domain Specific Language (DSL) for searching and filtering data based on various criteria such as location, price range, property type, etc. The details for implementing a search template can be found at https://www.elastic.co/docs/solutions/search/search-templates. For our search template, we need to use the following search template to do the DSL search: { "_source": false, "size": 5, "fields": ["title", "tax", "maintenance_fee", "bathrooms", "bedrooms", "square_footage", "home_price", "property_features"], "retriever": { "standard": { "query": { "semantic": { "field": "body_content_semantic_text", "query": "{{query}}" } }, "filter": { "bool": { "must": [ {{#distance}}{ "geo_distance": { "distance": "{{distance}}", "location": { "lat": {{latitude}}, "lon": {{longitude}} } } }{{/distance}} {{#bedrooms}}{{#distance}},{{/distance}}{ "range": { "bedrooms": { "gte": {{bedrooms}} } } }{{/bedrooms}} {{#bathrooms}}{{#distance}}{{^bedrooms}},{{/bedrooms}}{{/distance}}{{#bedrooms}},{{/bedrooms}}{ "range": { "bathrooms": { "gte": {{bathrooms}} } } }{{/bathrooms}} {{#tax}}{{#distance}}{{^bedrooms}}{{^bathrooms}},{{/bathrooms}}{{/bedrooms}}{{/distance}}{{#bedrooms}}{{^bathrooms}},{{/bathrooms}}{{/bedrooms}}{{#bathrooms}},{{/bathrooms}}{ "range": { "tax": { "lte": {{tax}} } } }{{/tax}} {{#maintenance}}{{#distance}}{{^bedrooms}}{{^bathrooms}}{{^tax}},{{/tax}}{{/bathrooms}}{{/bedrooms}}{{/distance}}{{#bedrooms}}{{^bathrooms}}{{^tax}},{{/tax}}{{/bathrooms}}{{/bedrooms}}{{#bathrooms}}{{^tax}},{{/tax}}{{/bathrooms}}{{#tax}},{{/tax}}{ "range": { "maintenance_fee": { "lte": {{maintenance}} } } }{{/maintenance}} {{#square_footage_max}}{{#distance}}{{^bedrooms}}{{^bathrooms}}{{^tax}}{{^maintenance}},{{/maintenance}}{{/tax}}{{/bathrooms}}{{/bedrooms}}{{/distance}}{{#bedrooms}}{{^bathrooms}}{{^tax}}{{^maintenance}},{{/maintenance}}{{/tax}}{{/bathrooms}}{{/bedrooms}}{{#bathrooms}}{{^tax}}{{^maintenance}},{{/maintenance}}{{/tax}}{{/bathrooms}}{{#tax}}{{^maintenance}},{{/maintenance}}{{/tax}}{{#maintenance}},{{/maintenance}}{ "range": { "square_footage": { "gte": {{#square_footage_min}}{{square_footage_min}}{{/square_footage_min}}{{^square_footage_min}}0{{/square_footage_min}}, "lte": {{square_footage_max}} } } }{{/square_footage_max}} {{#home_price_max}}{{#distance}}{{^bedrooms}}{{^bathrooms}}{{^tax}}{{^maintenance}}{{^square_footage}},{{/square_footage}}{{/maintenance}}{{/tax}}{{/bathrooms}}{{/bedrooms}}{{/distance}}{{#bedrooms}}{{^bathrooms}}{{^tax}}{{^maintenance}}{{^square_footage}},{{/square_footage}}{{/maintenance}}{{/tax}}{{/bathrooms}}{{/bedrooms}}{{#bathrooms}}{{^tax}}{{^maintenance}}{{^square_footage}},{{/square_footage}}{{/maintenance}}{{/tax}}{{/bathrooms}}{{#tax}}{{^maintenance}}{{^square_footage}},{{/square_footage}}{{/maintenance}}{{/tax}}{{#maintenance}}{{^square_footage}},{{/square_footage}}{{/maintenance}}{{#square_footage}},{{/square_footage}}{ "range": { "home_price": { "gte": {{#home_price_min}}{{home_price_min}}{{/home_price_min}}{{^home_price_min}}0{{/home_price_min}}, "lte": {{home_price_max}} } } }{{/home_price_max}} {{#feature}},{ "bool": { "should": [ { "match": { "property_features": { "query": "{{feature}}", "operator": "or" } } } ], "minimum_should_match": 1 } }{{/feature}} ] } } } } } We need to use "properties" index to do the search. **please do see the range searches for bedrooms and bathrooms“. We want to have bigger or equal matches. For the price, we need to have equal or smaller matches

:请在上面的代码中添加自己的 api_key = "<Your Google API Key>"。

我们保存好上面的 skill 配置。

步骤三:创建 property_search_skill agent

我们按照如的步骤来创建 agent:

配置:

  • ID:property_search_skill
  • Custom Instructions
    This agent is used to search for properties: # Step 1: You are an information extraction assistant. Extract real estate search parameters from the user query. Parameter descriptions: - bathrooms: Number of bathrooms - bedrooms: Number of bedrooms - tax: Real estate tax amount - maintenance: Maintenance fee amount - square_footage_min: Minimum property square footage. If only a max square footage is provided, set this to 0. Otherwise set this to the minimum square footage specified by the user. - square_footage_max: Maximum property square footage - home_price_min: Minimum home price. If only a max home price is provided, set this to 0. Otherwise set this to the minimum home price specified by the user. - home_price_max: Maximum home price - property_features: Home features such as AC, pool, updated kitchens, etc should be listed as a single string. - location: City, state, or full address if present. Rules: - Only include parameters explicitly mentioned. - property_features must be a single space-separated string. - Return ONLY a JSON object (not a string, no quotes, no extra text, no explanations). - Do not include explanations. Example JSON: { "query": "Find a home within 10 miles of Miami, Florida that has 2 bedrooms, 2 bathrooms, central air, and tile floors, with a budget up to $300,000." "bathrooms": 2, "bedrooms": 2, "home_price_min": 0, "home_price_max": 300000, "property_features": "central air tile floors", "location": "Miami, Florida" } # Step 2: - Use the above constructed JSON format, and do a DSL template search. If you need to convert it to ES|QL queries, please do follow exactly the DSL template search ranges: 1. bathrooms is bigger or equal to the extracted one 2. bedrooms is bigger or equal to the extracted one 3. home_price is smaller or equal to the extracted one (home_price_max) - Before you do the searches, please DO refer to the requirements specified by the property search skills/property_search_skill.md. - Please print out the search template used for search, and then print out the top **4 results** for viewing.
  • Display name:Property search skills

  • Display description:Search for property

添加 skills:

我们把创建的 skill 添加到我们创建的 agent 中。

测试

我们还是按照之前的测试用例来进行测试:

Find a home within 10 miles of Miami, Florida that has 2 bedrooms, 2 bathrooms, central air, and tile floors, with a budget up to $300,000.

我们再以第二个例子来做展示:

Find a home within 10 miles of DeBary, Florida with 5 bedrooms, at least 2 bathrooms, central air, and a garage, with a budget up to $600,000.

结论

在这个例子里,我们看到了 skill 的强大之处。我们甚至省去了繁琐的代码及 workflow 的创建。我们只使用 skill 即可。这些 skill 只有在需要的时候才会装载。非常省内存。

祝大家学习愉快!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/27 23:30:30

维普 AIGC 率 55% 降到 8%!率零一键帮毕业生过维普 AIGC 检测!

维普 AIGC 率 55% 降到 8%&#xff01;率零一键帮毕业生过维普 AIGC 检测&#xff01; 投稿截止前 24 小时&#xff0c;维普初检报告显示整篇 AI 率 55.4%&#xff0c;期刊编辑部要求 15% 以下。这种时刻没法等"慢工出细活"&#xff0c;本次实测用 率零&#xff08;w…

作者头像 李华
网站建设 2026/4/27 23:28:04

NAT+OSPF+DHCP 全网通综合实验

一、IP 地址规划设备接口IP 地址 / 掩码备注AR3GE0/0/233.47.33.254/24连接 Client3AR3GE0/0/01.47.1.2/30连接 AR4AR3GE0/0/113.47.13.9/29连接 AR1AR4GE0/0/01.47.1.1/30连接 AR3AR4GE0/0/244.47.44.254/24连接 DNS-ServerAR4GE0/0/124.47.24.2/29连接 AR2AR1GE0/0/013.47.13…

作者头像 李华
网站建设 2026/4/27 23:27:32

3分钟搭建静态网站服务器:零配置http-server入门指南

3分钟搭建静态网站服务器&#xff1a;零配置http-server入门指南 【免费下载链接】http-server A simple, zero-configuration, command-line http server 项目地址: https://gitcode.com/gh_mirrors/ht/http-server 你是否曾为本地开发时预览HTML页面而烦恼&#xff1f…

作者头像 李华
网站建设 2026/4/27 23:21:55

3步掌握华硕笔记本硬件控制:开源工具G-Helper性能优化全解析

3步掌握华硕笔记本硬件控制&#xff1a;开源工具G-Helper性能优化全解析 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Str…

作者头像 李华
网站建设 2026/4/27 23:20:34

JANCD-MIF01继电器输出模块

YASKAWA JANCD-MIF01继电器输出模块专为工业控制系统设计&#xff0c;用于实现PLC数字信号与外部高功率设备的可靠隔离驱动。模块类型为继电器输出模块&#xff0c;适用于交流或直流负载控制。每路输出均采用继电器隔离&#xff0c;实现电气隔离与信号隔离双重保护。每通道触点…

作者头像 李华
网站建设 2026/4/27 23:20:33

高压氢储罐的构造详解

HPH&#xff0c;即高压氢储罐&#xff0c;作为氢能储存领域的关键设备&#xff0c;其构造对于储氢密度、安全性以及使用寿命起着决定性作用。对于投身于氢能技术研发、设备维护以及安全管理工作的专业人士而言&#xff0c;深入理解HPH的内部结构以及材料选择是至关重要的。 本文…

作者头像 李华