学习总结录 学习总结录
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
  • Java基础

  • Java集合

  • MySQL

  • Redis

  • JVM

  • 多线程

  • 计算机网络

  • Spring

  • Kafka

  • Elasticsearch

    • ElasticSearch基本概念
    • 文档基本操作
    • 倒排索引
    • 分词器
    • Mapping和常见字段类型
    • Index Template&Dynamic Template
    • Elasticsearch聚合分析简介
    • 基于词项和基于全文的搜索
    • 结构化搜索
    • 搜索的相关性算分
    • 单字符串多字段查询
    • SearchTemplate 和 Index Alias 查询
    • Function Score Query 优化算分
    • Term&Phrase Suggester
    • 自动补全于基于上下文的提示
      • 一、Completion Suggester
        • 1、概念
        • 2、案例
      • 二、Context Suggester
        • 1、概念
        • 2、案例
      • 参考
  • Python

  • 面试专题

  • 知识库
  • Elasticsearch
旭日
2023-06-08
目录

自动补全于基于上下文的提示

# 一、Completion Suggester

# 1、概念

Completion Suggester提供了“自动完成”的功能,用户每输入一个字符,就需要即时发送一个查询请求到后段查找匹配项。

由于自动补全对性能要求比较高,Elasticsearch采用了不同的数据结构,并非通过倒排索引来完成的,而是将Analyze的数据编码成FST和索引一起存放,FST会被ES整个加载到内存,因此速度很快。但是FST只能用于前缀查找。

# 2、案例

字段定义

DELETE articles
PUT articles
{
  "mappings": {
    "properties": {
      "title_completion":{
        "type": "completion"
      }
    }
  }
}

数据准备

POST articles/_bulk
{ "index" : { } }
{ "title_completion": "lucene is very cool"}
{ "index" : { } }
{ "title_completion": "Elasticsearch builds on top of lucene"}
{ "index" : { } }
{ "title_completion": "Elasticsearch rocks"}
{ "index" : { } }
{ "title_completion": "elastic is the company behind ELK stack"}
{ "index" : { } }
{ "title_completion": "Elk stack rocks"}
{ "index" : {} }

搜索

POST articles/_search
{
  "size": 0,
  "suggest": {
    "article-suggester": {
      "prefix": "elastic",
      "completion": {
        "field": "title_completion"
      }
    }
  }
}

这种搜索只能支持前缀搜索,如果用户输入的是中间的文字,是没办法进行推荐的。

# 二、Context Suggester

# 1、概念

为了搜索中加入更多的上下文信息,Context Suggester对Completion Suggester进行了扩展。

Context Suggester可以定义两种类型的Context:

  • Category - 任意的字符串
  • Geo - 地理位置信息

实现Context Suggester的具体步骤:

  • 定制一个Mapping
  • 索引数据,并且为每一个文档加入Context信息
  • 结合Context进行Suggestion查询

# 2、案例

mapping定义

DELETE comments
PUT comments
PUT comments/_mapping
{
  "properties": {
    "comment_autocomplete":{
      "type": "completion",
      "contexts":[{
        "type":"category",
        "name":"comment_category"
      }]
    }
  }
}

数据准备

POST comments/_doc
{
  "comment":"I love the star war movies",
  "comment_autocomplete":{
    "input":["star wars"],
    "contexts":{
      "comment_category":"movies"
    }
  }
}

POST comments/_doc
{
  "comment":"Where can I find a Starbucks",
  "comment_autocomplete":{
    "input":["starbucks"],
    "contexts":{
      "comment_category":"coffee"
    }
  }
}

数据搜索

POST comments/_search
{
  "suggest": {
    "MY_SUGGESTION": {
      "prefix": "sta",
      "completion":{
        "field":"comment_autocomplete",
        "contexts":{
          "comment_category":"coffee"
        }
      }
    }
  }
}

# 参考

Elasticsearch 核心技术与实战 (opens new window)

#Elasticsearch
上次更新: 2024/06/29, 15:13:44
Term&Phrase Suggester
Python基础

← Term&Phrase Suggester Python基础→

最近更新
01
基础概念
10-31
02
Pytorch
10-30
03
Numpy
10-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 旭日 | 蜀ICP备2021000788号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式