使用ElasticSearch做suggestion服务

本文主要介绍如何使用ElasticSearch搭建网址链接的sug服务。
服务架构
在mysql中存储网址数据,使用task任务定时同步数据到es集群,用户请求服务端接口,服务端请求es获取suggestion数据返回给用户。
本项目主要使用了ElasticSearch的Completion Suggester:
Completion Suggester

es索引定义

 curl -X PUT 10.16.59.xx:9200/hao_mysite/mysite/_mapping -d '
 {
    "mysite": {
        "properties": {
            "name-sug": {
                "type": "completion",
                "analyzer": "standard",
                "search_analyzer": "standard"
            },
            "url-sug": {
                "type": "completion",
                "analyzer": "standard",
                "search_analyzer": "standard"
            },
            "name": {
                "type": "string"
            },
            "url": {
                "type": "string"
            }
        }
    }
}
 '

由于本项目有两个接口,一个是网站名字的suggestion,一个是网站url的suggestion,所以定义了两个type为completion的字段分别来进行suggest。

定时更新es数据

本项目使用es的bulk接口来进行数据的批量更新:
BULK接口文档
在mysql数据库表结构中有edit_time字段,每次更新或者新添加数据的时候都会将edit_time更新到当前时间,我们记录一个上次更新到的时间点,然后每次增量的选择edit_time在上次更新时间点到当前时间之间的数据,进行数据的更新,这样实现了数据的更新机制。
添加单条数据的接口如下:

curl -X PUT '10.16.59.xx:9200/hao_mysite/mysite/1?refresh=true' -d '
{
    "name-sug": {
        "input": [
            "12306"
        ],
        "weight": 34
    },
    "url-sug": {
        "input": [
            "12306.cn"
        ],
        "weight": 34
    },
    "name": "12306",
    "url": "www.12306.cn"
}
'

suggest搜索

curl -X POST '10.16.59.xx:9200/hao_mysite/mysite/_search?pretty' -d '
{
    "suggest": {
        "mysite-suggest": {
            "prefix": "www",
            "completion": {
                "field": "url-sug"
            }
        }
    }
}
'
好的,以下是使用 Elasticsearch 一个好用的韩语搜索引擎及自动补全的代码示例: 1. 准备数据 首先,准备一些韩语文本数据,例如韩语新闻、博客、论坛帖子等。本示例使用的数据集是从韩国最大的门户网站NAVER新闻中爬取的韩语新闻数据。 2. 创建索引 使用 Elasticsearch索引功能,将韩语文本数据创建索引,以便后续进行搜索和自动补全。以下是创建索引的示例代码: ```python from elasticsearch import Elasticsearch # 创建 Elasticsearch 实例 es = Elasticsearch() # 创建索引 index_name = "korean_news" if es.indices.exists(index_name): es.indices.delete(index=index_name) settings = { "settings": { "index": { "analysis": { "analyzer": { "korean_analyzer": { "tokenizer": "seunjeon_tokenizer" } }, "tokenizer": { "seunjeon_tokenizer": { "type": "korean_tokenizer" } } } } }, "mappings": { "properties": { "title": { "type": "text", "analyzer": "korean_analyzer" }, "content": { "type": "text", "analyzer": "korean_analyzer" } } } } es.indices.create(index=index_name, body=settings) ``` 3. 导入数据 使用 Elasticsearch 提供的 API 或者 Python 的 elasticsearch 库,将准备好的韩语文本数据导入到 Elasticsearch 中。以下是导入数据的示例代码: ```python import json # 从文件中读取数据 with open("korean_news.json", "r", encoding="utf-8") as f: news_list = json.load(f) # 导入数据 for news in news_list: es.index(index=index_name, body=news) ``` 4. 搜索功能 使用 Elasticsearch 提供的搜索功能,可以进行全文搜索、模糊搜索、精确搜索等多种搜索方式。以下是使用全文搜索功能进行搜索的示例代码: ```python # 全文搜索 query = "코로나" result = es.search(index=index_name, body={"query": {"match": {"title": query}}}) for hit in result["hits"]["hits"]: print(hit["_source"]["title"]) ``` 5. 自动补全功能 使用 Elasticsearch 提供的自动补全功能,可以在用户输入关键字时自动提示相关的搜索结果。以下是开启自动补全功能并进行自动补全的示例代码: ```python # 开启自动补全功能 settings["settings"]["index"]["analysis"]["analyzer"]["autocomplete_analyzer"] = { "tokenizer": "autocomplete_tokenizer" } settings["settings"]["index"]["analysis"]["tokenizer"]["autocomplete_tokenizer"] = { "type": "edge_ngram", "min_gram": 1, "max_gram": 20, "token_chars": ["letter", "digit"] } settings["mappings"]["properties"]["title"]["fields"] = { "autocomplete": { "type": "text", "analyzer": "autocomplete_analyzer" } } es.indices.put_settings(index=index_name, body=settings) # 自动补全 query = "코로" result = es.search(index=index_name, body={ "suggest": { "title-suggest": { "prefix": query, "completion": { "field": "title.autocomplete", "size": 10 } } } }) for suggestion in result["suggest"]["title-suggest"][0]["options"]: print(suggestion["text"]) ``` 以上是使用 Elasticsearch 一个好用的韩语搜索引擎及自动补全的示例代码,供您参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值