基于elasticsearch-dsl-py的文档索引及带高亮Percolator实现技术问询
没问题,我帮你把这套用于信息检索研讨会调研的Elasticsearch Percolator索引+高亮检索流程整理好,以下是终端可直接执行的完整步骤:
操作步骤:Percolator索引搭建与高亮检索
1. 创建包含Percolator字段的索引映射
首先我们要创建一个带Percolator类型字段的索引,用来存储预定义的检索规则:
curl -XPUT 'localhost:9200/my-index?pretty' \ -H 'Content-Type: application/json' \ -d' { "mappings": { "_doc": { "properties": { "title": { "type": "text" }, "query": { "type": "percolator" } } } } } '
这里的query字段是percolator类型,专门用来保存你需要匹配的检索规则;title则是普通文本字段,用来存储文章标题内容。
2. 索引待匹配的新文章
接下来把你JSON文件里的文章逐个索引到这个索引中,示例如下(替换id和title内容即可批量操作):
curl -XPUT 'localhost:9200/my-index/_doc/1?pretty' \ -H 'Content-Type: application/json' \ -d' { "title": "基于Elasticsearch的信息检索技术实践" } '
3. 配置Percolator检索规则并执行高亮查询
第一步:添加预定义的检索规则
先把你需要的检索条件存储到query字段中,比如匹配包含“信息检索”的标题:
curl -XPUT 'localhost:9200/my-index/_doc/rule1?pretty' \ -H 'Content-Type: application/json' \ -d' { "query": { "match": { "title": "信息检索" } } } '
第二步:执行Percolator查询并开启高亮
现在就可以用Percolator匹配符合规则的文章,同时开启高亮功能标记匹配内容:
curl -XGET 'localhost:9200/my-index/_search?pretty' \ -H 'Content-Type: application/json' \ -d' { "query": { "percolate": { "field": "query", "document": { "title": "基于Elasticsearch的信息检索技术实践" } } }, "highlight": { "fields": { "title": {} } } } '
返回结果里,匹配的关键词会被<em>标签包裹,实现高亮效果。
内容的提问来源于stack exchange,提问作者SalahAdDin
相关产品推荐
相关产品推荐

